Basic log4j Configuration

Source: Internet
Author: User
Tags ranges

4Basic log4j Configuration

Log4j consists of three important components: loggers, appenders, and layouts, which respectively indicate the priority of log information, the output destination of log information, and the output format of log information. Supports key = value format setting or XML format setting.

L the priority of log information ranges from high to low, including fatal, error, warn, info, and debug, which are used to specify the importance of the log information;

L The log output destination specifies whether the log will be printed to the console or a file;

L The output format controls the display of log information.

4.1Log priority

Log4j is divided into off, fatal, error, warn, info, debug, all, or the level you define. We recommend that you use only four levels of log4j. The priority ranges from high to low: error, warn, info, and debug. By defining the level here, you can control the switch to the corresponding level of log information in the application.

If a log request with a level of P occurs in a logger with a level of Q, if P> = Q, the request is enabled. This is the core principle of log4j. For example, if the info level is defined here, all debug-level logs in the application will not be printed.

4.2Use of the output source

The available or disabled log requests are only part of the log4j function. Log4j allows log requests to be output to multiple output sources. In log4j, an output source is called an appender. Appender includes console, files, Gui components, remote socket servers, JMS ), NT Event loggers (NT Event Logs), and remote UNIX syslog daemons (Remote UNIX background Log Service ). It can also achieve asynchronous record.

A logger can set more than one appender. Use the addappender method to add an appender to a given logger. For a given logger, every valid log request is forwarded to all appender of the logger and the appender of the parent Logger of the logger.

4.2.1 leleappender

If consoleappender is used, the log information is written to the console. The effect is equivalent to printing the information directly to system. Out.

4.2.2 fileappender

When fileappender is used, log information is written to the specified file. This is often used. Correspondingly, the log output file name should be specified in the configuration file. For example, the logfile name is dglog.txt.
Log4j.appender.a2.file1_dglog.txt

Replace A2 with the appender alias in the specific configuration.

4.2.3 dailyrollingappender

You can use fileappender to output log information to a file, but it is inconvenient to read the file if it is too large. Then you can use dailyrollingappender. Dailyrollingappender can output log information to files differentiated by date. The configuration file generates a log file every day. Each log file only records the log information of the current day:

Log4j. appender. A2 = org. Apache. log4j. dailyrollingfileappender

Log4j. appender. a2.file = dglog

Log4j. appender. a2.datepattern = '. 'yyyy-mm-dd

Log4j. appender. a2.layout = org. Apache. log4j. patternlayout

Log4j. appender. a2.layout. conversionpattern = % 5R %-5 p % c {2}-% m % N

4.2.4 rollingfileappender

When the file size reaches the specified size, a new file is generated.
Log4j. appender. r = org. Apache. log4j. rollingfileappender
Log4j. appender. R. File = ../logs/dglog. Log
# Control the maximum log file size
Log4j. appender. R. maxfilesize = 100kb
# Archive log files (one backup file here)
Log4j. appender. R. maxbackupindex = 1
Log4j. appender. R. layout = org. Apache. log4j. patternlayout
Log4j. appender. R. layout. conversionpattern = % P % T % C-% m % N
This configuration file specifies the output source R, which is a rotation log file. The maximum size of a log file is KB. When the maximum size of a log file is reached, log4j automatically sets example. rename log to dglog. log.1, and then create a new dglog. log files are rotated sequentially.

4.2.5 writerappender

Sends the log information to any specified place in the stream format.

4.3Layout Configuration

Layout specifies the log output style.

4.3.1 layout style

Org. Apache. log4j. htmllayout (in the form of HTML tables ),
Org. Apache. log4j. patternlayout (you can flexibly specify the layout mode ),
Org. Apache. log4j. simplelayout (including the log information level and information string ),
Org. Apache. log4j. ttcclayout (including the log generation time, thread, category, and so on)

4.3.2 format

% M output the specified message in the code
% P output priority, namely debug, info, warn, error, fatal
% R the number of milliseconds it takes to output the log information from application startup to output
% C output category, usually the full name of the class
% T name of the thread that outputs the log event
% N output a carriage return line break, Windows platform is "RN", UNIX platform is "N"
% D date or time of the log output time point. The default format is iso8601. You can also specify the format after it, for example, % d {YYY Mmm dd hh: mm: SS, SSS}, output is similar to: October 18, 2002 22:10:28, 921
% L location of log event output, including category name, thread, and number of lines in the code. Example: testlog4.main (test log4.java: 10)

4.3.3 example

Example 1: Display date and log information
Log4j. appender. a2.layout = org. Apache. log4j. patternlayout
Log4j. appender. a2.layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS, SSS} % m % N
The printed information is:
2002-11-12 11:49:42, 866 select * From role where 1 = 1 order by createdate DESC


Example 2: Display date, log location, and log information
Log4j. appender. a2.layout = org. Apache. log4j. patternlayout
Log4j. appender. a2.layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS, SSS} % L "#" % m % N
11:51:46, 313cn.net. unet. weboa. system. Dao. roledao. Select (roledao. Java: 409 )"#"
Select * From role where 1 = 1 order by createdate DESC

Example 3: Display Log Level, time, call method, and log information
Log4j. appender. a2.layout = org. Apache. log4j. patternlayout
Log4j. appender. a2.layout. conversionpattern = [%-5 p] % d {yyyy-mm-dd hh: mm: SS, SSS}
Method: % L % N % m % N
Log information:
[Debug] 12:00:57, 376
Method: cn.net. unet. weboa. system. Dao. roledao. Select (roledao. Java: 409)
Select * From role where 1 = 1 order by createdate DESC

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'><appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="[%-5p] [%d{yyyy-MM-dd HH\:mm\:SSS}] %c - %m%n  " /></layout></appender><!-- appender name="WARN" class="org.apache.log4j.DailyRollingFileAppender" this is every day create log file --><appender name="WARN" class="org.apache.log4j.RollingFileAppender"><param name="File" value="/log/app-warn.log" /><!--  param name="DatePattern"   value="'.'yyyy-MM-dd"/ --><param name="Append" value="true" /><param name="MaxFileSize" value="512KB" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="[%-5p] [%d{yyyy-MM-dd HH\:mm\:SSS}] %c - %m%n  " /></layout><filter class="org.apache.log4j.varia.LevelRangeFilter"><param name="LevelMin" value="WARN" /><param name="LevelMax" value="WARN" /></filter></appender><appender name="ERROR" class="org.apache.log4j.RollingFileAppender"><param name="File" value="/log/app-error.log" /><param name="Append" value="true" /><param name="MaxFileSize" value="512KB" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="[%-5p] [%d{yyyy-MM-dd HH\:mm\:SSS}] %c - %m%n  " /></layout><filter class="org.apache.log4j.varia.LevelRangeFilter"><param name="LevelMin" value="ERROR" /><param name="LevelMax" value="ERROR" /></filter></appender><root><priority value="WARN" /><appender-ref ref="STDOUT" />        <appender-ref ref="WARN" />        <appender-ref ref="ERROR" /></root></log4j:configuration>


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.