Simple configuration of Log4j

         The company's project needs to use log4j to manage the log, and briefly explain the content. (java environment)

         There are two forms of configuration management in log4j: one is a file in .properties format, and the other is a file in .xml format.

         

		#The level can be set: debug>info>error  
		#debug:显示debug、info、error  
		#info: display info, error  
		#error:只error  
		log4j.rootLogger=debug,appender1  
		#log4j.rootLogger=info,appender1  
		#log4j.rootLogger=error,appender1  
  
		# output to console  
		log4j.appender.appender1=org.apache.log4j.ConsoleAppender  
		#Style is TTCCLayout  
		log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout

 

From the configuration file, we can see that we need to configure three aspects:

1. The root directory (level and destination);

2. Destination (console, file, etc.);

3, the output style.  

Logger - log writer for programmers to output log information 

Appender - log destination, output the formatted log information to the specified place 

ConsoleAppender - Appender destined for console 

FileAppender - Appender destined for files 

RollingFileAppender - Appender destined for size-limited files 

Layout - A log formatter that formats programmers' logging requests into strings 

PatternLayout - Layout that formats logging requests with the specified pattern

 

        Among them, the appenders provided by Log4j are as follows:

  org.apache.log4j.ConsoleAppender (console),

  org.apache.log4j.FileAppender(file),

  org.apache.log4j.DailyRollingFileAppender (generates a log file every day),

  org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size),

      org.apache.log4j.WriterAppender (send log information to any specified place in stream format)

 

 

1. The level of the level (this level can be customized, the system provides the following levels by default):

debug//Debug information

info//General information

warn//Warning message

error//error message

fatal // fatal error message

   Listed above are the so-called output levels of log4j. Log4j recommends using only 4 levels. They are ERROR, WARN, INFO, and DEBUG from top to bottom. If the level you define is info, then the logs of error and warn can be displayed. And the debug information lower than him will not be displayed. 

 

2. log4j.appender.appenderName = fully.qualified.name.of.appender.class. log4j provides the following common output destinations:

   org.apache.log4j.ConsoleAppender, which outputs log information to the console

   org.apache.log4j.FileAppender, which outputs log information to a file

   org.apache.log4j.DailyRollingFileAppender, output log information to one, and output to a new log file every day

   org.apache.log4j.RollingFileAppender, which outputs log information to a file. By specifying the size of the file, when the file size reaches the specified size, the file will be renamed automatically. For example, a file named example.log will be renamed example. log.1, while generating a new example.log file. If the new file reaches the specified size again, it will automatically rename the file to example.log.2 and generate an example.log file. And so on until example.log.MaxBackupIndex, the value of MaxBackupIndex can be defined in the configuration file.

org.apache.log4j.WriterAppender, which sends log information to any specified place in stream format.

org.apache.log4j.jdbc.JDBCAppender, which outputs log information to the database through JDBC.

3. Log4j provides the following layouts:

org.apache.log4j.HTMLLayout, layout in HTML table

org.apache.log4j.PatternLayout, which can flexibly specify layout patterns

org.apache.log4j.SimpleLayout, containing the level and info string of the log information

 

The statement that defines a PatternLayout layout is:

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

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

Format meaning of the ConversionPattern parameter

format name meaning

%c The full name of the class to which the output log information belongs

%d Output the date or time of the log time point, the default format is ISO8601, you can also specify the format after it, for example: %d{yyy-MM-dd HH:mm:ss }, the output is similar to: 2002-10-18- 22:10:28

%f The class name of the class to which the output log information belongs

%l Output the location of the log event, that is, the line where the statement that outputs the log information is in the class where it is located

%m outputs the information specified in the code, such as the message in log(message)

%n outputs a carriage return and line feed, "\r\n" for Windows platforms and "\n" for Unix platforms

%p output priority, ie DEBUG, INFO, WARN, ERROR, FATAL. If it is output by calling debug(), it is DEBUG, and so on 

%r output the number of milliseconds it took to output the log information since the application started

