The log4j of Java EE

Source: Internet
Author: User

1.log4j

A. Concept: A very good open source logging tool

B. Configuration:

①SRC Create Log4j.properties file in the same directory, write:

Log4j.rootlogger=debug,appender1    <!---log4j.appender.appender1= Org.apache.log4j.consoleappenderlog4j.appender.appender1.layout=org.apache.log4j.simplelayout

②loggers

The loggers component is divided into five levels in this system: DEBUG, INFO, WARN, error, and fatal
DEBUG < INFO < WARN < ERROR < FATAL

③appenders

Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day) layout over a period of time
Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size)
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)

④layout

Org.apache.log4j.HTMLLayout (Layout in HTML table Form)
Org.apache.log4j.PatternLayout (flexibility to specify layout mode)
Org.apache.log4j.SimpleLayout (contains level and information strings for log information)
Org.apache.log4j.TTCCLayout (contains information about the thread, category, and so on that the log generates)

C. Use:

         Private StaticLogger Logger = Logger.getlogger (Testlogger.class);  Public Static voidMain (string[] args) {//record the debug level informationLogger.debug ("This is debug message.")); //record information at info levelLogger.info ("This is info message.")); //record the error level informationLogger.error ("This is error message.")); Try{                inti=1/0; }Catch(Exception e) {logger.error (E.getmessage ()); }    }   

D. Examples:

① output error messages to HTML files

Log4j.rootlogger=debug,appender1  log4j.appender.appender1= org.apache.log4j.fileappenderlog4j.appender.appender1.file=c:\\mylog4j.htmllog4j.appender.appender1.layout= Org.apache.log4j.HTMLLayout

② output log information to a file

log4j.appender.appender2=org.apache.log4j.fileappenderlog4j.appender.appender2.file=c:\\ Mylog4j.txtlog4j.appender.appender2.layout=org.apache.log4j.simplelayout

③ Custom Output Styles

Log4j.appender.appender3=org.apache.log4j.consoleappender log4j.appender.appender3.layout= Org.apache.log4j.PatternLayout  log4j.appender.appender3.layout.ConversionPattern =%d{yyyy-mm-dd hh:mm:ss}%r% T%p-%c-%l-%m%n  

%M the message specified in the output code
%p output Priority-DEBUG, INFO, WARN, ERROR, FATAL
%r output the number of milliseconds that the log information is consumed from the application boot to output
%c output belongs to the class, which is usually the full name of the class
%t output The name of the thread that generated the log event
%n output a carriage return newline character, Windows platform is "RN", Unix platform is "n"
%d the date or time of the output log time, the default format is ISO8601, or the format can be specified thereafter, for example:%d{yyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28, 921
%l where the output log event occurs, including the class name, the thread that occurred

④ rolling log files at a certain frequency

Log4j.appender.file=org.apache.log4j.dailyrollingfileappenderlog4j.appender.file.datepattern= '. ' yyyy-mm-dd-hh-mmlog4j.appender.file.file=c:\\run.loglog4j.appender.file.append=truelog4j.appender.file.layout= org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern=%c%x-%m%n        

In Dailyrollingfileappender, you can specify six frequencies for monthly (monthly), weekly (weekly), Daily (daily), half-daily (per half-day), hourly (hourly), and minutely (per minute). This is done by assigning different values to the Datepattern option. The valid values for the Datepattern option are:
‘.‘ YYYY-MM, corresponding monthly (per month)
‘.‘ YYYY-WW, corresponds to weekly (weekly)
‘.‘ YYYY-MM-DD, corresponding to daily (daily)
‘.‘ Yyyy-mm-dd-a, corresponding to half-daily (per half-day)
‘.‘ YYYY-MM-DD-HH, corresponds to hourly (per hour)
‘.‘ YYYY-MM-DD-HH-MM, corresponds to minutely (per minute)

E. Simple solution to Chinese

Log4j.appender.appender3.encoding=gbk

The log4j of Java EE

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.