Log4j

From JWik
Jump to navigationJump to search

ATTTENTION / Important

Force utf8

Not specifying charset / encoding could generate buggy cyrilic symbols into console!!!

<PatternLayout pattern="..." charset="UTF-8"/>

Sources

A good article for log4j

PatternLayout, for what to see on the log line

Backward compatibility

https://stackoverflow.com/questions/22321109/configuring-log4j2-and-log4j-using-a-single-log4j2-xml-file

http://logging.apache.org/log4j/2.x/faq.html#exclusions


Examples

Common case

log4j.properties

# ***** Set root logger level to DEBUG and its only appender to A.
log4j.rootLogger=debug, CONSOLE

# ***** CONSOLE is set to be a ConsoleAppender.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

# ***** CONSOLE uses PatternLayout.
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN


Multiple outputs - 2018

  1. ***** Set root logger level to DEBUG and its only appender to A.
log4j.rootLogger=debug, CONSOLE, LOGFILE

# ***** CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=debug
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.logger.com.miteff.imcro=debug

# ***** LOGFILE
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.Threshold=debug
log4j.appender.LOGFILE.MaxFileSize=10MB
log4j.appender.LOGFILE.File=output.log
log4j.appender.LOGFILE.MaxBackupIndex=5 
# MaxBackupIndex for auto rotation

# LOGFILE PatternLayout.
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.logger.org.apache.http=error, CONSOLE
log4j.logger.org.apache.http=debug, LOGFILE

log4j.additivity.com.miteff=false
log4j.additivity.org.apache.http=false

Multiple outputs - 2016

# ***** Set root logger level to DEBUG and its only appender to A.
log4j.rootLogger=debug, CONSOLE, LOGFILE

# ***** CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# CONSOLE PatternLayout.
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# ***** LOGFILE
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.Threshold=debug
log4j.appender.LOGFILE.File=output.log

# LOGFILE PatternLayout.
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

Useful Patterns

c / Category / Package and class name

%c

= com.miteff.project.subproject.functionality.ClassName

If you want to see only part of the package, you use this syntax:

%c{3}

= subproject.functionality.ClassName

d = Date

m = Message text

n = New line

the platform dependent line separator

p = Priority

Format pattern

PatternLayout documentation - including date/timestamp

Pattern examples

"%-4r [%t] %-5p %c %x - %m%n" => "169936 [qtp441319716-17] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Released JDBC connection"

"%-4r [%d{HH:mm:ss,SSS}] %-5p %c %x - %m%n" => "5658 [23:40:19,134] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 1684 ms"

"%-4r [%d{mm:ss,SSS}] %-5p %c %x - %m%n" => "5658 [40:19,134] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 1684 ms"

Levels comparation

FINEST  -> TRACE
FINER   -> DEBUG
FINE    -> DEBUG
CONFIG  -> INFO
INFO    -> INFO
WARNING -> WARN
SEVERE  -> ERROR

Code

import org.apache.log4j.Logger;

import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class log4jExample{

  /* Get actual class name to be printed on */
  static Logger log = Logger.getLogger(log4jExample.class.getName());
  
  public static void main(String[] args)throws IOException,SQLException{
     log.debug("Hello this is a debug message");
     log.info("Hello this is an info message");
  }
}