1, it is best to define the log variable with static final
Private static final Log log = Logfactory.getlog (Mytest.class);
There are three advantages to doing so:
- can guarantee thread safety
- Static or non-static code is available
- Does not affect serialization of objects
2, the output of the log log in the Code "first project engineering, to have the relevant jar"
1) The contents of the Log4g.properties configuration file "need to configure some log output path, and canonical output file name format"
Log4j.rootlogger = debug,stdout,d,e### output information to control lift # # #log4j. appender.stdout = Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = [%-5p]%d{yyyy-mm-dd Hh:mm:ss,sss} method:%l%n%m%n### output debug levels above the log to =e://logs/error.log # # #log4j. APPENDER.D = Org.apache.log4j.dailyrollingfileappenderlog4j.appender.d.file = E://logs/log.loglog4j.appender.d.append = Truelog4j.appender.d.threshold = DEBUG Log4j.appender.d.layout = Org.apache.log4j.patternlayoutlog4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m %n### output error level above the log to =e://logs/error.log # # #log4j. APPENDER.E = Org.apache.log4j.dailyrollingfileappenderlog4j.appender.e.file =e://logs/error.log log4j.appender.E.Append = Truelog4j.appender.e.threshold = ERROR Log4j.appender.e.layout = Org.apache.log4j.patternlayoutlog4j.appender.e.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n
2) Specific implementation class "Log4gtest.java"
Package Com.log4g;import Org.apache.log4j.logger;public class Log4gtest {private static final Logger Logger = Logger.getl Ogger (log4gtest.class); public static void Main (string[] args) { //record debug level information logger.debug ("This is debug Message.debug test"); Record info level information logger.info ("This is info message.info test"); Record the error level information logger.error ("This is the test for error Message.error");} }
The result of the operation is:
Then find the relevant e-disk, you will find the relevant log file exists, that is, success!
Related uses of log