log4j (ii)--LOG4J Basic use method

Source: Internet
Author: User

LOG4J configuration:

  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 the properties file (key = value).

  Log4j has three main components: the priority of the log information (rootloggers), the output destination of the log information (appenders), the output format of the log information (Layouts).

1, configuration Rootlogger, its 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 custom levels. LOG4J recommends using only four levels, with the priority from high to low being error, WARN, INFO, DEBUG. Log4j has a rule: only the output level is not below the set level of log information, if the loggers level is set to info, then the info, WARN, error and fatal level of log information will be output, and the level is lower than info debug will not output.

Appendername: Specifies where the log information is to be exported. You can specify multiple output destinations at the same time, separated by commas. Example: LOG4J.ROOTLOGGER=INFO,A1,B2,C3

2, the configuration log information output destination Appender, its syntax is:

Log4j.appender.appenderName = Org.apache.log4j.appenderName

Log4j.appender.appenderName.option1 = value1

...

Log4j.appender.appenderName.option = Valuen

Among them, the Appender provided by LOG4J have the following most commonly used:

Org.apache.log4j.ConsoleAppender (console)

Org.apache.log4j.FileAppender (file)

Org.apache.log4j.DailyRollingFileAppender (Generate 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 (send log information in stream format to any specified location)

3. Configure the format of the log information (Layout) with the following syntax:

Log4j.appender.appenderName.layout = Org.apache.log4j.layoutName

Log4j.appender.appenderName.layout.option1 = value1

...

Log4j.appender.appenderName.layout.option = Valuen

Among them, the layout provided by LOG4J has the following several most commonly used:

Org.apache.log4j.HTMLLayout (Layout in HTML table Form)

Org.apache.log4j.PatternLayout (flexibility to specify layout mode)

Org.apache.log4j.SimpleLayout (contains level and information strings for log information)

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log

log4j (ii)--LOG4J Basic use method

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.