/**************************************************************************** Copyright (c) 2003, Landmark Graphics and others. All rights reserved. This program and accompanying materials are made available under the terms of the Common Public License - v1.0, which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html ****************************************************************************/ package com.lgc.wsh.util; import java.io.*; import java.util.LinkedList; import java.util.List; import java.util.logging.*; /** An alternative to ConsoleHandler. Uses CleanFormatter and System.out instead of SimpleFormatter and System.err @author W.S. Harlan, Landmark Graphics */ public class CleanHandler extends Handler { private static List s_printStreams = new LinkedList(); /** Keep track of whether CleanHandler has been set as a default */ private static boolean s_setDefault; /** Construct a new CleanHandler. */ public CleanHandler() { setFormatter(new CleanFormatter()); } /** All CleanHandlers will also log to this file. @param fileName Name of file to log to. @throws FileNotFoundException if file not found */ public static void addGlobalLogFile(String fileName) throws FileNotFoundException { s_printStreams.add(new PrintStream(new FileOutputStream(fileName), true)); } @Override public void publish(LogRecord record) { if (record == null || !isLoggable(record)) return; String message = getFormatter().format(record); if (message == null) return; if (record.getLevel().intValue() > Level.INFO.intValue()) { System.err.print(message); System.err.flush(); } else { System.out.print(message); System.out.flush(); } for (PrintStream ps: s_printStreams) { ps.print(message); } } @Override public void close() {} @Override public void flush() {} /** Call this from your code to test each type of log message */ public static void testLogger() { setDefaultHandler(); assert null != CleanHandler.class.getResource("CleanHandler.properties"); assert null != java.util.ResourceBundle.getBundle ("com.lgc.wsh.util.CleanHandler") : "can't find rb"; Logger logger = Logger.getLogger("com.lgc.wsh.util", "com.lgc.wsh.util.CleanHandler"); logger.severe("test a severe"); logger.warning("test a warning"); logger.info("test an info"); logger.info("test a\\"); logger.info(" continued info"); logger.config("test an config"); logger.fine("test a fine"); logger.finer("test a finer"); logger.finest("test a finest"); logger.info("testmessage"); logger.info("Try this:>>${testmessage}<<"); logger.info("Try this:>>${testmessage}<< >>${testmessage}<<"); } /** Test code * @param args command line */ public static void main(String[] args) { testLogger(); } /** If the user has not specified a java property for the global Handler, then set the default global handler to this CleanHandler at an INFO level. */ public static void setDefaultHandler() { synchronized (CleanHandler.class) { if (s_setDefault) return; if (System.getProperties().getProperty ("java.util.logging.config.file") == null && System.getProperties().getProperty ("java.util.logging.config.class") == null) { try { LogManager.getLogManager().readConfiguration (new java.io.ByteArrayInputStream ("handlers=com.lgc.wsh.util.CleanHandler\n.level=INFO\n" .getBytes())); } catch (java.io.IOException e) { e.printStackTrace(); throw new IllegalStateException("This should never fail "+ e.getMessage()); } } s_setDefault = true; } } }