Log4j study concluded (1) - Quick Start Tutorial

log4j is an excellent logging component, substantially all of the java development projects will use it. Some of their own learning experience following summary, to facilitate future learning.

log4j will not be alone in the project, or at least I wrote a java project no. Generally log4j and commons-loggings component is used together, in maven dependencies are:

1

Commons-logging and log4j relationship is like jdbc mysql database and as a factory method, is a specific implementation, the benefits of: 1, is to separate the logic code 2. Maintenance reduced. The second point which is very important. Log4j configuration simpler than using separate components.

Let me talk about principles.

After completion of commons-logging project references a jar in the classpath. According to commons-loggings's official statement:

2

Translated into Chinese, I also find in other places, like this:

1) First, to find their own configuration files commons-logging.properties in classpath, if found, the Log implementation class defined therein;

2) If you can not find commons-logging.properties file, then find out whether the defined system environment variables org.apache.commons.logging.Log, then use the Log implementation class find their definition;

3) Otherwise, if there is Log4j package to view the classpath, if found, it will automatically use Log4j as the logging implementation class;

4) Otherwise, use JDK logging implementation class itself (JDK1.4 only after logging implementation class);

5) Otherwise, a simple log using commons-logging to provide their own implementation class SimpleLog

Therefore, in accordance with the above statement, we refer to one in a million in the classpath log4j jar package can automatically use log4j components to enter the log to us. This completes the coupling log4j and commons-logging of the. Does not require any configuration files, only we need two jar package on it.

Then fill in the program after the following sentence:

private static Log log = LogFactory.getLog(YouClassName.class);

You can use the commons-logging to enter log files. You can use the following five levels of output in the program:

log.debug () output "Debug" level log information;

log.info () outputs "information" level log information;

log.warn () output "warning" level log information;

log.error () output "error" level log information;

log.fatal () output "fatal error" level log information;

have to be aware of is:

log4j components must use the log4j.properties configuration file.

log4j assembly has three components: Logger (Loggers), an output terminal (Appenders) and log formatter (Layout). It can be configured through a unified configuration file.

For example, the following configuration file from the Internet excerpt:

log4j.rootLogger=DEBUG, CONSOLE,A1

# It defines the log information of " output level " and " output destination ." "DEBUG, CONSOLE, A1" where we put it into two parts: the first comma before the first part, designated "output level"; the latter is the second part, designated "# to output object." You can specify multiple "output destination", separated by commas. Specific to the line above: it specifies "output level" is "the DEBUG"; it specifies "output destination" is "the CONSOLE" and "A1."

#“输出级别”有可选的五个值,分别是DEBUG、INFO、WARN、ERROR、FATAL,它们是由Log4j系统定义的。

# “输出目的地”就是我们自己定义的了,就在log4j.properties的后面部分,此文件定义的“输出目的地”有CONSOLE、FILE、ROLLING_FILE、SOCKET、LF5_APPENDER、MAIL、DATABA#SE、A1、im。该文件之所以可作主模板,就是因为它比较全面地定义了各种常见的输出目的地(控制台、文件、电子邮件、数据库等)。

#当在程序中使用DEBUG的时候,意思是调试级别以上的日志信息都要输出,包括  log.debug,log.info,log.warn,log.warn,log.error,log.fatal的日志。

#同理,当使用INFO的时候,意思是级别以上的日志信息都要输出,包括 log.info,log.warn,log.warn,log.error,log.fatal的日志。

log4j.addivity.org.apache=true

# 应用于控制台

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.Threshold=DEBUG

log4j.appender.CONSOLE.Target=System.out

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n

#应用于文件

log4j.appender.FILE=org.apache.log4j.FileAppender

log4j.appender.FILE.File=file.log

log4j.appender.FILE.Append=false

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

# Use this layout for LogFactor 5 analysis

# 应用于文件回滚

log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender

log4j.appender.ROLLING_FILE.Threshold=ERROR

log4j.appender.ROLLING_FILE.File=rolling.log

log4j.appender.ROLLING_FILE.Append=true

log4j.appender.ROLLING_FILE.MaxFileSize=10KB

log4j.appender.ROLLING_FILE.MaxBackupIndex=1

log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

#应用于socket

log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender

log4j.appender.SOCKET.RemoteHost=localhost

log4j.appender.SOCKET.Port=5001

log4j.appender.SOCKET.LocationInfo=true

# Set up for Log Facter 5

log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout

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

# Log Factor 5 Appender

log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender

log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000

# 发送日志给邮件

log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender

log4j.appender.MAIL.Threshold=FATAL

log4j.appender.MAIL.BufferSize=10

[email protected]

log4j.appender.MAIL.SMTPHost=www.wusetu.com

log4j.appender.MAIL.Subject=Log4J Message

[email protected]

log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout

log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

# 用于数据库

log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender

log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test

log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver

log4j.appender.DATABASE.user=root

log4j.appender.DATABASE.password=

log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')

log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout

log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender

log4j.appender.A1.File=SampleMessages.log4j

log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j'

log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout

#自定义Appender

log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppende

log4j.appender.im.host = mail.cybercorlin.net

log4j.appender.im.username = username

log4j.appender.im.password = password

log4j.appender.im.recipient = [email protected]

log4j.appender.im.layout=org.apache.log4j.PatternLayout

log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

# 结束

还有一个经常使用到的地方就是:

使用log4j控制程序中的包的输出级别,一开始看这句话可能不太理解,下面我举例来说明。

比如下面的一句话:

log4j.rootLogger=DEBUG,console

log4j.logger.org.springframework=ERROR 

log4j.logger.com=INFO   
log4j.logger.net=INFO

所有以org.springframework打头的包都是ERROR级别和ERROR级别以上的,也就是FATAL才会输出;所有以com或者net打头的包都是INFO级别和INFO级别以上的输出,而其他的则为DEBUG级别及DEBUG级别以上的输出。

Is disposed above the level of the package, you may be provided to the output level: for example, the following

log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d %p [%t] [%c] - %m%n

log4j.appender.stdout.Threshold=DEBUG

Set to the console output level must be above DEBUG, because the output level is set rootLogger INFO level, so this setting is actually of no use.

Here is an example to illustrate the above configuration.

For example, the program needs to be deployed to the client to the server test run,

log4j.rootLogger=DEBUG, stdout, logfile

In the root directory, we choose to have all of the log output, but a lot of things you do not want users to see in the console.

We can set log4j.appender.stdout.Threshold = INFO

So that the file information in there we want to see all the information, and the user can see not so much.

Wait until some time the procedure is relatively stable. Log files do not need that much of the information. It can increase log4j.appender.logfile.Threshold = INFO

Such information in the log will be reduced, or increase the use of a package in the information output levels achieved:

INFO = log4j.logger.com.lxzh

Reproduced in: https: //my.oschina.net/zhanghaiyang/blog/592874

Guess you like

Origin blog.csdn.net/weixin_33816821/article/details/92662247