Logging in Java
Why we need logging in Java ?
Java Logging or Logging in Java is almost an art of science. At the same time what to log in java, which login level to use, the format of messages etc. also comes along with the practice.
It is a well known fact in industry that java logging severely affects the performance of systems. Thus it becomes very necessary to understand and learn java logging in detail and best practices on logging in java.
System.out.println in Java for printing messages is not powerful as the advanced Java logging API s. In case of writing a Java application server, to know what the server is doing or when a server error occurs, java logs would be very much useful. This necessity cannot be full filled by System.out.println. (Though we can redirect the standard output to a file, it is very inflexible. At the same time it prints out everything and we cannot filter what goes to the output)
What is Log4J ?
- It's a java logging utility. In brief it is a library that specializes in logging.
- Log4J is written in Java and distributed under the Apache Software License.
- It gives levels of priorities to the logging process and logging information can be directed to variety of destinations like databases, files, console, UNIX syslog etc.
- Log4j is highly configurable through external configuration files at the runtime
Log4j Architecture
Log4j API follows a layered architecture. Each layer performs a different task. And different objects are designated to perform these tasksGetting a logger object........
- is a class in org.apache.log4j.*
- one logger object per one java class
- call a static factory method to create a logger object
static Logger log = Logger.getLogger(YourClassName.class.getName())
- logger methods are used to generate log statements
- logger methods are assigned with a level of priority
Getting an layout object........
- is a class in org.apache.log4j.Layout which is the top level class(abstract) in the hierarchy
- we work with its subclasses which are as follows
line 13 => layout object created
Getting a appender object........
- takes the log statements created by the logger object and stores in some database
- is an interface
line 18 => create Appender object
#first parameter -> layout object
#second parameter -> file name
Configuration in Log4j 2
The latest version of Log4j being Log4j 2.5, has made the logging process more efficient, less coding and easy. Yet the basics explained above applies.Log4j 2 mainly focuses on configuration. Once the configuration is done successfully, its just a matter of initializing the logger and calling the required logger method as shown below.
In the above class Bar the logging is done by following steps, after proper configuration
- importing log4j related classes
- define the static logger variable
- call the logger methods like entry, error
4 Ways of accomplishing Log4j Configuration
1) Through a configuration file written in XML, JSON, YAML, or properties format.
2) Programmatically, by creating a ConfigurationFactory and Configuration implementation.
3) Programmatically, by calling methods on the internal Logger class.
4) Programmatically, by calling the APIs exposed in the Configuration interface to add
components to the default configuration.
Furthermore it should be noted that log4j provides default configuration in cases where a configuration file is not found. In the default configuration, following are included
- A ConsoleAppender attached to the root logger.
- A PatternLayout set to the pattern "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
- Level.ERROR is assigned to the root Logger
example of a configuration file
- two main tags. <Appenders></Appenders>and <Loggers> </Loggers>
- define the appender type as console, file etc. inside the Appenders tag
- pattern for the log message layout can be specified under the PatternLayout tag as shown above
- define the logger level inside Loggers tag for each appender specified in Appenders tag
-if we want to specify different logger levels to different classes , it can be done by adding a new logger definition
-it can be done as below by mentioning the class name which requires a different logger level than the others and its required logger level
No comments:
Post a Comment