Log4J
· In general we use system.out.println statements in the application.
· Generally SOPL statements are printed on console, so there are temporary messages and whenever the console is closed then automatically the messages are removed from the console
· It is not possible to store the SOPL messages in a permanent place and these are single threaded model, means these will prints only one by one message on the console screen
· In order to overcome the problems of SOPL statements Log4j came into picture, with Log4j we can store the flow details of our Java/J2EE in a file or databases
· Working with log4j is nothing but working with classes & interfaces given in org.apache.log4j.*
Main Components
1. Logger
2. Appender
3. Layout
1. Logger
· Logger is a class, in org.apache.log4j.*
· We need to create Logger object one per java class
· This component enables Log4j in our java class
· We can get the Logger Object by factory method Logger.getLogger(.class)
static Logger logger = Logger.getLogger(LogDemo.class.getName());
· We have totally 5 methods in Logger class
1. logger .debug(“----msg----”)
2. logger .info(“----msg----”)
3. logger .warn(“----msg----”)
4. logger .error(“----msg----”)
5. logger .fetal(“----msg----”)
· Priority Order
o debug < info < warn < error < fatal
2. Appender
· Appender is write the messages into the external file or database or smtp
· Appender is an interface
· In log4j we have different Appender implementation classes
§ FileAppender [ writing into a file ]
§ ConsoleAppender [ Writing into console ]
§ JDBCAppender [ For Databases ]
§ SMTPAppender [ Mails ]
§ SocketAppender [ For remote storage ]
§ SocketHubAppender
§ SyslogAppendersends
§ TelnetAppender
· Again in FileAppender we have 2 more
§ RollingFileAppender
§ DailyRollingFileAppender
3.Layout
· This component specifies the format in which the log statements are written into the destination repository by the appender
· We have different type of layout classes in log4j
§ DateLayout
§ HTMLLayout
§ PatternLayout
§ SimpleLayout
§ XMLLayout
Log4j.properties
For standalone Java app, make sure the log4j.properties file is under the project/classes directory
For Java web applications, make sure the log4j.properties file is under the WEB-INF/classes directory
# Define the root logger with appender X
log4j.rootLogger = DEBUG, X
# Set the appender named X to be a File appender
log4j.appender.X=org.apache.log4j.FileAppender
# Define the layout for X appender
log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.conversionPattern=%m%n
Example
LogDemo.java
package xyz;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class LogDemo {
static Logger l = Logger.getLogger(LogDemo.class.getName());
public static void main(String[] args) {
BasicConfigurator.configure();
l.debug("This is debug message");
l.info("This is info message");
l.warn("This is warn message");
l.fatal("This is fatal message");
l.error("This is error message");
System.out.println("Your logic executed successfully....");
}
}
log4j.properties
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=d\:\\log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Output:
2015-08-10 14:53:48 DEBUG LogDemo:13 - This is debug message
2015-08-10 14:53:48 INFO LogDemo:14 - This is info message
2015-08-10 14:53:48 WARN LogDemo:15 - This is warn message
2015-08-10 14:53:48 FATAL LogDemo:16 - This is fatal message
2015-08-10 14:53:48 ERROR LogDemo:17 - This is error message
2015-08-10 15:22:26 DEBUG LogDemo:13 - This is debug message
2015-08-10 15:22:26 INFO LogDemo:14 - This is info message
2015-08-10 15:22:26 WARN LogDemo:15 - This is warn message
2015-08-10 15:22:26 FATAL LogDemo:16 - This is fatal message
2015-08-10 15:22:26 ERROR LogDemo:17 - This is error message