LOG4J Log Configuration Detailed

Source: Internet
Author: User
Tags log log custom name

log4j Introduction log4j has three main components: loggers (Logger), appenders (output source), and layouts (layout). This can be simply understood as the log category, where the logs are to be exported and how the logs are exported. The combined use of these three components makes it easy to record the type and level of information, and to control the style and location of the log output at run time. 1. Loggersloggers components are divided into five levels in this system: DEBUG, INFO, WARN, error, and fatal. These five levels are sequential, DEBUG < INFO < WARN < ERROR < FATAL, respectively, to specify the importance of this log information, it is important to understand this, log4j has a rule: output level is not lower than the set level of log information, Assuming that the loggers level is set to info, the log information for info, WARN, error, and fatal levels is output, and debug with a lower level than info will not be output. 2, appenders Disable and use the log request is only the basic function of log4j, log4j log system also provides many powerful functions, such as allowing the log output to different places, such as consoles, files, etc. New files can be generated depending on the number of days or file size, and can be sent as streams to other places and so on. Commonly used classes are as follows: Org.apache.log4j.ConsoleAppender (console) org.apache.log4j.FileAppender (file) Org.apache.log4j.DailyRollingFileAppender (per Generates a log file) Org.apache.log4j.RollingFileAppender (generates a new file when the file size reaches the specified size) Org.apache.log4j.WriterAppender (sends the log information in stream format to any specified location) Configuration mode: Log4j.appender.appenderName = ClassNamelog4j.appender.appenderName.Option1 = value1 ... Log4j.appender.appenderName.OptionN = valueN3, layouts sometimes users want to format their own log output according to their preferences, Log4j can add layouts to the appenders to complete this function. Layouts provides four kinds of log output styles, such as HTML style, free-to-specify style, packageStyles that contain log levels and information, and styles that include information such as log time, threads, categories, and so on. Commonly used classes are as follows: Org.apache.log4j.HTMLLayout (layout in HTML table Form) Org.apache.log4j.PatternLayout (with flexibility to specify layout mode) Org.apache.log4j.SimpleLayout ( The level and information string that contains the log information) Org.apache.log4j.TTCCLayout (contains information such as the time, thread, category, etc.) of the log configuration mode: Log4j.appender.appenderName.layout = ClassNamelog4j.appender.appenderName.layout.Option1 = Value1...log4j.appender.appendername.layout.optionn = Valuen   Second, configuration detailed in the actual application, in order to make the log4j in the system must be pre-set configuration file. The configuration file is, in fact, the logger, Appender, and layout are set accordingly. LOG4J supports two configuration file formats, one in XML format and the Properties property file. The following is an example of the configuration of the log4j.properties with the Properties property file. 1. Configure root Logger:log4j.rootLogger = [level], appenderName1, appenderName2, ... log4j.additivity.org.apache= False: Indicates that logger does not output in the appender of the parent logger, which is true by default. Level: Sets the lowest levels of logging, the values that can be set are off, FATAL, ERROR, WARN, INFO, DEBUG, all, or custom levels, and LOG4J recommends using only the middle four levels. By setting the level here, you can control the switch of the appropriate level of log information in the application, such as setting the info level here, and the log information for all debug levels in the application will not be printed. Appendername: Specifies where the log information is to be exported. You can specify multiple output destinations at the same time, separated by commas. Example: LOG4J.ROOTLOGGER=INFO,A1,B2,C32, configuration log information output destination (appender): Log4j.appender.appenderName = ClassnameappendernaMe: Custom Appdername, used in Log4j.rootlogger settings; ClassName: Can be set to the following values: (1) Org.apache.log4j.ConsoleAppender (console) (2) Org.apache.log4j.FileAppender (file) (3) Org.apache.log4j.DailyRollingFileAppender (generate one log file per day) (4) Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size) (5) Org.apache.log4j.WriterAppender (send log information in stream format to any specified place) (1) consoleappender option: Threshold=warn: Specifies the minimum output level for log information, which is debug by default. Immediateflush=true: Indicates that all messages will be output immediately, set to False to not output, and the default value is true. Target=system.err: The default value is System.out. (2) Fileappender option: Threshold=warn: Specifies the minimum output level for log information, which is debug by default. Immediateflush=true: Indicates that all messages will be output immediately, set to False to not output, and the default value is true. Append=false:true indicates that the message was added to the specified file, false overwrites the message with the specified file content, and the default value is true. FILE=D:/LOGS/LOGGING.LOG4J: Specifies that the message is output to the logging.log4j file. (3) Dailyrollingfileappender option: Threshold=warn: Specifies the minimum output level for log information, which is debug by default. Immediateflush=true: Indicates that all messages will be output immediately, set to False to not output, and the default value is true. Append=false:true indicates that the message was added to the specified file, false overwrites the message with the specified file content, and the default value is true. FILE=D:/LOGS/LOGGING.LOG4J: Specifies that the current message is output to the logging.log4j file. Datepattern= '. ' YYYY-MM: Scrolls the log file once a month, resulting in a new log file per month. The current month's log file is named Logging.log4j, and the first one months of the log file is named Logging.log4j.yyyy-mm. In addition, you can specify by week, day, time, minute, etc. to scroll the log file, corresponding format is as follows: 1) '. ' YYYY-MM: 2 per month) '. ' YYYY-WW: 3 per week) '. ' YYYY-MM-DD: 4 per day) '. ' Yyyy-mm-dd-a: Two times a day 5) '. ' YYYY-MM-DD-HH: 6 per hour) '. ' Yyyy-mm-dd-hh-mm: Per minute (4) rollingfileappender option: Threshold=warn: Specifies the minimum output level for log information, which is debug by default. Immediateflush=true: Indicates that all messages will be output immediately, set to False to not output, and the default value is true. Append=false:true indicates that the message was added to the specified file, false overwrites the message with the specified file content, and the default value is true. FILE=D:/LOGS/LOGGING.LOG4J: Specifies that the message is output to the logging.log4j file. MAXFILESIZE=100KB: The suffix can be kb, MB, or GB. When the log file reaches this size, it will automatically scroll to move the original content to the logging.log4j.1 file. maxbackupindex=2: Specifies the maximum number of scrolling files that can be generated, for example, set to 2 to produce logging.log4j.1,logging.log4j.2 two scrolling files and a logging.log4j file. 3. Configuration log information output format (Layout): Log4j.appender.appendername.layout=classnameclassname: Can be set to the following values: (1) Org.apache.log4j.HTMLLayout (layout as HTML table) (2) Org.apache.log4j.PatternLayout (flexibility to specify layout mode) (3) Org.apache.log4j.SimpleLayout (level and information string containing log information) (4) Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log (1) Htmllayout option: locationinfo=true: Output Java file name and line number, default value is False. Title=my Logging: The default value is log4j Log Messages. (2) Patternlayout options: conversionpattern=%m%n: Sets the format in which messages are displayed. Formatting symbolsDescription:%p: The priority of the output log information, that is, debug,info,warn,error,fatal. %d: the date or time of the output log time, the default format is ISO8601, or the format can be specified later, such as:%d{yyyy/mm/dd hh:mm:ss,sss}. %r: The number of milliseconds to output the log information from the application startup to output. %t: Outputs the name of the thread that generated the log event. %l: The location of the output log event, equivalent to the%c.%m (%f:%l) combination, including the class name, method, file name, and number of rows in the code. For example: Test. Testlog4j.main (testlog4j.java:10). %c: The output log information belongs to the class, which is usually the full name of the class. %M: Outputs the method name that produces the log information. %F: The name of the file where the output log message was generated. %l:: The line number in the output code. %m:: The specific log information specified in the output code. %n: Outputs a carriage return newline character, the Windows platform is "RN", and the UNIX platform is "n". %x: The NDC (nested diagnostic environment) associated with the output and current line threads, especially for multi-client multithreaded applications such as Java Servlets. Percent: Outputs a "%" character. In addition, you can also add modifiers between% and format characters to control their minimum length, maximum length, and text alignment. such as: 1) C: Specify the name of the output category, the minimum length is 20, if the category name length is less than 20, the default is the right alignment. 2)%-20c: "-" indicates left alignment. 3)%.30c: Specify the name of the output category, the maximum length is 30, if the category name length is greater than 30, will be the left more than the character of the cut off, but less than 30 will not fill the space.  : log4j More comprehensive configuration log4j configuration file for output to the console, files, rollback files, send log messages, output to the database log table, custom tags and other full set of features. log4j.rootlogger=debug,console,dailyfile,imlog4j.additivity.org.apache=true# Control Console (console) log4j.appender.console =org.apache.log4j.consoleappenderlog4j.appender.console.threshold=debuglog4j.appender.console.immediateflush= truelog4j.appender.console.target=system.errlog4j.appender.console.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=[%-5P]%d (%r)--[%t]%l:%m%x%n# Log file (LogFile) log4j.appender.logfile=org.apache.log4j.fileappenderlog4j.appender.logfile.threshold= Debuglog4j.appender.logfile.immediateflush=truelog4j.appender.logfile.append=truelog4j.appender.logfile.file=d :/logs/log.log4jlog4j.appender.logfile.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.LOGFILE.LAYOUT.CONVERSIONPATTERN=[%-5P]%d (%r)--[%t]%l:%m%x%n# Rollback file (rollingfile) log4j.appender.rollingfile= Org.apache.log4j.rollingfileappenderlog4j.appender.rollingfile.threshold= Debuglog4j.appender.rollingfile.immediateflush=truelog4j.appender.rollingfile.append= Truelog4j.appender.rollingfile.file=d:/logs/log.log4jlog4j.appender.rollingfile.maxfilesize= 200kblog4j.appender.rollingfile.maxbackupindex=50log4j.appender.rollingfile.layout= org.apache.log4j.patternlayoutlog4j.appender.rollingfile.layout.conversionpattern=[%-5p]%d (%R)--[%t]%l:%m%x%n# periodic rollback of log files (dailyfile) log4j.appender.dailyfile= Org.apache.log4j.dailyrollingfileappenderlog4j.appender.dailyfile.threshold= Debuglog4j.appender.dailyfile.immediateflush=truelog4j.appender.dailyfile.append= Truelog4j.appender.dailyfile.file=d:/logs/log.log4jlog4j.appender.dailyfile.datepattern= '. ' yyyy-mm-ddlog4j.appender.dailyfile.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.DAILYFILE.LAYOUT.CONVERSIONPATTERN=[%-5P]%d (%r)--[%t]%l:%m%x% n# applied to socketlog4j.appender.socket=org.apache.log4j.rollingfileappenderlog4j.appender.socket.remotehost= localhostlog4j.appender.socket.port=5001log4j.appender.socket.locationinfo=true# Set up for Log Factor 5log4j.appender.socket.layout=org.apache.log4j.patternlayoutlog4j.appender.socket.layout.conversionpattern=[%- 5p]%d (%r)--[%t]%l:%m%x%n# Log Factor 5 Appenderlog4j.appender.lf5_appender=org.apache.log4j.lf5.lf5appenderlog 4j.appender.lf5_appender. Maxnumberofrecords=2000# sends the log to the specified message log4j.appender.mail=org.Apache.log4j.net.smtpappenderlog4j.appender.mail.threshold=fatallog4j.appender.mail.buffersize= 10log4j.appender.mail.from = [email protected]log4j.appender.mail.smtphost= Mail.comlog4j.appender.mail.subject=log4j messagelog4j.appender.mail.to= [email protected] LOG4J.APPENDER.MAIL.LAYOUT=ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.MAIL.LAYOUT.CONVERSIONPATTERN=[%-5P]%d (%r)--[%t]%l:%m%x%n# apply to Database log4j.appender.database= org.apache.log4j.jdbc.jdbcappenderlog4j.appender.database.url=jdbc:mysql://localhost:3306/ Testlog4j.appender.database.driver=com.mysql.jdbc.driverlog4j.appender.database.user= Rootlog4j.appender.database.password=log4j.appender.database.sql=insert into log4j (Message) VALUES (' =[%-5p]%d (%r) --[%t]%l:%m%x%n ') log4j.appender.database.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.DATABASE.LAYOUT.CONVERSIONPATTERN=[%-5P]%d (%r)--[%t]%l:%m%x%n # Custom Appenderlog4j.appender.im = Net.cybercorlin.util.logger.appender.IMAppenderlog4j.appendeR.im.host = Mail.cybercorlin.netlog4j.appender.im.username = Usernamelog4j.appender.im.password = Passwordlog4j.appender.im.recipient = [email protected]log4j.appender.im.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.IM.LAYOUT.CONVERSIONPATTERN=[%-5P]%d (%r)--[%t]%l:%m%x%n  The power of log4j is undeniable, but the actual application inevitably encountered a function need to output a separate log file, how can you separate the required content from the original log to form a separate log file? In fact, as long as the existing log4j based on a little configuration can easily achieve this function.   First look at a common log4j.properties file, which is logging in the console and Myweb.log files: Log4j.rootlogger=debug, stdout, logfile  log4j.category.org.springframework=errorlog4j.category.org.apache=info log4j.appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.layout= org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%d%p [%c]-%m%n  log4j.appender.logfile=org.apache.log4j.rollingfileappenderlog4j.appender.logfile.file=${myweb.root}/web-inf/ log/myweb.loglog4j.appender.logfile.maxfilesize=512kblog4j.appender.logfile.maxbackupindex= 5log4j.appender.logfile.layout=org. apache.log4j.patternlayoutlog4j.appender.logfile.layout.conversionpattern=%d%p [%c]-%m%n If you want to output different files for different classes (  Take Cn.com.Test as an example), the first to be defined in Test.java: private static Log logger = Logfactory.getlog (Test.class); Then add in log4j.properties: log4j.logger.cn.com.test= DEBUG, testlog4j.appender.test= org.apache.log4j.fileappenderlog4j.appender.test.file=${myweb.root}/web-inf/log/ test.loglog4j.appender.test.layout=org.apache.log4j.patternlayoutlog4j.appender.test.layout.conversionpattern= %d%p [%c]-%m%n is the configuration that allows logger in cn.com.Test to use Log4j.appender.test.   But what if you need to output multiple log files in the same class? In fact, the truth is the same, first defined in Test.java: private static log Logger1 = Logfactory.getlog ("MyTest1");p rivate static Log Logger2 =  Logfactory.getlog ("MyTest2"); Then add in log4j.properties: log4j.logger.mytest1= DEBUG, test1log4j.appender.test1= org.apache.log4j.fileappenderlog4j.appender.test1.file=${myweb.root}/web-inf/log/ test1.loglog4j.appender.test1.layout= org.apache.log4j.patternlayoutlog4j.appender.test1.layout.conversionpattern=%d%p [%c]-%m%n log4j.logger.mytest2= DEBUG, test2log4j.appender.test2=org.apache.log4j.fileappenderlog4j.appender.test2.file= ${myweb.root}/web-inf/log/test2.loglog4j.appender.test2.layout=  org.apache.log4j.patternlayoutlog4j.appender.test2.layout.conversionpattern=%d%p [%c]-%m%n That is, when using logger, give it a custom name (such as "MyTest1"), and then make the appropriate configuration in log4j.properties. Don't forget to use different logger for different logs (e.g., to use Logger1.info ("abc") for output to Test1.log).   Another problem is that these custom logs are exported to the logs configured by Log4j.rootlogger at the same time, how can they only be output to their own specified log? Don't worry, there's a switch: Log4j.additivity.myTest1 = False It is used to set the output to the Log4j.rootlogger configured log, false will not output to other places! Note that the "MyTest1" here is the custom name you gave logger in the program! If you say, I just do not want to output this log at the same time Log4j.rootlogger configured logfile, stdout I also want to output it at the same time! That's good, put your log4j.logger.myTest1 = DEBUG, test1 change to the next type on OK! Log4j.logger.mytest1=debug, Test1

