First, you need to import the jar packages used by log4j in your project, and the results are as follows:
Menu: Build path->configure build Path->add Extern Jars
After importing the jar package, create the log4j.properties configuration file under the SRC level, configured as follows:
# # # set # # #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 level above the log file Settings # # #log4j. APPENDER.D=Org.apache.log4j.dailyrollingfileappenderlog4j.appender.d.file=Vincent_player_debug.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 file Settings # # #log4j. APPENDER.E=Org.apache.log4j.dailyrollingfileappenderlog4j.appender.e.file=Vincent_player_error.loglog4j.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
If the log file does not have a path set, the log file is exported to the project directory.
The test code is as follows:
Importorg.apache.log4j.*; Public classVincent_player_framt {Private StaticLogger Logger = Logger.getlogger (Vincent_player_framt.class); Public Static voidMain (string[] args)throwsException {//record the debug level informationLogger.debug ("This is debug message.")); //record information at info levelLogger.info ("This is info message.")); //record the error level informationLogger.error ("This is error message.")); }}
First, create the logger, and then you can call debug, info, error functions to output the information to the log file. The error function information is only output to the error log file.
How to use JAVA log4j