[Switch] Log4j Loading, Usage and Configuration Instructions

Log4j loading usage and configuration instructions

Main component

1.
The root category (at the top of the category hierarchy, that is, the global log level)

configures the root Logger , its syntax is:

log4j.rootLogger = [ level ] , appenderName, appenderName, ...

level
is the category of logging
appenderName
is to specify where the log information is output. You can specify multiple output destinations at the same time.

Category level is OFF , FATAL , ERROR , WARN , INFO , DEBUG , log , ALL or a custom priority. The common priority of
og4j is
FATAL>ERROR>WARN>INFO>DEBUG
to configure the root Logger , and its syntax is:
log4j.rootLogger = [ level ] , appenderName, appenderName, ...
if it islog4j.rootLogger=WARN , it means that only WARN, ERROR, FATAL are output, DEBUG, INFO will be blocked.

Example: log4j.rootCategory=INFO,stdout,Runlog,Errorlog
The root log category is INFO , DEBUG will be blocked, and others will be output. stdout, Runlog, and Errorlog are three output destinations respectively.

2.
Common output format

- X
number : X information is left aligned when outputting;
%p:
log information level
%d{}:
log information generation time
%c:
log information location (class name)
%m:
generated log specific information
% n:
Output log information wrapping
example:
log4j.appender.stdout.layout.ConversionPattern= %5p %d{yyyy-MM-dd HH:mm:ss} %c %m %n
log4j.appender.Runlog.layout.ConversionPattern= %5p %d{yyyy-MM-dd HH:mm:ss} %c %m %n
log4j.appender.Errorlog.layout.ConversionPattern= %5p %d{yyyy-MM -dd HH:mm:ss} %c %m %n

3
,
the output layout used by the layout, of which log4j provides 4 layouts:
org.apache.log4j.HTMLLayout
(layout in HTML form)
org.apache.log4j.PatternLayout
(The layout mode can be flexibly specified),
org.apache.log4j.SimpleLayout
(including the level and information string of the log information),
org.apache.log4j.TTCCLayout
(including the time, thread, category, etc. of the log generated)

Examples : The
output format is HTML form
log4j.appender.stdout.layout=org.apache.log4j.HTMLLayout

The output format is a layout pattern that can be flexibly specified
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

The output format is the level and information string containing the log information
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

The output format is the time, thread, category and other information that the log is generated
log4j.appender.stdout.layout= org.apache.log4j.TTCCLayout


4.
Destination

configuration log information output destination Appender , its syntax is


log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = value1
.. .
log4j.appender.appenderName.option = valueN
appenderName
is to specify where the log information is output. You can specify multiple output destinations at the same time. Output destinations supported by

log4j :

org.apache.log4j.ConsoleAppender
console
org.apache.log4j.FileAppender
file
org.apache.log4j.DailyRollingFileAppender
produces a log file per day
org.apache.log4j.RollingFileAppender
(generate a new file when the file size reaches the specified size),
org.apache.log4j.WriterAppender
(send log information to any specified place in stream format)
org.apache.log4j.net .SMTPAppender
mail
org.apache.log4j.jdbc.JDBCAppender
database
Other such as: GUI components, even socket server, NT event logger, UNIX Syslog daemon, etc.

Example:


output to console
log4j.appender.CONSOLE=org. apache.log4j.ConsoleAppender
(specify output to console)
log4j.appender.Threshold=DEBUG
(specify output category)
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
(specify output layout)
log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
(specified output format)

output to file

   log4j.appender. FILE=org.apache.log4j.FileAppender
(specify output to file)
   log4j.appender.FILE.File=file.log
(specify output path and file name)
   log4j.appender.FILE.Append=false
   log4j.appender.FILE. layout=org.apache.log4j.PatternLayout
(specifies the layout of the output)
   log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - % m%n
(specified output format)

output to file (rotate " log file ", when the log file reaches the specified size, the file is closed and backed up, and then a new log file is created)

log4j.appender.ROLLING_FILE=org .apache.log4j.RollingFileAppender
(specify output to file)
log4j.appender.ROLLING_FILE.Threshold=ERROR
(Specify output type)
log4j.appender.ROLLING_FILE.File=rolling.log
(Specify output path and file name)
log4j.appender.ROLLING_FILE.Append=true
log4j.appender.ROLLING_FILE.MaxFileSize=10KB
(Specify output to file size )
log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
(specify output layout)
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c - %-4r [%t] %-5p %c %x - %m%n
(specify output format)