Here is the log log of the file type when the file is uploaded and the configuration to the specified file

Log4j.rootlogger=info, stdout######################### logger ############################# #log4j. appender.stdout = Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout = Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.conversionPattern =%d [%t]%-5p%c-%m% Nlog4j.logger.extprofile=info, extprofile# log level is INFO, tag is extprofilelog4j.additivity.extprofile=false;# Output to the specified file ExtProfile.log #userprofile log\uff08\u8bb0\u5f55\u4fee\u6539\u5bc6\u7801\uff0c\u627e\u56de\u5bc6\u7801\ uff0c\u4fee\u6539\u90ae\u7bb1\uff0c\u4fee\u6539\u624b\u673a\u53f7\uff09log4j.appender.extprofile= org.apache.log4j.rollingfileappenderlog4j.appender.extprofile.file=logs/extprofile.log# output to the logs folder of the resin root directory, LOG4J will automatically generate directories and files log4j.appender.extprofile.maxfilesize=20480kb# Re-create a file over 20M log4j.appender.extprofile.maxbackupindex=10log4j.appender.extprofile.layout= org.apache.log4j.patternlayoutlog4j.appender.extprofile.layout.conversionpattern=%d [%t]%-5p%c-%m%njava End control Code <% @page contenttype= "text/html" session="False" pageencoding= "UTF-8"%><% @page ... org.apache.commons.logging.Log, Org.apache.commons.logging.LogFactory "%&GT; Log extprofile  = logfactory.getlog ("Extprofile"), ... if (!item.isformfield ()) {       string Fileext = Stringutils.substringafterlast (Item.getname (), "."). toLowerCase ();       extprofile.info ("Upfile type is: [" +fileext + "]");} Reference: http://www.cnblogs.com/ITEagle/archive/2010/04/23/1718365.htmlhttp://blog.csdn.net/anlina_1984/article/ details/5313023

LOG4J Log Configuration Detailed

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.