%t output the name of the thread that generated the log event

 
Configuration for log4j xml:
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">  
  
    <!-- ========================= Custom output format description =============== ================== -->  
    <!-- %p output priority, ie DEBUG, INFO, WARN, ERROR, FATAL -->  
    <!-- #%r The number of milliseconds it took to output the log information since the application was started-->  
    <!-- #%c The category to which the output belongs, usually the full name of the category -->  
    <!-- #%t output the name of the thread that generated the log event-->  
    <!-- #%n output a carriage return line feed, "\r\n" for Windows platform, "\n" for Unix platform -->  
    <!-- #%d Output the date or time of the log time point, the default format is ISO8601, you can also specify the format after it, for example: %d{yyy MMM dd HH:mm:ss,SSS}, the output is similar to: 2002 October 18, 22:10:28, 921 -->  
    <!-- #%l Output where the log event occurred, including the category name, the thread where it occurred, and the number of lines in the code. Example: Testlog4.main(TestLog4.java:10) -->  
    <!-- ========================================================================== -->  
      
    <!-- ========================= Output method description ================= ============== -->  
    <!-- The appenders provided by Log4j are as follows: -->  
    <!-- org.apache.log4j.ConsoleAppender(console), -->  
    <!-- org.apache.log4j.FileAppender(file), -->  
    <!-- org.apache.log4j.DailyRollingFileAppender (generates a log file every day), -->  
    <!-- org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size), -->  
    <!-- org.apache.log4j.WriterAppender (send log information to any specified place in stream format) -->  
<!-- ========================================================================== -->  
<!-- output to log file -->  
    <appender name="filelog_appender"  
        class="org.apache.log4j.RollingFileAppender">  
        <!-- Set the File parameter: log output file name -->  
        <param name="File" value="log/testlog4jxml_all.log" />  
        <!-- Set whether to add a new log based on the original log when restarting the service -->  
        <param name="Append" value="true" />  
        <!-- set file size-->  
        <param name="MaxFileSize" value="1MB" />  
        <!-- Settings file backup-->  
        <param name="MaxBackupIndex" value="10000" />  
        <!-- Set output file item and format-->  
        <layout class="org.apache.log4j.PatternLayout">  
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p (%c:%L)- %m%n" />  
        </layout>  
    </appender>  
  
<!-- Output to log file one log per day -->  
    <appender name="filelog_daily" class="org.apache.log4j.DailyRollingFileAppender">     
        <param name="File" value="log/daily.log" />     
        <param name="DatePattern" value="'daily.'yyyy-MM-dd'.log'" />     
        <layout class="org.apache.log4j.PatternLayout">     
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss\} %-5p] [%t] (%c:%L) - %m%n" />     
        </layout>     
    </appender>   
  
<!-- output to console -->  
    <appender name="console" class="org.apache.log4j.ConsoleAppender">  
        <layout class="org.apache.log4j.PatternLayout">  
            <param name="ConversionPattern"  
                value="%d{yyyy-MM-dd HH:mm:ss} %-5p: %m%n" />  
            <!-- "%-5p: [%t] [%c{3}.%M(%L)] | %m%n" -->  
        </layout>  
    </appender>  
  
<appender name="EMAIL_QQ" class="org.apache.log4j.net.SMTPAppender">  
        <param name="Threshold" value="INFO"/>  
        <param name="BufferSize" value="128" />  
        <param name="SMTPHost" value="smtp.qq.com" />  
        <param name="SMTPUsername" value="cjjvictory" />  
        <param name="SMTPPassword" value="***" />  
        <param name="From" value="[email protected]" />  
        <param name="To" value="[email protected]" />  
        <param name="Subject" value="Test mail sending" />  
        <param name="LocationInfo" value="true" />  
        <param name="SMTPDebug" value="true" />  
        <layout class="org.cjj.log4j.extend.PatternLayout_zh">  
            <param name="ConversionPattern" value="[%d{ISO8601}] %-5p %c %m%n"/>  
        </layout>  
    </appender>  
  
<!--- Asynchronous test, execute the wrapped appender when the log reaches the buffer size -->  
    <appender name="ASYNC_test" class="org.apache.log4j.AsyncAppender">     
     <param name="BufferSize" value="10"/>     
     <appender-ref ref="EMAIL_QQ"/>  
   </appender>  
  
 <!-- Set the channel for packet limit output-->  
    <category name="org.cjj" additivity="false">  
<!-- Log output level, at least 5 levels, you can expand your own level, mail sending must be ERROR level is not easy to use, so in the end Extend a mailing level -->  
        <level value="ERROR" />  
        <appender-ref ref="filelog_daily" />  
        <appender-ref ref="daily_appender" />  
        <appender-ref ref="console" />  
        <appender-ref ref="ASYNC_test" />  
 </category>  
  
</log4j:configuration>  
 

 

 

 

Guess you like

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