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 file log4j.rootLogger = DEBUG, CONSOLE, FILE #define your own logger named com.foo #and assign level and appender to your own logger log4j.logger.com.foo=DEBUG,FILE #define the appender named FILE log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=${user.home}/log.out #define the appender named CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.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.
时间: 2024-07-31 21:54:26