log4j split by date

Before, I didn't know much about log4j, but my friend asked me how to make the log generated according to the date, and thought about the record.

The log4j.jar package can be downloaded directly from the official website: http://logging.apache.org/log4j/1.2/download.html

The log4j-extr package is mainly to generate a custom file name immediately (the original file suffix name generated by log4j can only be changed the next day)
URL http://www.apache.org/dyn/closer.cgi /logging/log4j/companions/extras/1.1/apache-log4j-extras-1.1.zip

Brief description:

There are 5 important concepts in the Log4j configuration: Logger, Logger, category, category, Appender and Layout. Among them, Logger is responsible for logging; rootLogger is the father of all loggers, any logger can inherit the configuration of rooLogger; category can set all Loggers under the category, similar to the package in java, the effect is equivalent to the Logger name; Appender Where to output; Layout is responsible for output in what format, and what additional information is output (such as: time, class name, method name, number of rows, etc.). In the log4j.properties configuration, Logger is configured behind log4j.logger, Appender is configured after log4j.appender, and rootLogger is directly configured with log4j.rootLogger.

Let's talk about the log level of log4j:
the behavior of the Logger is hierarchical. As shown in the following table:
divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL, or your defined levels, these levels are from high to low level. Log4j recommends using only four levels, with priority from high to low as ERROR, WARN, INFO, DEBUG. Through the level defined here, you can control the switch to the corresponding level of log information in the application. For example, if the INFO level is defined here, all DEBUG level log information in the application will not be printed.
Usage:
1. Introduce jar
2. Web.xml configuration path, if it is in the root directory by default, it is not required, please Ignore this step (not necessary)
3. Write log4j.properties or log4j.xml file

# priority  :debug<info<warn<error
#you cannot specify every priority with different file for log4j 
log4j.rootLogger=debug,stdout,info,debug,warn,error 
 
#console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern= [%d{yyyy-MM-dd HH:mm:ss a}]:%p %l%m%n
#info log
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.info.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.info.File=${catalina.base}/logs/info.log
log4j.appender.info.Append=true
log4j.appender.info.Threshold=INFO
log4j.appender.info.layout=org.apache.log4j.PatternLayout 
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#debug log
log4j.logger.debug=debug
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.debug.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.debug.File=${catalina.base}/logs/debug.log
log4j.appender.debug.Append=true
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.layout=org.apache.log4j.PatternLayout 
log4j.appender.debug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#warn log
log4j.logger.warn=warn
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.warn.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.warn.File=${catalina.base}/logs/warn.log
log4j.appender.warn.Append=true
log4j.appender.warn.Threshold=WARN
log4j.appender.warn.layout=org.apache.log4j.PatternLayout 
log4j.appender.warn.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#error
log4j.logger.error=error
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.error.File = ${catalina.base}/logs/error.log 
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR 
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n

Found a problem : the format with the date will not appear until the next day, and the file for the day does not have the date format (test the manual modification of the machine time)

In addition, shortcomings : log4j.properties is more flawed than log4j.xml, that is, the log levels inside may be output to a file, and high-level log information will also appear in low-level log files, which is a bit confusing.

Stepping on thunder here is actually over.

Added: log4j configuration in web.xml

way1:

<!- webAppRootKey: The default value is webapp.root. When multiple applications are deployed under tomcat (each uses log4j), 
this parameter must be configured in the web.xml of each application. This parameter is the same as Log4j. $ {webapp.root} in the xml file, 
otherwise the webAppRootKey value of each application is the same, it will cause conflicts- 
> 
< context-param > 
< param-name > webAppRootKey </ param-name > 
< param-value > webapp .root </ param-value > 
</ context-param > 

<!- log4jConfigLocation: log4j configuration file storage path- > 
< context-param > 
< param-name > log4jConfigLocation </param-name>
<param-value>/WEB-INF/conf/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

way2 :

public void init() {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4jConfigLocation");
if (file != null) {
PropertyConfigurator.configure(prefix + file); 
}
}

====================================================================

<servlet>
<servlet-name>your servlet</servlet-name>

<servlet-class>your servelt class</servlet-class>
<init-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

 

Guess you like

Origin www.cnblogs.com/kaspar/p/12737845.html