How to use Logger

A Logger object let you log messages to easily debug your Java code.

There are 9 log levels, but 4 of them are most used:

OFF – turn off logging
INFO – informational messages
WARNING – potential issues
SEVERE – exceptions or any other serious failure

How to use it

1. Choose a dot-separated name for logger.
static Logger logger = Logger.getLogger("eu.turuga.logDemo");

2. Set the file where you want to record logs.

FileHandler fileOutput=new FileHandler("myfile.log");
logger.addHandler(fileOutput);

3. Set output format, by default is XML, but you might consider a more human readable output.
fileOutput.setFormatter(new SimpleFormatter());

4. Set log level, using Level constants
logger.setLevel(Level.INFO);

Leave a Reply

Your email address will not be published. Required fields are marked *