logo
Generate logs in java
The Logger object
The various handlers
The various formatters
The logs of levels
Log a message
The long-awaited example

Generate logs in java

We will simply use the standard Java API to allow our application to generate logs.

The Logger object

The logger object allows us to obtain the logs. To show where the message originates, it must retrieve the object by calling the static method "getLogger" in the class "Logger" with the full name of the class. It is better to do this in the following way (in case we rename our class later):
private final static Logger LOGGER = Logger.getLogger(MyClass.class.getName());

The various handlers

Each logger can use multiple handlers. Each handler is responsible for directing the message (LogRecord) logg to a terminal, a socket, a file..
ConsoleHandler
FileHandler
MemoryHandler
SocketHandler
StreamHandler
To add a handler:
logger.addHandler(handler);

The various formatters

A formatter is made for formatting :) amazing. It is easily possible to redefine one by creating a class that extends Formatter. However, some formatters exist by default:
SimpleFormatter
XMLFormatter
To add a formatter:
handler.setFormatter(formatter);

The logs of levels

Here is the list of log levels (the more precise below).
OFF
SEVERE
WARNING
INFO
CONFIG
FINE
FINER
FINEST
ALL
These levels are constants of the Logger class. For a log level warning we do Level.WARNING. It is possible to define a different level by handler.
handler.setLevel(Level.INFO)
Or a single level for the logger.
logger.setLevel(Level.INFO)

Log a message

Nothing is easier than to log a message:
logger.log(Level.INFO, "message to log");

The long-awaited example

Without delay here's a basic example:
package org.hanoo.loggerExample;
 
import java.io.IOException;
 
 
public class LoggerTest {
 private final static Logger logger = Logger.getLogger(LoggerTest.class.getName());
 private static FileHandler fh = null;
 
 public static void init(){
 try {
 fh=new FileHandler("loggerExample.log", false);
 } catch (SecurityException | IOException e) {
 e.printStackTrace();
 }
 Logger l = Logger.getLogger("");
 fh.setFormatter(new SimpleFormatter());
 l.addHandler(fh);
 l.setLevel(Level.CONFIG);
 }
 
 public static void main(String[] args) {
 LoggerTest.init();
 
 logger.log(Level.INFO, "message 1");
 logger.log(Level.SEVERE, "message 2");
 logger.log(Level.FINE, "message 3");
 LoggerTest2.thing();
 }
}
package org.hanoo.loggerExample;
 
import java.util.logging.Level;
 
public class LoggerTest2 {
 private final static Logger logger = Logger.getLogger(LoggerTest.class.getName());
 
 public static void thing(){
 logger.log(Level.WARNING,"something to log");
 }
}
 
And here is the file that was generated:
févr. 24, 2012 6:09:19 PM org.hanoo.loggerExample.LoggerTest main
Infos: message 1
févr. 24, 2012 6:09:19 PM org.hanoo.loggerExample.LoggerTest main
Grave: message 2
févr. 24, 2012 6:09:19 PM org.hanoo.loggerExample.LoggerTest2 thing
Avertissement: something to log
 
kjackvony :

This was extremely helpful, much more so than other blogs that tried to explain the topic. Thank you!

Gustavo :

How do you split log files? I have the problem that they grow a lot. Thank you! BTW: Great example!

lingceng :

Good job! ${java.home}/lib/logging.properties is read when using Logger.getLogger(cname), I might add.

Kothaufen :

What if I want to log at different classes? Do I need to define one logger for each class?

asd :

asasadadasadasadasdasda

asdsadds :

asdsadasdasdasd

zeronyk :

I have a Problem what is when you want to print all Exeptions in the Log file. Can I add a Listener or something like that ?. I have multiple Swing Frames which call each other and want the Logger to log all the exeptions from every Screen. G. Michael

A :

Great

Add a comment:





You can use these HTML tags: <br><a><i><b>