Tag:log4j随笔
Article From:https://www.cnblogs.com/Danial7777777/p/9136883.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration>

    <!-- Output the log information to the console -->< appender name= "ConsoleAppender"class="org.apache.log4j.ConsoleAppender">
        <!-- Set the style --> for the log output;< layoutclass="org.apache.log4j.PatternLayout">
            <!-- The format --> of the log output is set;< param name= "ConversionPattern" value= "[%d{yyyy-MM-dd HH:mm:ss:SSS}]"[%-5p] [method:%l]%n%m%n%n "/>< /layout><! - - the filter sets the output level -->≪ filterclass="org.apache.log4j.varia.LevelRangeFilter">
            <!-- Set the minimum level of --> for log output;< param name= "levelMin" value= "WARN" /><! -- set the logThe maximum level --> of the output;< param name= "levelMax" value= "ERROR" /><! -- set the log outputXXX, the default is false -->< param name= "AcceptOnMatch" value= "true" />< /filteR>< /appender><!! -- log information output to the file, but when the size of the file reaches a certain threshold, the log file will automatically roll back -->.< appenDer name= "RollingFileAppender"class="org.apache.log4j.RollingFileAppender">
        <!-- Set the full path name --> of the log information output file;< param name= "File" value= "log4j/RollingFileAppender.log" /><!! - set up a new log --> on the basis of the original log when restarting the service.< param name= "Append" value= "true" /><!! - set the maximum number of --> to save backup rollback logs.< param name= "MaxBackupIndex" value= "10" /><!!! - when the log file reaches this threshold, it automatically rolls back. The unit can be KB, MB, GB, and the default unit is KB -->.< param name= "MaxFileSize" value= "10KB" /><! - the style --> that sets the log output;< layoutclass="org.apache.log4j.PatternLayout">
            <!-- The format --> of the log output is set;< param name= "ConversionPattern" value= "[%d{yyyy-MM-dd HH:mm:ss:SSS}]"[%-5p] [method:%l]%n%m%n%n "/>< /layout>< /appender><! - lose the log informationHow long can it be configured to generate a new log file -->< appender name= "DailyRollingFileAppender"class="org.apache.log4j.DailyRollingFileAppender">
        <!-- Set the full path name --> of the log information output file;< param name= "File" value= "log4j/DailyRollingFileAppender.log" /><!! - set the log to roll back once a minute, that is to generate a new log file -->.< param name= "DatePattern" value= "'.'yyyy-MM-dd-HH-mm'.log'"/><! - the style --> that sets the log output;< layoutclass="org.apache.log4j.PatternLayout">
            <!-- The format --> of the log output is set;< param name= "ConversionPattern" value= "[%d{yyyy-MM-dd HH:mm:ss:SSS}]"[%-5p] [method:%l]%n%m%n%n "/>< /layout>< /appender><! --
     Be careful:1:When additivity= "false"The configuration in root fails, and does not follow the default inheritance mechanism.2:loggerThe name in the system is very important. It represents the form of the packet in the recorder, and has a certain inclusion relation.2-1:When the name name of the logger is defined, only the last one can print the log correctly.2-2:When the corresponding logger contains inclusion relations, such as name=test.log4j.test8 and name=test.log4j.test8.UseLog4j, 2-1The situation is the same2-3:loggerThe name indicates that all the registers that contain this name follow the same configuration. The inclusion relation in the value of name is the name of the recorder. Pay attention!3:loggerInterval intersection of level defined in level and appender defined in filter4:If the filter in appender defines levelMin > levelMax ,No log information is printed-->

    <!-- Specify the settings of logger, additivity indicates whether to follow the default inheritance mechanism -->< logger name= "xml.UseLog4j" additivity= "false" >< level value = "WARN" />< appender-ref ref= "DailyRollingFileAppender" />< /logger><!! - specifies the setting of logger, and additivity indicates whether to follow the default inheritance mechanism -->< logger name= "xml.UsELog4j_ "additivity=" false ">< level value = "ERROR" />< appender-ref ref= "RollingFileAppender "/>< /logger><! - the setting of the root logger is -->< root>< level value = "INFO" />< appender-ref ref= "ConsoleAppender" /><! --< appender-ref ref= "DailyRollingFileAppender" /> -->< /root>< /log4j:configuration≫
public class UseLog4j {
    //logger
    private static Logger LOGGER = LogManager.getLogger(UseLog4j.class);
    //Program entrance: main function
    public static void main(String[]args) {
        //Read configuration files written using Java's feature files.
        DOMConfigurator.configure( "log4j.xml" );
        //Output log information, function of test log level (configured in configuration file), output only log information greater than or equal to the current valid level.
        LOGGER.debug("[1]-my level is DEBUG "Heaven");
        LOGGER.info("[2]-my level is INFO");
        LOGGER.warn("[3]-my level is WARN");
        LOGGER.error("[4]-my level is ERROR");
    }
}
public class UseLog4j_ {
    //logger
    private static Logger LOGGER = LogManager.getLogger(UseLog4j_.class);
    //Program entrance: main function
    public static void main(String[]args) {
        //Read configuration files written using Java's feature files.
        DOMConfigurator.configure( "log4j.xml" );
        //Output log information, function of test log level (configured in configuration file), output only log information greater than or equal to the current valid level.
        LOGGER.debug("[1]-my level is DEBUG "Heaven");
        LOGGER.info("[2]-my level is INFO");
        LOGGER.warn("[3]-my level is WARN");
        LOGGER.error("[4]-my level is ERROR");
    }
}

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *