Log4j and log (1) Common log4j configuration common log4j configuration, generally two methods can be used ,. properties and. xml. Here are two simple examples: 1. log4j. properties ### set org. level INFO, DEBUG, WARN, ERROR, and output location A1, A2 # log4j.category.org. zblog = ERROR, A1 log4j.category.org. zblog = INFO, A2 log4j. appender. a1 = org. apache. log4j. consoleAppender ### set the output location A1 to leleappender (console) ## log4j. appender. a1.layout = org. apache. log4j. patternLayout ### set the output layout format PatterLayout of A1. (you can flexibly specify the layout mode.) # log4j. Appender. a1.layout. conversionPattern = % d {yyyy-MM-dd HH: mm: ss, SSS} [% c]-[% p] % m % n ### configure the log output format ## log4j. appender. a2 = org. apache. log4j. rollingFileAppender ### set the output location A2 to the file (a new file is generated when the file size reaches the specified size) # log4j. appender. a2.File = E:/study/log4j/zhuwei.html ### File Location ## log4j. appender. a2.MaxFileSize = 500KB ### file size # log4j. appender. a2.MaxBackupIndex = 1 log4j. appender. a2.layout = org. apache. log4j. HTMLLayout # specify html-based Output 2, Log4j. xml <? Xml version = "1.0" encoding = "GB2312"?> <! DOCTYPE log4j: configuration SYSTEM "log4j. dtd "> <log4j: configuration xmlns: log4j =" http://jakarta.apache.org/log4j/ "> <appender name =" org. zblog. all "class =" org. apache. log4j. rollingFileAppender "> <! -- Set the channel ID: org. zblog. all and output mode: org. apache. log4j. rollingFileAppender --> <param name = "File" value = "E:/study/log4j/all. output. log "/> <! -- Set the File parameter: log output File name --> <param name = "Append" value = "false"/> <! -- Set whether to add new logs to the base of the original log when the service is restarted --> <param name = "MaxBackupIndex" value = "10"/> <layout class = "org. apache. log4j. patternLayout "> <param name =" ConversionPattern "value =" % p (% c: % L)-% m % n "/> <! -- Set the output file project and format --> </layout> </appender> <appender name = "org. zblog. zcw "class =" org. apache. log4j. rollingFileAppender "> <param name =" File "value =" E:/study/log4j/zhuwei. output. log "/> <param name =" Append "value =" true "/> <param name =" MaxFileSize "value =" 10240 "/> <! -- Set the file size --> <param name = "MaxBackupIndex" value = "10"/> <layout class = "org. apache. log4j. patternLayout "> <param name =" ConversionPattern "value =" % p (% c: % L) -% m % n "/> </layout> </appender> <logger name =" zcw. log "> <! -- Set the domain name limit, that is, the zcw. log domain and the following logs are output to the corresponding channel below --> <level value = "debug"/> <! -- Set level --> <appender-ref = "org. zblog. zcw"/> <! -- Corresponds to the preceding channel id --> </logger> <root> <! -- Set the channel for receiving all outputs --> <appender-ref = "org. zblog. all"/> <! -- Corresponds to the preceding channel id --> </root> </log4j: configuration> 3. configuration file loading method: import org. apache. log4j. logger; import org. apache. log4j. propertyConfigurator; import org. apache. log4j. xml. DOMConfigurator; public class Log4jApp {public static void main (String [] args) {DOMConfigurator. configure ("E:/study/log4j/log4j. xml "); // load. xml file // PropertyConfigurator. configure ("E:/study/log4j/log4j. properties "); // load. properties file Logger Log = Logger. getLogger ("org. zblog. test "); log.info (" test ") ;}} 4. When using log4j for a project in a web application, you can load the configuration file in a separate servlet and. configure the servlet in xml to load when the application starts. In a multi-person project, you can set an output channel for each person, so that each person can use his/her own domain name when building a Logger, output debugging information to your own log file. 5. Common output formats #-X: Left alignment when X information is output; # % p: log information level # % d {}: log information generation time # % c: log information location (Class Name) # % m: generated log details # % n: Output log information line feed (2) Considerations <! -- This Logger will work for the ibe package and all its sub-packages --> <Logger name = "msg"> <! -- Output debug and more advanced logs --> <level value = "debug"> </level> <appender-ref = "toConsole"> </appender-ref>/ logger> <! -- Specify the Log Level and Appender to be referenced --> <root> <! -- Output debug logs and higher-level logs --> <level value = "info"> </level> <appender-ref = "toFile"> </appender-ref> </ root> <logger name = "msg"> If the set level is higher, all log output levels are used. The level setting in <root> takes effect only when there is no logger Log Level setting. Before using logger, you must read xml static Logger logger = Logger. getLogger (LogQs. class); domaggregator. configure ("log4j. xml "); (3) Example log4j is an open-source project of Apache, we can control the log information delivery destination, including the console, files, GUI components, and even the interface server, NT event recorder, and UNIX Syslog daemon; we can also control the output format of each log. By defining the level of each log information, we can control the log generation process in more detail. The most interesting thing is that these can be configured flexibly through a configuration file, without the need to modify the application code. Use the sample code Log. java [import the jar package of log4j first] import org. apache. log4j. logger; import org. apache. log4j. propertyConfigurator; public class Log {public static void record (Class <?> Clazz, String msg) {PropertyConfigurator. configure ("log4j. properties "); Logger logger = Logger. getLogger (clazz. getName (); // logger can be used for security levels above debug according to the configuration file. debug (msg); // logger. error ("error", new RuntimeException ("runtime error");} public static void record (Class <?> Clazz, String msg, String level) {PropertyConfigurator. configure ("log4j. properties "); Logger logger = Logger. getLogger (clazz. getName (); // according to the configuration file, you can use the security level above debug. // logger. debug (msg); logger. error ("error", new RuntimeException (msg);} public static void record (String ClassName, String msg) {PropertyConfigurator. configure ("log4j. properties "); Logger logger = Logger. getLogger (ClassName); // according to the configuration file, logger can be used for security levels above debug. debug (msg); // logger. error ("error", new RuntimeException ("runtime error");} // use the example public static void main (String [] args) {Log. record ("Log", "Here parameter exception");} configuration file [under the project root directory] log4j. properties # debug here is optional and it shows the rank of the log, # debug or others whose ranks is higher than debug is ambiguous. # ToFile is a target. toConsole, too. log4j. rootLogger = debug, ToFile, ToConsole # toFile log4j. appender. toFile = org. apache. log4j. fileAppender log4j. appender. toFile. file = run. log log4j. appender. toFile. append = true log4j. appender. toFile. layout = org. apache. log4j. patternLayout log4j. appender. toFile. layout. conversionPattern = % d {yyyy-mm-dd hh \: mm \: ss} % c % m % n # toConsole log4j. appender. toConsole = org. apache. log4j. leleappender log4j. appender. toConsole. layout = org. apache. log4j. patternLayout log4j. appender. toConsole. layout. conversionPattern = % c % m % n