1 /* 2 * Copyright (c) 2003-2008 by Cosylab d. d. 3 * 4 * This file is part of Java-Common. 5 * 6 * Java-Common is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * Java-Common is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with Java-Common. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package com.cosylab.logging; 21 22 import java.io.ByteArrayInputStream; 23 24 import java.util.logging.Formatter; 25 import java.util.logging.Handler; 26 import java.util.logging.Level; 27 import java.util.logging.LogManager; 28 import java.util.logging.LogRecord; 29 import java.util.logging.Logger; 30 31 32 /** 33 * Implementation of Java Logging API handler. 34 * 35 * @author <a href="mailto:matej.sekoranjaATcosylab.com">Matej Sekoranja</a> 36 * @version $id$ 37 */ 38 public class ConsoleLogHandler extends Handler 39 { 40 /** Logging formatter. */ 41 private Formatter formatter; 42 43 /** 44 * Default constructor. 45 */ 46 public ConsoleLogHandler() 47 { 48 this(new ConsoleLogFormatter()); 49 } 50 51 /** 52 * Construct handler with using giver formatter. 53 * 54 * @param formatter console log formatter, non-<code>null</code>. 55 */ 56 public ConsoleLogHandler(Formatter formatter) 57 { 58 assert (formatter != null); 59 this.formatter = formatter; 60 } 61 62 /** 63 * @see java.util.logging.Handler#close() 64 */ 65 public void close() throws SecurityException 66 { 67 // noop 68 } 69 70 /** 71 * @see java.util.logging.Handler#flush() 72 */ 73 public void flush() 74 { 75 System.out.flush(); 76 } 77 78 /** 79 * Prints the log record to the console using the current formatter, if the 80 * log record is loggable. 81 * 82 * @param record the log record to publish 83 * 84 * @see java.util.logging.Handler#publish(java.util.logging.LogRecord) 85 */ 86 public void publish(LogRecord record) 87 { 88 if (isLoggable(record)) { 89 System.out.print(formatter.format(record)); 90 } 91 } 92 93 /** 94 * Overrides default JVM logging configuration. This is convenience method 95 * - there is no need for additional configuration file and system 96 * property. 97 */ 98 public static void initializeConsoleLogging() 99 { 100 try { 101 /*ClassLoader.getSystemClassLoader().loadClass(ConsoleLogHandler.class 102 .getName());*/ 103 104 // to make sure class loaded, because of problems with JUnit class loader 105 /*ConsoleLogHandler clh= new ConsoleLogHandler(); 106 clh.close();*/ 107 108 ByteArrayInputStream bais = new ByteArrayInputStream(("handlers=" 109 + ConsoleLogHandler.class.getName()).getBytes()); 110 LogManager.getLogManager().readConfiguration(bais); 111 } catch (Throwable th) { 112 th.printStackTrace(); 113 } 114 } 115 116 /** 117 * Simple test. 118 * 119 * @param args 120 */ 121 public static void main(String[] args) 122 { 123 initializeConsoleLogging(); 124 125 Logger logger = Logger.getLogger("loggerName"); 126 logger.info("Testing: 1, 2, 3..."); 127 logger.info("... 4, 5, 6."); 128 logger.setLevel(Level.ALL); 129 logger.finest("... finest"); 130 } 131 } 132 133 /* __oOo__ */