output to Socket
log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender
(specify output to Socket )
log4j.appender .SOCKET.RemoteHost=localhost
(remote host)
log4j.appender.SOCKET.Port=5001
(remote host port)
log4j.appender.SOCKET.LocationInfo=true
log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout
(layout)
log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n %p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n %m[MESSAGE]%n%n
(output format)

output to mail
log4j.appender.MAIL= org.apache.log4j.net.SMTPAppender
(specify output to mail)
log4j.appender.MAIL.Threshold=FATAL
log4j.appender.MAIL.BufferSize=10
[email protected]
(sender )
log4j.appender.MAIL.SMTPHost=mail.hollycrm.com
( SMTP server)
log4j.appender.MAIL.Subject=Log4J Message
[email protected]
(recipient)
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
(layout)
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c % x - %m%n
(format)



output to database
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
(specify output to database)
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306 /test
(specify database URL )
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
(specify database driver )
log4j.appender.DATABASE.user=root
(specify database user)
log4j.appender.DATABASE.password=root
(Specify database user password)
log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m% n')
(organizationSQL statement)
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
(layout)
log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
(format)

5.
Log category supplement
Sometimes we need to specify a log category that is different from the root category for a specific part. You can specify the priority of a package
such as:
log4j.category. com.neusoft.mbip.dm.util=ERROR
, where com.neusoft.mbip.dm.util is the part where we need to specify the log category.

Or you can specify the priority of the output file
log4j.appender.Errorlog.Threshold=ERROR


3. Common log4j configuration Commonly used log4j configuration, generally can be used in two ways, .properties and .xml, here are two simple examples: 1. log4j .properties






###
Set the level INFO, DEBUG, WARN, ERROR and output destination A1 , A2 ## log4j.category.org.zblog =ERROR,A1 log4j.category.org.zblog=INFO,A2 log4j. appender.A1=org.apache.log4j.ConsoleAppender ### Set the output destination A1 for ConsoleAppender ( console ) ## log4j.appender.A1.layout=org.apache.log4j.PatternLayout ### Set the output layout of A1 Format PatterLayout, ( the layout mode can be flexibly specified) ## log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m %n ### Configure the format of the log output ##










log4j.appender.A2=org.apache.log4j.RollingFileAppender
###
Set the output destination A2 to the file (a new file will be generated when the file size reaches the specified size) ##
log4j.appender.A2.File=E:/study /log4j/zhuwei.html
###
File Location ##
log4j.appender.A2.MaxFileSize=500KB
###
File Size ##
log4j.appender.A2.MaxBackupIndex=1
log4j.appender.A2.layout=org.apache. log4j.HTMLLayout
##
Specify html output

2
, log4j.xml


<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" >

<appender name="org.zblog.all" class="org.apache.log4j.RollingFileAppender">
<!--
Set channel ID: org.zblog.all and output method: org.apache.log4j.RollingFileAppender -- >
   <param name="File" value="E:/study/log4j/all.output.log" /><!--
Set File parameter: log output file name -->
   <param name="Append" value= "false" /><!--
Set whether to add a new log based on the original log when restarting the service -->
   <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern" value="%p (%c:%L)- %m%n" /><!--
设置输出文件项目和格式 -->
   </layout>
</appender>

<appender name="org.zblog.zcw" class="org.apache.log4j.RollingFileAppender">
   <param name="File" value="E:/study/log4j/zhuwei.output.log" />
   <param name="Append" value="true" />
   <param name="MaxFileSize" value="10240" /> <!--
设置文件大小 -->
   <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern" value="%p (%c:%L)- %m%n" />
   </layout>
</appender>

<logger name="zcw.log"> <!--
设置域名限制,即 zcw.The logs -->Set the level    <level value="debug" /><!-- -->
in the log domain and below are output to the corresponding channel below
   <appender-ref ref="org.zblog.zcw" /><!--
corresponds to the previous channel id -->
</logger>

<root> <!--
sets the channel to receive all output -->
   < appender-ref ref="org.zblog.all" /><!--
corresponds to the previous channel id -->
</root>

</log4j:configuration>


3.
Configuration file loading method:


import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jApp {
   public static void main(String[] args) {
     DOMConfigurator.configure("E:/study/log4j/log4j.xml");//
加载 .xml 文件
     //PropertyConfigurator.configure("E:/study/log4j/log4j.properties");//
加载 .properties 文件

     Logger log=Logger.getLogger("org.zblog.test");
     log.info("
测试 ");
   }
}



4.
The project uses log4j
in the web application, you can load the configuration file in a separate servlet , and configure the servlet in web.xml to load when the application starts.
 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327038287&siteId=291194637