How to use JAVA log4j

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.