The use of log4j, log4j log for Java (Java log) is the mainstream Java log framework, providing various types, various storage, various formats, and a variety of log services.
Can be downloaded from the Apache official website.
We download log4j,j is the meaning of Java, there are other platforms such as PHP. NET
Select the 1.2.17 version,
Unzip after download
First build the project, build the Log4j.properties file
Log4j.properties
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 #Filelog4j. Appender.file = Org.apache.log4j.FileAppenderlog4j.appender.File.File = D://log4j.loglog4j.appender.file.layout = Org.apache.log4j.PatternLayoutlog4j.appender.File.layout.ConversionPattern =%d [%t]%-5p [%c]-%m%n
Output debug information to console and local files
Specify layout format and output address specific meanings Here you just need to know
The code is as follows:
Import Org.apache.log4j.logger;public class Demo {private static Logger Logger=logger.getlogger (Demo.class); Gets the logger instance public static void main (string[] args) {logger.info ("General info Information"); Logger.debug ("Debugging Debug Information");}}
After running, the console output and the D disk will be a file more
Console content:
2017-10-08 00:42:18,718 [main] info [Com.zhi.log4j.Demo]-General Info Info
2017-10-08 00:42:18,750 [main] debug [Com.zhi.log4j.Demo]-Debugging debug information
d disk under File name:
Log4j.log (which is what we specified in the configuration file)
code example:
Import Org.apache.log4j.logger;public class Demo {private static Logger Logger=logger.getlogger (Demo.class); Gets the logger instance public static void main (string[] args) {logger.info ("General info Information"); Logger.debug ("Debugging Debug Information"); Logger.error ("error message"); Logger.warn ("Warning warn information"); Logger.fatal ("Critical Error fatal Information"); It is possible to encounter a classic exception logger.error ("Wrong", new IllegalArgumentException ("Illegal parameter exception") in development);}}
At this point the console output:
2017-10-08 00:58:07,000 [main] info [Com.zhi.log4j.Demo]-General Info Info
2017-10-08 00:58:07,000 [main] debug [Com.zhi.log4j.Demo]-Debugging debug information
2017-10-08 00:58:07,000 [main] error [Com.zhi.log4j.Demo]-Error message
2017-10-08 00:58:07,000 [main] WARN [Com.zhi.log4j.Demo]-Warning WARN info
2017-10-08 00:58:07,000 [main] FATAL [Com.zhi.log4j.Demo]-Critical Error FATAL info
2017-10-08 00:58:07,000 [main] error [Com.zhi.log4j.Demo]-wrong
Java.lang.IllegalArgumentException: Illegal parameter exception
At Com.zhi.log4j.Demo.main (demo.java:15)
Illegal parameter exceptions can often be met by beginners.
log4j file contents of D drive at the same time
The log information is added again.
log4j Log for Java (Java log) usage