0% found this document useful (0 votes)
30 views2 pages

Log4J StepByStep BestRef

Log4J - Best Document

Uploaded by

Chanchal Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Log4J StepByStep BestRef

Log4J - Best Document

Uploaded by

Chanchal Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Create Maven Project

2. Add the Log4J Dependency

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
3.Create Properties file under resources folder
4. Add the below code in properties file

# 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

1. %d{yyyy-MM-dd HH:mm:ss} = Date and time format, refer to SimpleDateFormat JavaDoc.


2. %-5p = The logging priority, like DEBUG or ERROR. The -5 is optional, for the pretty print format.
3. %c{1} = The logging name we set via getLogger(), refer to log4j PatternLayout guide.
4. %L = The line number from where the logging request.
5. %m%n = The message to log and line break.
Description
Level
ALL All levels including custom levels.
Designates fine-grained informational events that are most useful to
DEBUG
debug an application.
Designates error events that might still allow the application to continue
ERROR
running.
Designates very severe error events that will presumably lead the
FATAL
application to abort.
Designates informational messages that highlight the progress of the
INFO
application at coarse-grained level.
OFF The highest possible rank and is intended to turn off logging.
TRACE Designates finer-grained informational events than the DEBUG.
WARN Designates potentially harmful situations.

public class NewTest {


Logger l=Logger.getLogger(NewTest.class);
@Test
public void f() {
//ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.
l.setLevel(Level.DEBUG);
l.trace("trace message");
l.debug("Debug message");
l.info("Info Message");
l.warn("warn Message");
l.error("Error Message");
l.fatal("Fatal Message");

}
}

You might also like