What is Logger?
Logger is kind of custom auditing tool through which you can trace back the execution flow.In application we can use it the way we want as a debugger during development mode and as a info during production mode.
Moving from one level to another does not allow binary changes .It can be controlled by external properties/configuration file.
In order to define logging we need following objects.These are:-
i)Logger is an entity identified by a name and follows the hierarchy
Logger for com.kunaal.X is parent of logger for com.kunaal.sub.Y
Logger can customize the priority level.Since all loggers are child of root logger.Properties defined here over ride the inherited settings of the root logger.
e.g. Lets suppose we have 3 packages in our project
We have defined root logger with priority level as INFO.
We have defined logger for com.kunaal.dao with priority level as DEBUG.
So in our application following logging will happen
-For all classes in com.kunaal.dao. all logging information with priority level as DEBUG and above it will be logged in the appender.
-For all classes in com.kunaal.domain and com.kunaal.service.There is no specific logging criteria defined.So it will inherit the information from root logger.So logging information with priority level as INFO and above will be logged.
ii)Appenders is an object which sends the logging information to the final destination.Log4J has provided many appenders.Some of the frequently used appenders are as follows.
-File Appender lets you log the information in a file.
-Console Appender lets you log the information to the console.By default its System.out.
-JDBC Appender lets you log the information to the database table.
-JMS Appender lets you publish the information to JMS topic
-Rolling File Appender lets you log the information ina file.Once file reaches up the max size.It backs up the file and create a new file.
iii)Layout lets you customize the format in which you log the information.
Root logger is at the top of logger hierarchy and all other loggers are child loggers of the root logger.If we donot specify any custom settings for the child logger,child logger inherit the setting information from the root logger or the logger nearest to it.
Root logger always exits and there is a utility method Logger.getRootLogger() to fetch it.
Inheritance in logger
Log4j incorporates inheritance among loggers.If we have not specified any logger for the particular class,it fetches logger information from immediate parent going upto root logger.
This is illustrated by testInheritance() method in LoggerExample.There we have not defined any logger for the Logger for LoggerExample.So it inherits the information from the immediate parent which is root logger in this case.More information is available in method comments.
Logging output to multiple appenders
Logging lets us write the log information to multiple appenders if reqd.All we need is to define the appender information in the logger.
In the Logger Example mentioned below we have added a file appender.So for logger "com.kunaal.logger.domain" logging information will go to file appender besides the console appender specified in root logger.
What is LogManager and how logger is created
LogManager is the internal class used by log4j for getting loggers and other operations.LogManager has a data structure of type map to persist logger against classname.When we do Logger.getLogger(Class....).It checks in the internal data structure whether any logger for this class is created or not.If its created it returns the same,otherwise creates a new logger and persist it in the internal map. This is illustrated by testLogMgr() in LoggerExample
Different log levels
There are six log levels supported by log4j.These are as follows
If logging level for a logger is defined as INFO,then except debug and trace rest all information will be logged.
Why logging is preferred over SOP?
Reason being its not possible with System.out.println(SOP) to turn it off/on.It can be either totally on or off.Moreover once placed inside the code it will always be run.Its not possible to let few statements run and few does not run.Moreover this setting can't externalized and controlled as we can do with priority level in log4j using configuration /properties file.
With log4j we can customize the layout and information can be sent to different appenders like file appender/jms appender/mail appender /console appender which is not possible with SOP.
In log4j there is a hierarchy among loggers which does not exist in SOP.
Logging examplelog4j.xml-Configuration file for logging
Domain class for EducationalInfo
Domain class for Employee
Domain class for FamilyInfo
Domain class for PersonalInfo
LoggerExample class showing inheritance,multiple appenders,logmanager datastructure and others
When we do LOGGER.debug("......" + "....");,the operation becomes expensive if logger priority level is above debug as the expression will be evaluated and only when logger tries to put the information in the appender.Then it will check and figure out that this information can't be logged.
So its better to write the above statement as follows:-
LOGGER.debug("......" + "....");
In this way we avoid unnecessary creation of log message if its not going to be logged.