colorful logging v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 25 Dec 2013 00:43:06 +0100
branchv_0
changeset 55 f5ed7c4efacc
parent 54 53020d0bd2e4
child 56 29f45ab3b959
colorful logging
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java
java/sql-dk/src/info/globalcode/sql/dk/Constants.java
java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Tue Dec 24 14:36:14 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Wed Dec 25 00:43:06 2013 +0100
@@ -44,6 +44,7 @@
 	private Configuration configuration;
 
 	public static void main(String[] args) {
+		log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
 
 		if (args.length == 0) {
 			args = new String[]{CLIParser.Tokens.INFO_HELP};
@@ -56,6 +57,7 @@
 			CLIStarter starter = new CLIStarter(options);
 			starter.installDefaultConfiguration();
 			starter.process();
+			log.log(Level.FINE, "All done");
 		} catch (CLIParserException e) {
 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
 		} catch (InvalidOptionsException e) {
@@ -113,6 +115,7 @@
 				throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
 			} else {
 				try (DatabaseConnection c = dd.connect()) {
+					log.log(Level.FINE, "Database connected");
 					Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
 					c.executeQuery(options.getSQLCommand(), f);
 				}
@@ -138,10 +141,11 @@
 		Constants.DIR.mkdir();
 
 		if (Constants.CONFIG_FILE.exists()) {
-			log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
+			log.log(Level.FINER, "Config file already exists: {0}", Constants.CONFIG_FILE);
 		} else {
 			try {
 				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
+				log.log(Level.FINE, "Installing default config file: {0}", Constants.CONFIG_FILE);
 			} catch (IOException e) {
 				throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
 			}
--- a/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Tue Dec 24 14:36:14 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Wed Dec 25 00:43:06 2013 +0100
@@ -256,7 +256,7 @@
 	}
 
 	public void resetAll() {
-		printCodes(0);
+		printCodes(TerminalStyle.Reset.code);
 	}
 
 	private void printCodes(int... codes) {
--- a/java/sql-dk/src/info/globalcode/sql/dk/Constants.java	Tue Dec 24 14:36:14 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/Constants.java	Wed Dec 25 00:43:06 2013 +0100
@@ -25,6 +25,8 @@
  */
 public class Constants {
 
+	public static final String PROGRAM_NAME = "SQL-DK";
+	public static final String JAVA_PACKAGE = Constants.class.getPackage().getName();
 	public static final String WEBSITE = "https://sql-dk.globalcode.info/";
 	public static final String LICENSE_FILE = "info/globalcode/sql/dk/license.txt";
 	public static final String VERSION_FILE = "info/globalcode/sql/dk/version.txt";
--- a/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Tue Dec 24 14:36:14 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Wed Dec 25 00:43:06 2013 +0100
@@ -26,6 +26,8 @@
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  *
@@ -33,6 +35,7 @@
  */
 public class DatabaseConnection implements AutoCloseable {
 
+	private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
 	private DatabaseDefinition databaseDefinition;
 	private Connection connection;
 
@@ -58,9 +61,11 @@
 
 	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
 		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
+			log.log(Level.FINE, "Statement prepared");
 			sqlCommand.parametrize(ps);
 
 			boolean isRS = ps.execute();
+			log.log(Level.FINE, "Statement executed");
 			if (isRS) {
 				try (ResultSet rs = ps.getResultSet()) {
 					processResultSet(sqlCommand, rs, formatter);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java	Wed Dec 25 00:43:06 2013 +0100
@@ -0,0 +1,75 @@
+/**
+ * SQL-DK
+ * Copyright © 2013 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package info.globalcode.sql.dk.logging;
+
+import info.globalcode.sql.dk.ColorfulPrintWriter;
+import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
+import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalStyle;
+import static info.globalcode.sql.dk.Functions.rpad;
+import java.io.StringWriter;
+import java.util.logging.Formatter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class ColorfulConsoleFormatter extends Formatter {
+
+	@Override
+	public String format(LogRecord r) {
+		StringWriter sw = new StringWriter();
+		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
+			printLevel(out, r.getLevel());
+			printMessage(out, r);
+			printThrowable(out, r.getThrown());
+			out.println();
+		}
+		return sw.toString();
+	}
+
+	private void printLevel(ColorfulPrintWriter out, Level l) {
+		TerminalColor color = TerminalColor.Magenta;
+		TerminalStyle style;
+
+		if (l == Level.SEVERE) {
+			color = TerminalColor.Red;
+		} else if (l == Level.WARNING) {
+			color = TerminalColor.Yellow;
+		}
+
+		out.print(color, rpad(l.getLocalizedName() + ": ", 10));
+	}
+
+	private void printMessage(ColorfulPrintWriter out, LogRecord r) {
+		out.print(formatMessage(r));
+	}
+
+	private void printThrowable(ColorfulPrintWriter out, Throwable t) {
+		if (t != null) {
+			out.print(": ");
+			out.print(TerminalColor.Red, t.getClass().getSimpleName());
+			String message = t.getLocalizedMessage();
+			if (message != null) {
+				out.print(": ");
+				out.print(message);
+			}
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java	Wed Dec 25 00:43:06 2013 +0100
@@ -0,0 +1,49 @@
+/**
+ * SQL-DK
+ * Copyright © 2013 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package info.globalcode.sql.dk.logging;
+
+import info.globalcode.sql.dk.Constants;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Configures logging subsystem.
+ * Usage: java -Djava.util.logging.config.class=info.globalcode.sql.dk.logging.LoggerInitializer …
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class LoggerInitializer {
+
+	public LoggerInitializer() {
+		Logger logger = Logger.getLogger(Constants.JAVA_PACKAGE);
+		ConsoleHandler handler = new ConsoleHandler();
+		ColorfulConsoleFormatter formatter = new ColorfulConsoleFormatter();
+
+		logger.addHandler(handler);
+		handler.setFormatter(formatter);
+
+		handler.setLevel(Level.FINE);
+		logger.setLevel(Level.FINE);
+
+
+		/**
+		 * TODO: FileHandler – detailed logs in file in ~/sql-dk/log/…
+		 */
+	}
+}