---
title: "Log4j"
author: "RobertLeeJesus"
date: "2009-02-24T00:16:44Z"
tags: []
slug: "log4j"
draft: false
lastmod: "2009-02-24T00:16:44Z"
id: 337642
url: "https://hidefromall.iteye.com/blog/337642"
---

1\. Default property file.

 

CLASSPATH/log4j.configuration

 

2\. Overrided property file.

 

log4j.configuration=log4j.configuration

 

3\. Default initialization algorithm

1. Setting the **log4j.defaultInitOverride** system property to any other value then "false" will cause log4j to skip the default initialization procedure (this procedure).
2. Set the `resource` string variable to the value of the **log4j.configuration** system property. *The preferred way to specify the default initialization file is through the **log4j.configuration** system property.* In case the system property **log4j.configuration** is not defined, then set the string variable `resource` to its default value "log4j.properties".
3. Attempt to convert the `resource` variable to a URL.
4. If the resource variable cannot be converted to a URL, for example due to a `MalformedURLException` , then search for the `resource` from the classpath by calling `org.apache.log4j.helpers.Loader.getResource(resource, Logger.class)` which returns a URL. Note that the string "log4j.properties" constitutes a malformed URL. See [Loader.getResource(java.lang.String)](/admin/blogs/apidocs/org/apache/log4j/helpers/Loader.html#getResource%28java.lang.String%29) for the list of searched locations.
5. If no URL could not be found, abort default initialization. Otherwise, configure log4j from the URL. The [PropertyConfigurator](/admin/blogs/apidocs/org/apache/log4j/PropertyConfigurator.html) will be used to parse the URL to configure log4j unless the URL ends with the ".xml" extension, in which case the [DOMConfigurator](/admin/blogs/apidocs/org/apache/log4j/xml/DOMConfigurator.html) will be used. You can optionaly specify a custom configurator. The value of the **log4j.configuratorClass** system property is taken as the fully qualified class name of your custom configurator. The custom configurator you specify *must* implement the [Configurator](/admin/blogs/apidocs/org/apache/log4j/spi/Configurator.html) interface.

4\. Properties file sample

 

log4j.category.org.example.foo.bar=DEBUG

log4j.rootCategory=DEBUG, R, O

\# Stdout  
log4j.appender.O=org.apache.log4j.ConsoleAppender

\# File  
log4j.appender.R=org.apache.log4j.RollingFileAppender  
log4j.appender.R.File=log4j.log

\# Control the maximum log file size  
log4j.appender.R.MaxFileSize=100KB

\# Archive log files (one backup file here)  
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout  
log4j.appender.O.layout=org.apache.log4j.PatternLayout

log4j.appender.R.layout.ConversionPattern=\[%d{ISO8601}]%5p%6.6r\[%t]%x - %C.%M(%F:%L) - %m%n  
log4j.appender.O.layout.ConversionPattern=\[%d{ISO8601}]%5p%6.6r\[%t]%x - %C.%M(%F:%L) - %m%n

 

5\. Help

 

**Apache Log4j配置说明:** http://zhangjunhd.blog.51cto.com/113473/21014

**Apache Log4j使用实例:** http://zhangjunhd.blog.51cto.com/113473/21211