In this tutorial, we will learn about Java Logging and its various components with the help of examples.
Java logging helps us to create and capture log messages and files.
Java has a built-in package for Logging frameworks i.e,java.util.logging
.
The figure given below represents the flow chart of logging components.
Loggers are objects that runs log events. Loggers are created and called in the code of your Java application, where they generate events before passing them to an Appender.The logger class provides methods for logging.Example - Logger getLogger(String)
:This is use to create and fing logger.
Each Logger has a level that determines the importance of the log message. There are 7 basic log levels:
Log Level (in descending order) | Use |
---|---|
SEVERE | serious failure |
WARNING | warning message, a potential problem |
INFO | general runtime information |
CONFIG | configuration information |
FINE | general developer information (tracing messages) |
FINER | detailed developer information (tracing messages) |
FINEST | highly detailed developer information(tracing messages) |
OFF | turn off logging for all levels (capture nothing) |
ALL | turn on logging for all levels (capture everything) |
Each log level has an integer value that determines their severity except for two special log levels OFF and ALL.
// Java program to illustrate logging in Java
// The following code shows a basic example how logging
// works in Java
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.*;
class TestLogger
{
private final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
// Get the Logger from the log manager which corresponds
// to the given name <Logger.GLOBAL_LOGGER_NAME here>
// static so that it is linked to the class and not to
// a particular log instance because Log Manage is universal
public void makeLog()
{
// add some code of your choice here
// Moving to the logging part now
LOGGER.log(Level.INFO, "My first Log Message");
// A log of INFO level with the message "My First Log Message"
}
}
public class Main
{
public static void main(String[] args)
{
TestLogger obj = new TestLogger();
obj.makeLog();
// Generating some log messages through the
// function defined above
LogManager lg = LogManager.getLogManager();
// lgmngr now contains a reference to the log manager.
Logger log = lg.getLogger(Logger.GLOBAL_LOGGER_NAME);
// Getting the global application level logger
// from the Java Log Manager
log.log(Level.INFO, "This is a log message");
// Create a log message to be displayed
// The message has a level of Info
}
}
Output :
Aug 03, 2020 12:57:17 AM TestLogger makeLog
INFO: My first Log Message
Aug 03, 2020 12:57:17 AM Main main
INFO: This is a log message
A filter determines whether the LogRecord should be forwarded or not. As the name suggests, it filters the log messages according to specific criteria.
//How to set filters
logger.setFilter(filter);
//How to get a filter
Filter filter = logger.getFilter();
Handlers are also known as Appenders.Appenders forward logs from Loggers to an output destination. During this process, log messages are formatted using a Layout before being delivered to their final destination.
There are various Handlers.Some of them are :
Handlers | Uses |
---|---|
StreamHandler | writes to an OutputStream |
ConsoleHandler | writes to console |
FileHandler | writes to file |
SocketHandler | writes to remote TCP ports |
MemoryHandler | writes to memory |
A handler uses a Formatter to format the LogRecord object into a string before exporting it to external systems.
There are two built-in Formatters available in Java SE.They are :
Formatters | Uses |
---|---|
SimpleFormatter | formats LogRecord to string |
XMLFormatter | formats LogRecord to XML form |