colorful logging v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 25 Dec 2013 00:43:06 +0100
branchv_0
changeset 55f5ed7c4efacc
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
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Tue Dec 24 14:36:14 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Wed Dec 25 00:43:06 2013 +0100
     1.3 @@ -44,6 +44,7 @@
     1.4  	private Configuration configuration;
     1.5  
     1.6  	public static void main(String[] args) {
     1.7 +		log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
     1.8  
     1.9  		if (args.length == 0) {
    1.10  			args = new String[]{CLIParser.Tokens.INFO_HELP};
    1.11 @@ -56,6 +57,7 @@
    1.12  			CLIStarter starter = new CLIStarter(options);
    1.13  			starter.installDefaultConfiguration();
    1.14  			starter.process();
    1.15 +			log.log(Level.FINE, "All done");
    1.16  		} catch (CLIParserException e) {
    1.17  			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    1.18  		} catch (InvalidOptionsException e) {
    1.19 @@ -113,6 +115,7 @@
    1.20  				throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
    1.21  			} else {
    1.22  				try (DatabaseConnection c = dd.connect()) {
    1.23 +					log.log(Level.FINE, "Database connected");
    1.24  					Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
    1.25  					c.executeQuery(options.getSQLCommand(), f);
    1.26  				}
    1.27 @@ -138,10 +141,11 @@
    1.28  		Constants.DIR.mkdir();
    1.29  
    1.30  		if (Constants.CONFIG_FILE.exists()) {
    1.31 -			log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
    1.32 +			log.log(Level.FINER, "Config file already exists: {0}", Constants.CONFIG_FILE);
    1.33  		} else {
    1.34  			try {
    1.35  				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
    1.36 +				log.log(Level.FINE, "Installing default config file: {0}", Constants.CONFIG_FILE);
    1.37  			} catch (IOException e) {
    1.38  				throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
    1.39  			}
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Tue Dec 24 14:36:14 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Wed Dec 25 00:43:06 2013 +0100
     2.3 @@ -256,7 +256,7 @@
     2.4  	}
     2.5  
     2.6  	public void resetAll() {
     2.7 -		printCodes(0);
     2.8 +		printCodes(TerminalStyle.Reset.code);
     2.9  	}
    2.10  
    2.11  	private void printCodes(int... codes) {
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Constants.java	Tue Dec 24 14:36:14 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Constants.java	Wed Dec 25 00:43:06 2013 +0100
     3.3 @@ -25,6 +25,8 @@
     3.4   */
     3.5  public class Constants {
     3.6  
     3.7 +	public static final String PROGRAM_NAME = "SQL-DK";
     3.8 +	public static final String JAVA_PACKAGE = Constants.class.getPackage().getName();
     3.9  	public static final String WEBSITE = "https://sql-dk.globalcode.info/";
    3.10  	public static final String LICENSE_FILE = "info/globalcode/sql/dk/license.txt";
    3.11  	public static final String VERSION_FILE = "info/globalcode/sql/dk/version.txt";
     4.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Tue Dec 24 14:36:14 2013 +0100
     4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Wed Dec 25 00:43:06 2013 +0100
     4.3 @@ -26,6 +26,8 @@
     4.4  import java.sql.PreparedStatement;
     4.5  import java.sql.ResultSet;
     4.6  import java.sql.SQLException;
     4.7 +import java.util.logging.Level;
     4.8 +import java.util.logging.Logger;
     4.9  
    4.10  /**
    4.11   *
    4.12 @@ -33,6 +35,7 @@
    4.13   */
    4.14  public class DatabaseConnection implements AutoCloseable {
    4.15  
    4.16 +	private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
    4.17  	private DatabaseDefinition databaseDefinition;
    4.18  	private Connection connection;
    4.19  
    4.20 @@ -58,9 +61,11 @@
    4.21  
    4.22  	private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
    4.23  		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
    4.24 +			log.log(Level.FINE, "Statement prepared");
    4.25  			sqlCommand.parametrize(ps);
    4.26  
    4.27  			boolean isRS = ps.execute();
    4.28 +			log.log(Level.FINE, "Statement executed");
    4.29  			if (isRS) {
    4.30  				try (ResultSet rs = ps.getResultSet()) {
    4.31  					processResultSet(sqlCommand, rs, formatter);
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java	Wed Dec 25 00:43:06 2013 +0100
     5.3 @@ -0,0 +1,75 @@
     5.4 +/**
     5.5 + * SQL-DK
     5.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     5.7 + *
     5.8 + * This program is free software: you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License as published by
    5.10 + * the Free Software Foundation, either version 3 of the License, or
    5.11 + * (at your option) any later version.
    5.12 + *
    5.13 + * This program is distributed in the hope that it will be useful,
    5.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    5.16 + * GNU General Public License for more details.
    5.17 + *
    5.18 + * You should have received a copy of the GNU General Public License
    5.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    5.20 + */
    5.21 +package info.globalcode.sql.dk.logging;
    5.22 +
    5.23 +import info.globalcode.sql.dk.ColorfulPrintWriter;
    5.24 +import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
    5.25 +import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalStyle;
    5.26 +import static info.globalcode.sql.dk.Functions.rpad;
    5.27 +import java.io.StringWriter;
    5.28 +import java.util.logging.Formatter;
    5.29 +import java.util.logging.Level;
    5.30 +import java.util.logging.LogRecord;
    5.31 +
    5.32 +/**
    5.33 + *
    5.34 + * @author Ing. František Kučera (frantovo.cz)
    5.35 + */
    5.36 +public class ColorfulConsoleFormatter extends Formatter {
    5.37 +
    5.38 +	@Override
    5.39 +	public String format(LogRecord r) {
    5.40 +		StringWriter sw = new StringWriter();
    5.41 +		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
    5.42 +			printLevel(out, r.getLevel());
    5.43 +			printMessage(out, r);
    5.44 +			printThrowable(out, r.getThrown());
    5.45 +			out.println();
    5.46 +		}
    5.47 +		return sw.toString();
    5.48 +	}
    5.49 +
    5.50 +	private void printLevel(ColorfulPrintWriter out, Level l) {
    5.51 +		TerminalColor color = TerminalColor.Magenta;
    5.52 +		TerminalStyle style;
    5.53 +
    5.54 +		if (l == Level.SEVERE) {
    5.55 +			color = TerminalColor.Red;
    5.56 +		} else if (l == Level.WARNING) {
    5.57 +			color = TerminalColor.Yellow;
    5.58 +		}
    5.59 +
    5.60 +		out.print(color, rpad(l.getLocalizedName() + ": ", 10));
    5.61 +	}
    5.62 +
    5.63 +	private void printMessage(ColorfulPrintWriter out, LogRecord r) {
    5.64 +		out.print(formatMessage(r));
    5.65 +	}
    5.66 +
    5.67 +	private void printThrowable(ColorfulPrintWriter out, Throwable t) {
    5.68 +		if (t != null) {
    5.69 +			out.print(": ");
    5.70 +			out.print(TerminalColor.Red, t.getClass().getSimpleName());
    5.71 +			String message = t.getLocalizedMessage();
    5.72 +			if (message != null) {
    5.73 +				out.print(": ");
    5.74 +				out.print(message);
    5.75 +			}
    5.76 +		}
    5.77 +	}
    5.78 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java	Wed Dec 25 00:43:06 2013 +0100
     6.3 @@ -0,0 +1,49 @@
     6.4 +/**
     6.5 + * SQL-DK
     6.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     6.7 + *
     6.8 + * This program is free software: you can redistribute it and/or modify
     6.9 + * it under the terms of the GNU General Public License as published by
    6.10 + * the Free Software Foundation, either version 3 of the License, or
    6.11 + * (at your option) any later version.
    6.12 + *
    6.13 + * This program is distributed in the hope that it will be useful,
    6.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    6.16 + * GNU General Public License for more details.
    6.17 + *
    6.18 + * You should have received a copy of the GNU General Public License
    6.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    6.20 + */
    6.21 +package info.globalcode.sql.dk.logging;
    6.22 +
    6.23 +import info.globalcode.sql.dk.Constants;
    6.24 +import java.util.logging.ConsoleHandler;
    6.25 +import java.util.logging.Level;
    6.26 +import java.util.logging.Logger;
    6.27 +
    6.28 +/**
    6.29 + * Configures logging subsystem.
    6.30 + * Usage: java -Djava.util.logging.config.class=info.globalcode.sql.dk.logging.LoggerInitializer …
    6.31 + *
    6.32 + * @author Ing. František Kučera (frantovo.cz)
    6.33 + */
    6.34 +public class LoggerInitializer {
    6.35 +
    6.36 +	public LoggerInitializer() {
    6.37 +		Logger logger = Logger.getLogger(Constants.JAVA_PACKAGE);
    6.38 +		ConsoleHandler handler = new ConsoleHandler();
    6.39 +		ColorfulConsoleFormatter formatter = new ColorfulConsoleFormatter();
    6.40 +
    6.41 +		logger.addHandler(handler);
    6.42 +		handler.setFormatter(formatter);
    6.43 +
    6.44 +		handler.setLevel(Level.FINE);
    6.45 +		logger.setLevel(Level.FINE);
    6.46 +
    6.47 +
    6.48 +		/**
    6.49 +		 * TODO: FileHandler – detailed logs in file in ~/sql-dk/log/…
    6.50 +		 */
    6.51 +	}
    6.52 +}