Log4j学习

log4j日志级别:

程序会打印高于或等于所设置级别的日志,设置的日志等级越高,打印出来的日志就越少.

log4j日志级别(由低到高)
Level Description
all All levels including custom levels
trace (since log4j 1.2.12) developing only, can be used to follow the program execution
debug developing only, for debugging purpose
info Production optionally, Course grained (rarely written informations), I use it to print that a configuration is initialized, a long running import job is starting and ending.
warn Production, simple application error or unexpected behaviour. Application can continue. I warn for example in case of bad login attemps, unexpected data during import jobs
error Production, application error/exception but application can continue. Part of the application is probably not working.
fatal Production, fatal application error/exception, application cannot continue, for example database is down.
no Do not log at all.

Layout of the log file

The layout specifies how a log message looks like.

First you define the layout.

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

The pattern layout requires another parameter, i.e. the pattern.

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

The best up-to-date documentation about available layouts can be found in the API
documentation:

Layout

There you can see that we have DateLayout, HTMLLayout, PatternLayout, SimpleLayout,
XMLLayout as options.
SimpleLayout has no properties to be set. It is simple.
We used PatternLayout in our example and we set a property named ConversionPattern. This
property allows us to define the log output.

表达式 含义
%d{ABSOLUTE} Date in Format Absolute
%5p %5 defines a right justified print with 5 characters, p prints the priority of the log message
%c{1}:%L - %m%n And the other settings. Very simple. They are all explained in the API.

The options to influence the layout are explained perfectly in the API documentation:

PatternLayout

Custom Layout

If the configuration options does not suite your needs, you can define custom layouts as well.
Examples for custom layout are provided with the log4j download. Have a look in the examples
directory.

Types of log appender

An appender specifies where your log messages are written to. There is a wide choice of
appenders available. All appenders are direct or indirect subclasses of the AppenderSkeleton.
Therefore we can find all options on the following API page:

Appender

The console and the file appender are a subclass of WriterAppender.
Later on, we are going to choose examples for the following appenders.

Appender 含义
ConsoleAppender Logs to console
FileAppender Logs to a file
SMTPAppender Logs by email
SMTPAppender Logs to a file, starts a new file once the max size is reached. (An alternative is the DailyRollingFileAppender which creates on file per day)

But there are as well:

AsyncAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender,
NullAppender, NullAppender, SMTPAppender, SocketAppender, SocketHubAppender,
SyslogAppender, TelnetAppender, DailyRollingFileAppender, RollingFileAppender.

Custom appenders can be created as well. The log4j download comes with a whole bunch of
samples in the examples directory.

Demo

log4j.rootLogger=INFO,Console,OneFile,HtmlFile

#记录所有的信息
#log4j.logger.org.hibernate=INFO
#记录执行的SQL DML语句
#log4j.logger.org.hibernate.SQL=DEBUG
#记录JDBC参数
#log4j.logger.org.hibernate.type=DEBUG
log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
#log4j.logger.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
#在所有 SQL DDL 语句执行时为它们记录日志
log4j.logger.org.hibernate.tool.hbm2ddl=DEBUG
#log4j.logger.org.hibernate.SQL=DEBUG

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

log4j.appender.OneFile=org.apache.log4j.RollingFileAppender
log4j.appender.OneFile.File=../sypro.log
log4j.appender.OneFile.MaxFileSize=10MB
log4j.appender.OneFile.Threshold=ALL
log4j.appender.OneFile.layout=org.apache.log4j.PatternLayout
log4j.appender.OneFile.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

log4j.appender.HtmlFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HtmlFile.file=../sypro_log
log4j.appender.HtmlFile.DatePattern='_'yyyy-MM-dd'.html'
log4j.appender.HtmlFile.layout=org.apache.log4j.HTMLLayout
log4j.appender.HtmlFile.Threshold=ALL