Share this page 

Use a Log fileTag(s): Environment


Since 1.4, a logging functionnality is included with the JDK. It's the java.util.logging package.
import java.util.logging.*;
import java.io.*;

public class TestLog {

public static Logger logger;

static {
    try {
      boolean append = true;
      FileHandler fh = new FileHandler("TestLog.log", append);
      //fh.setFormatter(new XMLFormatter());
      fh.setFormatter(new SimpleFormatter());
      logger = Logger.getLogger("TestLog");
      logger.addHandler(fh);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
}

public static void main(String args[]) {
    logger.severe("my severe message");
    logger.warning("my warning message");
    logger.info("my info message");
    }
}
and the result is
2005-02-28 21:19:28 TestLog main
GRAVE: my severe message
2005-02-28 21:19:28 TestLog main
ATTENTION: my warning message
2005-02-28 21:19:28 TestLog main
INFO: my info message
if the XMLFormatter is in use then the output is
<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2005-02-28T21:21:09</date>
  <millis>1109643669250</millis>
  <sequence>0</sequence>
  <logger>TestLog</logger>
  <level>SEVERE</level>
  <class>TestLog</class>
  <method>main</method>
  <thread>10</thread>
  <message>my severe message</message>
</record>
<record>
  <date>2005-02-28T21:21:09</date>
  <millis>1109643669328</millis>
  <sequence>1</sequence>
  <logger>TestLog</logger>
  <level>WARNING</level>
  <class>TestLog</class>
  <method>main</method>
  <thread>10</thread>
  <message>my warning message</message>
</record>
<record>
  <date>2005-02-28T21:21:09</date>
  <millis>1109643669328</millis>
  <sequence>2</sequence>
  <logger>TestLog</logger>
  <level>INFO</level>
  <class>TestLog</class>
  <method>main</method>
  <thread>10</thread>
  <message>my info message</message>
</record>
</log>
to customize the output, you can provide you own formatter
import java.util.logging.*;
import java.io.*;

public class TestLog {

public static Logger logger;

static {
    try {
      boolean append = true;
      FileHandler fh = new FileHandler("TestLog.log", append);
      fh.setFormatter(new Formatter() {
         public String format(LogRecord rec) {
            StringBuffer buf = new StringBuffer(1000);
            buf.append(new java.util.Date());
            buf.append(' ');
            buf.append(rec.getLevel());
            buf.append(' ');
            buf.append(formatMessage(rec));
            buf.append('\n');
            return buf.toString();
            }
          });
      logger = Logger.getLogger("TestLog");
      logger.addHandler(fh);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
}

public static void main(String args[]) {
    logger.severe("my severe message");
    logger.warning("my warning message");
    logger.info("my info message");
    }
}
then the output is
Mon Feb 28 21:30:54 EST 2005 SEVERE my severe message
Mon Feb 28 21:30:54 EST 2005 WARNING my warning message
Mon Feb 28 21:30:54 EST 2005 INFO my info message

To limit the log file size and set a rolling pattern

int limit = 1000000; // 1 Mb
int numLogFiles = 3;
FileHandler fh = new FileHandler("TestLog%g.log", limit, numLogFiles);
will give TestLog0.log, TestLog1.log and so on.

Default values are defined in JRE_HOME/lib/logging.properties. To use a different properties file, you specify a filename with the java.util.logging.config.file system property.

java -Djava.util.logging.config.file=mylogging.props TestLog

To suppress the logging output to the console
Logger rootLogger = Logger.getLogger("");
Handler[] handlers = rootLogger.getHandlers();
if (handlers[0] instanceof ConsoleHandler) {
    rootLogger.removeHandler(handlers[0]);
}
or modify the logging.properties file located in JRE_HOME/lib (default).

Look for the property handlers and remove the value java.util.logging.ConsoleHandler

You may want to take a look at another popular logging mechanism : Log4J.

See also this HowTo and this one