Log4j log study notes for Java crawler technology

1. Introduction to Log4j

        Log4j is an open source project of Apache. By using Log4j, we can control the destination of log information delivery to the console, file, etc.; we can also control the output format of each log; by defining the level of each log information, we can More granular control over the log generation process. These can be flexibly configured through a configuration file without modifying the application code.

1. First create a log.log text file on the D drive

2. Create a new maven project

Introduce jar package into Pom

 <!-- https://mvnrepository.com/artifact/log4j/log4j -->
     <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
     </dependency>

3. Create a new log4j.properties configuration file in Resource

log4j.rootLogger=DEBUG, Console ,File  
   
#Console  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
   
#File
log4j.appender.File = org.apache.log4j.FileAppender
log4j.appender.File.File = D://log.log
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n

4. Write a test class

public class Log4jTest {
    private static Logger logger=Logger.getLogger(Log4jTest.class);  // 获取log4j实例
    public static void main(String[] args) {
        logger.info( "General Info" );
        logger.debug( "Debug debug information" );
        logger.error( "Error message" );
        logger.warn( "Warning warn message" );
        logger.fatal( "Critical error fatal information" );
        
        logger.error( "Error message", new IllegalArgumentException("Illegal parameter" ));
     }
}

  Second, the configuration of Log4j

1. According to the importance of log information, Log4j is divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL

However , if the official recommendation of Log4j is practical, Log4j recommends using only four levels, and the priority from high to low is ERROR, WARN, INFO, DEBUG

2. Log4j root configuration syntax

log4j.rootLogger = [ level ] , appenderName, appenderName, …

Refers to output the log information of the specified level to the specified one or more locations

 example:

log4j.rootLogger=DEBUG, Console ,File

Output debug level and above information to Console and File

According to the log4j level, debug is the lowest level, so the test code and output results are as follows

If root is configured log4j.rootLogger= INFO, Console ,File  

Then the console and files will not output lower levels than Info, so debug will not output 

3.

The official appender of Log4j gives several implementations

 

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)

 

For actual development we use the 1st, 3rd and 4th implementations;

 

If the amount of log data is not large, we can use DailyRollingFileAppender to generate a log every day for easy viewing;

If the amount of log data is large, we generally use RollingFileAppender, a log of fixed size, if it exceeds, a new file will be generated;

MaxFileSize is the maximum size of the log file; 10KB or 100KB can also be determined according to actual needs

MaxBackupIndex is the number of log files. If it exceeds, it will be overwritten. The main consideration is the capacity of the hard disk; it is determined according to actual needs, such as 100 500

 

4、

The layouts provided by Log4j are as follows

 

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

org.apache.log4j.PatternLayout (the layout pattern can be specified flexibly), 

org.apache.log4j.SimpleLayout (contains the level and info string of the log info), 

org.apache.log4j.TTCCLayout (including log generation time, thread, category, etc.)

 

The second is often used. Take the second as an example (the configurations in the previous sections are all PatternLayout)

-5 is for 5 letters, the few are filled with spaces

%p output priority, namely DEBUG, INFO, WARN, ERROR, FATAL;

 %m outputs the message specified in the code;

%n output a carriage return line feed, "rn" on Windows platform , "n" on 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{yyyy-MM-dd HH:mm:ss,SSS}, the output is similar to: 2002-10- 18 22:10:28,921;

%c The category to which the output belongs, usually the full name of the category;

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

 

5. The Log4j Threshold property specifies the output level

Sometimes we need to save some error logs to the specified file separately. At this time, the Threshold attribute comes in handy;

 

Configure DEBUG in rootLogger ,

The Threshold of DFile is configured as DEBUG, and the information entered into the configuration file of DFile is debug and higher than debug level

The Threshold of EFILE is configured as ERROR. Only ERROR information or Fatal is input to the EFILE configuration file.

information;

 

Attached test code

import org.apache.log4j.Logger;

public class Log4jTest {
    private static Logger logger=Logger.getLogger(Log4jTest.class);  // 获取log4j实例
    public static void main(String[] args) {
        logger.info( "General Info" );
        logger.debug( "Debug debug information" );
        logger.error( "Error message" );
        logger.warn( "Warning warn message" );
        logger.fatal( "Critical error fatal information" );
        
        logger.error( "Error message", new IllegalArgumentException("Illegal parameter" ));
}
}

 

 6. The Log4j Append property specifies whether to append content

By default, Log4j continuously appends the log content to the log file;

There is a property Append which is true by default;

If we set it to false, it will not append and directly overwrite the previous content

Run the test class   with attached code

output result

 

Run test class 2   , because it is false, it will overwrite the previous output of test 1

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324776483&siteId=291194637