dsa

Java Logging

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.

Components of Logging

The figure given below represents the flow chart of logging components.

Java Logging
Java Logging

  1. Logger

    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
    SEVEREserious failure
    WARNINGwarning message, a potential problem
    INFOgeneral runtime information
    CONFIGconfiguration information
    FINEgeneral developer information (tracing messages)
    FINERdetailed developer information (tracing messages)
    FINESThighly detailed developer information(tracing messages)
    OFFturn off logging for all levels (capture nothing)
    ALLturn 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
    
  2. Filters

    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();
    
  3. Handlers

    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 :

    HandlersUses
    StreamHandlerwrites to an OutputStream
    ConsoleHandlerwrites to console
    FileHandlerwrites to file
    SocketHandlerwrites to remote TCP ports
    MemoryHandlerwrites to memory
  4. Formatters

    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 :

    FormattersUses
    SimpleFormatterformats LogRecord to string
    XMLFormatterformats LogRecord to XML form