Sunday, February 9, 2014
Refer to this article Using Log4j for debugging in Java for a quick introduction on Log4j. Log4j is a simple, reliable, fast and extensible open source logging and tracing API. Logging is an important part of any software development lifecycle and is the only way to debug in certain production systems where it is not feasible to use debuggers. Here we provide most commonly used properties configuration examples.

Log to console only

# Set root logger
log4j.rootLogger=DEBUG, console

# Add log messages to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n

Log to file only

# Root logger option
log4j.rootLogger=DEBUG, file

# File appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n
log4j.appender.file.File=myoutput.log
log4j.appender.file.MaxFileSize=100KB

Log to both console and file

# Root logger option
log4j.rootLogger=DEBUG, file, console

# Direct log messages to stdout
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n

# File appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n
log4j.appender.file.file=myoutput.log
log4j.appender.file.MaxFileSize=100KB

Log to both console and file and with different log levels

# Root logger option
log4j.rootLogger=TRACE, file, console

# Direct log messages to stdout
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n
log4j.appender.console.Threshold=TRACE

# File appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n
log4j.appender.file.file=myoutput.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.Threshold=DEBUG

Log to console and customize level based on package

# Set root logger
log4j.rootLogger=TRACE, console

# Add log messages to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n
log4j.logger.com.sourcetricks=DEBUG

1 comment :

  1. Nice article!
    Can you help how to filter a string message from log file. E.g. I don't want to print any string which contains [27,01] how to do that?

    ReplyDelete

Contact Form

Name

Email *

Message *

Back to Top