首页 > 代码库 > Configuring log4j

Configuring log4j

Configuring log4j typically involes assigning level objects, defining appender objects, and specifying Layout objects. 

  • naming and placing the configuation file

By default, the LogManager class will look for a file named log4j.properties or log4j.xml in the classpath used for loading the log4j classes.

If you decide to name your configuation file differently, you are free to do that. However you have to let the log4j framework know by supplying  a command line argument as follows:

-Dlog4j.cofinguation="file_name".

 

# define the root logger with two appenders writing to console and filelog4j.rootLogger = DEBUG, CONSOLE, FILE#define your own logger named com.foo#and assign level and appender to your own loggerlog4j.logger.com.foo=DEBUG,FILE#define the appender named FILElog4j.appender.FILE=org.apache.log4j.FileAppenderlog4j.appender.FILE.File=${user.home}/log.out#define the appender named CONSOLElog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.conversionPattern=%m%n

notable points about Configuration

  • the configuation file is case-sensive
  • a few of appenders can be only configured through an XMl file
  • some advanced controls, such as Filter and ObjectRender, can be configured only via an XML file.

Configuring log4j