log4j Using Tutorials

Source: Internet
Author: User

log4j Basic Use Method

LOG4J consists of three important components: the priority of the log information, the output destination of the log information, and the output format of the log information. The priority of log information is high to low with error, WARN, INFO, DEBUG, respectively, to specify the importance of this log information; the output destination of the log information specifies whether the log will be printed to the console or the file, and the output format controls the display of the log information.

defining a configuration file

Instead of using configuration files at all, you can configure the LOG4J environment in your code. However, using a configuration file will make your application more flexible. LOG4J supports two configuration file formats, one in XML format and one Java attribute file (key = value). Here's how to use the Java attributes file as a configuration file:

    • Configure the root logger, whose syntax is:
    • Log4j.rootlogger = [level], Appendername, Appendername, ...

      Level is the priority of logging, which is divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or levels you define. LOG4J recommends using only four levels, with the priority from high to low being error, WARN, INFO, DEBUG. By defining the level here, you can control the switch to the appropriate level of log information in your application. Appendername refers to where the log information is exported. Multiple output destinations can be specified at the same time.

    • configuration log information output destination Appender with the syntax:
       log4j.appender.appendername =  Fully.qualified.name.of.appender.class Log4j.appender.appenderName.option1 = value1 ... Log4j.appender.appenderName.option = Valuen  

      Where log4j provides the following appender:

       org.apache.log4j.consoleappender (console), Org.apache.log4j.FileAppender (file),  Org.apache.log4j.DailyRollingFileAppender (generates a log file every day),  Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size), Org.apache.log4j.WriterAppender (sends the log information in stream format to any specified place)  
    • The
    • configures the format (layout) of the log information with the following syntax:
       log4j.appender.appendername.layout =  Fully.qualified.name.of.layout.class Log4j.appender.appenderName.layout.option1 = value1 ... Log4j.appender.appenderName.layout.option = Valuen  

      Where the layout provided by LOG4J has the following e:

       org.apache.log4j.htmllayout (layout in an HTML table), Org.apache.log4j.PatternLayout (with the flexibility to specify layout mode), Org.apache.log4j.SimpleLayout (contains the level and information string for log information), Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on) of the log,  
      The

      log4j formats the log information in a print format similar to the printf function in C, with the following printing parameters:%m The message specified in the output code

        
log4j.properties Example:
 # # # #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 

log4j Using Tutorials

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.