Priority Levels of Logging statements:

To insert logging statements in a java class we should assign some priority for those statements.
Log4j has given priority levels and respective methods but it is a programmer responsibility to apply an appropriate level for a statement.
The priority levels of a log4j are
1) ALL
2) TRACE
3) DEBUG
4) INFO
5) WARN
6) ERROR
7) FATAL
8) OFF
ALL<TRACE<DEBUG<INFO<WARN<ERROR<FATAL<OFF

->The standard priority levels used in a java application are…
     
1)DEBUG:
To indicate a control enter or exit from a method or class or module of a project
2)INFO:
To indicate some information like a server started or a connection is opened or an application is deployed or a connection is closed or server or services are closed
3)WARN:
To indicate some warning messages like a connection between client and server is lost or a socket is reached its limit etc.
4)ERROR:
To indicate some error message .Generally, we use this level in catch block of our code.
5)FATAL:
To indicate some un-recoverable error or problem. Generally, when a fatal error occurs then the application is aborted or terminated.
log4j.properties

log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.file=mylog.txt
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern= %m %n

         log4j.rootLogger = DEBUG,A1 — > Here DEBUG means we are specifying the level from where log4j methods execution must start,  see mylog.txt file it showing all messages right.  But if we wrote log4j.rootLogger = WARN,A1 then it will prints the messages in l.warn(), l.error(), l.fatal() only  printed ,remaining logs ignore like  l.debug(), l.info() because of priority.
         I have used FileAppender as my Appender, so if we want to change my appender to ConsoleAppender, i will open log4j.properties file and do the modifications,  so no need to touch our java classes, this is the main advantage of log4j.properties file.

0 comments:

Post a Comment