
Generate logs in java
The Logger object
The various handlers
The various formatters
The logs of levels
Log a message
The long-awaited example
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 |
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 |
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 |
handler.setLevel(Level.INFO)
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"); } }
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
This was extremely helpful, much more so than other blogs that tried to explain the topic. Thank you!
How do you split log files? I have the problem that they grow a lot. Thank you! BTW: Great example!
Good job! ${java.home}/lib/logging.properties is read when using Logger.getLogger(cname), I might add.
What if I want to log at different classes? Do I need to define one logger for each class?
asasadadasadasadasdasda
asdsadasdasdasd
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
Great
y is message 3 not coming out? thanks