Quantcast
Channel: Thiranjith's Blog
Viewing all articles
Browse latest Browse all 11

Log4j property file examples

$
0
0

There aren’t too many clear examples of log4j.properties configuration examples around on the net. So, I thought of compiling a list of sample configurations to help anyone who might be looking to configure log4j.

In this article I will be covering the following scenarios with examples:

  1. Logging to Console
  2. Logging to a File
  3. Logging to multiple Appenders (or log files)
  4. Restricting logging levels by package name
  5. Having multiple appenders with different log levels
  6. Logging the output of a specific class or a package to a specific Appender

Logging to Console

The example below will send all log messages with a level of INFO or higher to the console (e.g. DOS prompt). The ConversionPattern determines how the output will be displayed on-screen.

#
## Create a single appender called 'stdout' with 
## a log level of 'INFO'
log4j.rootLogger=INFO, stdout

## Log to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p: %c - %m%n

Logging to a File

There are various different File appenders that come out of the box with log4j library. These include FileAppender (for continuously appending to a single file), and more advanced file-appenders such as RollingFileAppender that automatically start a new file after reaching a pre-specified size.

See the API for File Appender for more information about possible configuration options.

#
## Create a single appender called 'R' with 
## a log level of 'INFO'. This will create new
## files whenever the current file reaches 100KB
log4j.rootLogger=INFO, stdout

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=rolling-log.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-5p: %c - %m%n

Logging to multiple Appenders (or log files)

The example below illustrates how we can have multiple appenders simultaneously. In the example below, all logs with a level INFO or higher will be logged to both the console and a file (file-log.log).

log4j.rootLogger=INFO, stdout, F

# Console appender configuration
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p: %c - %m%n

# File appender configuration
log4j.appender.F=org.apache.log4j.FileAppender
log4j.appender.F.File=file-log.log
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=%-5p: %c - %m%n

Restricting logging levels by package name

Oftentimes you have 3rd party libraries that do their own logging, which can quickly fill up your log. In such cases, you want to restrict logging for some (or all) of those libraries so your logs only show what really matters. Log4j provides a mechanism to do this by restricting log levels on a per-package level. This allows us to specify the logging level on the specified package (including the sub-packages within it).

In the following example, the packages org.springframework.core and org.springframework.web will only log ERROR messages, while the rest of the application will log DEBUG or higher messages. For example, only ERROR logs by org.springframework.web.servlet.mvc.AbstractController class will be logged by our application.

log4j.rootLogger=DEBUG, stdout

# Console appender configuration
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p: %c - %m%n

# Restrict logging for some of the Spring MVC libraries
log4j.logger.org.springframework.core=WARN
log4j.logger.org.springframework.web=WARN

How to configure multiple appenders with different log levels

Imagine you want to log INFO and ERROR logs to different files; for example, have file-info.log with all messages of level INFO or higher, and file-error.log to have only ERROR messages.

The log4j.properties file below shows how to accomplish the above task by outputting different levels of logging to different files using the log4j.appender.FError.Threshold attribute.

#
# Initilise the two appenders
# FInfo - Log INFO messages and above
# FError - Log ERROR messages and above
log4j.rootLogger=INFO, FInfo, FError

# FInfo appender will log at default log level (INFO)
log4j.appender.FInfo=org.apache.log4j.FileAppender
log4j.appender.FInfo.File=file-log.log
log4j.appender.FInfo.layout=org.apache.log4j.PatternLayout
log4j.appender.FInfo.layout.ConversionPattern=%-5p: %c - %m%n

# FError appender will log ERROR and above
log4j.appender.FError=org.apache.log4j.FileAppender
# "log4j.appender.FError.Threshold" allows us to customise
# the log level for a specific appender
log4j.appender.FError.Threshold=ERROR
log4j.appender.FError.File=file-log.log
log4j.appender.FError.layout=org.apache.log4j.PatternLayout
log4j.appender.FError.layout.ConversionPattern=%-5p: %c - %m%n

Logging the output of a specific class to a specific Appender

The example below shows how to output the logs from a specific class (PerformanceProfiler class in com.thira.example package) to a dedicated logger/appender.

log4j.rootLogger=DEBUG, stdout

# Console appender configuration
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p: %c - %m%n

# Define another appender for the specified class
log4j.logger.com.thira.example.PerformanceProfiler=INFO, Performance
log4j.additivity.com.thira.example.PerformanceProfiler=false
log4j.appender.Performance=org.apache.log4j.RollingFileAppender
log4j.appender.Performance.File=client-portal-performance.log
log4j.appender.Performance.MaxFileSize=2MB
log4j.appender.Performance.MaxBackupIndex=10
log4j.appender.Performance.layout=org.apache.log4j.PatternLayout
log4j.appender.Performance.layout.ConversionPattern=%-5p: %c - %m%n

The highlighted line #9 (log4j.logger.com.thira.example.PerformanceProfiler=INFO, Performance) tells log4j to redirect all to redirect all INFO (or higher) log messages to the appender called ‘Performance‘.

The additivity option in the next line (i.e. line #10) ensures that the INFO (or higher) log messages from com.thira.example.PerformanceProfiler class will not show up on the rootLogger (i.e. stdout appender in the above example). Note that by default, additivity for all appenders are set to true, implying the log messages will appear on all loggers.

References and Resources

Checkout the following resources if you want to find more about log4j:


Viewing all articles
Browse latest Browse all 11

Trending Articles