Why we use Log4j ?
Log4j is used for logging. Logging is the process of writing log message in any file, db,console etc( any central place).
If we use SOP (System.out.print() ) statement to print the log message then we can have some disadvantage :
1. We can print log message on console only. So when console is closed , we will lose all logs.
2.We can’t store log message at any permanent place. These message will print one by one on console because it is single threaded environment.
To overcome these problems Log4j framework came into the picture.Log4j is an open source framework provided by Apache for only java projects.
Log4j Components:
Log4J have three components mainly which are following :
- Logger
- Appender
- Layout
-
Logger
Logger is a class in org.apache.log4j.* package. we have to initialize one Logger object for each java class. We use Logger’s methods to generate log statements. Log4j provide the factory method to get Logger object.
Syntax to get Logger object :
static Logger logger = Logger.getLogger(CurrentClass.class.getName()).
Note: CurrentClass is a java class name for which we are getting logger object.
Example
public class Student{ private static final Logger LOGGER = Logger.getLogger(Student.class); public void getStudentRecord() { } }
Logger class has some method which are used to print the application status.
We have totally five methods in Logger class
- info()
- debug()
- warn()
- fatal()
- error()
It depends on us how to use and when to use these methods. Here methods name are different but working is same of all. All will print a message only.
Levels
Level is a class in org.apache.log4j.* package. We can also make custom level by extending Level class. Each level has a different priority order like this :
debug < info < warn < error < fatal
It means fatal is the highest priority error like database closed.
Appender
Appender is used to write the message into the file or db or smtp.
Log4j have different type of appender:
- SyslogAppendersends
- SMTPAppender
- JDBCAppender
- FileAppender
- SocketHubAppender
- SocketAppender
- TelnetAppender
- ConsoleAppender
Layout
This is used to define the formatting in which logs will print in repository.
We have different types of layout:
- PatternLayout
- SimpleLayout
- XMLLayout
- HTMLLayout
log4j – Configuration
log4j.properties
# Root logger option log4j.rootLogger=INFO, file, stdout # configuration to print into file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=D:\\log\\logging.log log4j.appender.file.MaxFileSize=12MB 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 # configuration to print on 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
Description of log4j.properties file :
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
It will define appender type that means it specify where we want to store application logs . RollingFileAppender will use to print all logs in file and ConsoleAppender will use to print all log in console
log4j.appender.file.File=D:\\log\\logging.log
It specify log file location.
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
It specify the pattern in which log will print into log file
Example:
import org.apache.log4j.Logger; public class Student { static Logger logger = Logger.getLogger(Student.class); public static void main(String[] args) { logger.debug("This is debug message"); logger.info("This is info message"); logger.warn("This is warn message"); logger.fatal("This is fatal message"); logger.error("This is error message"); System.out.println("Logic executed successfully...."); } }
logging.log(log file)
2018-05-02 16:01:45 INFO Student:12 - This is info message 2018-05-02 16:01:45 WARN Student:13 - This is warn message 2018-05-02 16:01:45 FATAL Student:14 - This is fatal message 2018-05-02 16:01:45 ERROR Student:15 - This is error message
It will not print debug level error logs because we defined root logger as INFO in log4j.properties file . That’s why, error message will print which have priority greater than INFO level.
Console logs
16:01:45,511 INFO Student:12 - This is info message 16:01:45,517 WARN Student:13 - This is warn message 16:01:45,517 FATAL Student:14 - This is fatal message 16:01:45,518 ERROR Student:15 - This is error message program executed successfully....