Note how setting the log level in code of the console logger only matters if the configuration file allows logging at the specified levels. The default level is INFO.
import java.util.logging.*; public class Logging1 { public static void main(String[] args) { Logger log = Logger.getLogger("edu.kishwaukeecollege.Logging1"); log.setLevel(Level.INFO); log.severe("Test of logger SEVERE message"); log.warning("Test of logger WARNING message"); log.info("Test of logger INFO message"); log.config("Test of logger CONFIG message"); log.fine("Test of logger FINE message"); log.finer("Test of logger FINER message"); log.finest("Test of logger FINEST message"); } } C:\>java Logging1 Feb 14, 2010 3:22:52 AM Logging1 main SEVERE: Test of logger SEVERE message Feb 14, 2010 3:22:52 AM Logging1 main WARNING: Test of logger WARNING message Feb 14, 2010 3:22:52 AM Logging1 main INFO: Test of logger INFO message // contents of mylog.props file used in next command handlers= java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level = FINER java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter C:\>java -Djava.util.logging.config.file=mylog.props Logging1 Feb 14, 2010 3:23:01 AM Logging1 main SEVERE: Test of logger SEVERE message Feb 14, 2010 3:23:01 AM Logging1 main WARNING: Test of logger WARNING message Feb 14, 2010 3:23:01 AM Logging1 main INFO: Test of logger INFO message Feb 14, 2010 3:23:01 AM Logging1 main CONFIG: Test of logger CONFIG message Feb 14, 2010 3:23:01 AM Logging1 main FINE: Test of logger FINE message Feb 14, 2010 3:23:01 AM Logging1 main FINER: Test of logger FINER message */
Note how the file log has a different logging level than the console. Note also that the output to the file defaults to XML format. You can specify your own formatter or use one of the built-in formatters. The console default formatter produces a simple text output. You can have quite a bit of detail recorded in the log entries. Notice that the XML format log entries include the class and method where the log entry came from.
import java.util.logging.*; import java.io.*; public class Logging2 { public static void main(String[] args) { Logger log = null; try { FileHandler fh = new FileHandler("test.log", true); log = Logger.getLogger("edu.kishwaukeecollege.Logging2"); log.addHandler(fh); } catch (IOException e) { e.printStackTrace(); System.exit(1); } log.setLevel(Level.CONFIG); log.severe("Test of logger SEVERE message"); log.warning("Test of logger WARNING message"); log.info("Test of logger INFO message"); log.config("Test of logger CONFIG message"); log.fine("Test of logger FINE message"); log.finer("Test of logger FINER message"); log.finest("Test of logger FINEST message"); } } /* Screen output: C:\>java Logging2 Feb 14, 2010 3:29:01 AM Logging2 main SEVERE: Test of logger SEVERE message Feb 14, 2010 3:29:01 AM Logging2 main WARNING: Test of logger WARNING message Feb 14, 2010 3:29:01 AM Logging2 main INFO: Test of logger INFO message Contents of log file: <?xml version="1.0" encoding="windows-1252" standalone="no"?> <!DOCTYPE log SYSTEM "logger.dtd"> <log> <record> <date>2010-02-14T03:29:01</date> <millis>1266139741531</millis> <sequence>0</sequence> <logger>edu.kishwaukeecollege.Logging2</logger> <level>SEVERE</level> <class>Logging2</class> <method>main</method> <thread>10</thread> <message>Test of logger SEVERE message</message> </record> <record> <date>2010-02-14T03:29:01</date> <millis>1266139741546</millis> <sequence>1</sequence> <logger>edu.kishwaukeecollege.Logging2</logger> <level>WARNING</level> <class>Logging2</class> <method>main</method> <thread>10</thread> <message>Test of logger WARNING message</message> </record> <record> <date>2010-02-14T03:29:01</date> <millis>1266139741546</millis> <sequence>2</sequence> <logger>edu.kishwaukeecollege.Logging2</logger> <level>INFO</level> <class>Logging2</class> <method>main</method> <thread>10</thread> <message>Test of logger INFO message</message> </record> <record> <date>2010-02-14T03:29:01</date> <millis>1266139741546</millis> <sequence>3</sequence> <logger>edu.kishwaukeecollege.Logging2</logger> <level>CONFIG</level> <class>Logging2</class> <method>main</method> <thread>10</thread> <message>Test of logger CONFIG message</message> </record> </log>
Logger log = Logger.getLogger(""); Handler[] handlers = log.getHandlers(); if (handlers[0] instanceof ConsoleHandler) log.removeHandler(handlers[0]);
import java.util.logging.*; import java.io.*; public class Logging3 { public static void main(String[] args) { Logger log = Logger.getLogger("edu.kishwaukeecollege.Logging3"); log.setUseParentHandlers(false); Handler handler = new ConsoleHandler(); handler.setLevel(Level.FINE); // changes console log level log.addHandler(handler); log.setLevel(Level.ALL); // can reduce what handler sees log.severe("Test of logger SEVERE message"); log.warning("Test of logger WARNING message"); log.info("Test of logger INFO message"); log.config("Test of logger CONFIG message"); log.fine("Test of logger FINE message"); log.finer("Test of logger FINER message"); log.finest("Test of logger FINEST message"); } } /* Sample output: Feb 14, 2010 11:08:37 AM Logging3 main SEVERE: Test of logger SEVERE message Feb 14, 2010 11:08:37 AM Logging3 main WARNING: Test of logger WARNING message Feb 14, 2010 11:08:37 AM Logging3 main INFO: Test of logger INFO message Feb 14, 2010 11:08:37 AM Logging3 main CONFIG: Test of logger CONFIG message Feb 14, 2010 11:08:37 AM Logging3 main FINE: Test of logger FINE message */