log4j configuration file details and manual loading mode

Source: Internet
Author: User
Tags log4j
I. Description of the configuration file:
1, Log4j.rootlogger=info, stdout, R
Use stdout and R for the log information of the level info to output, here you can name;
The level can be divided into off, FATAL, ERROR, WARN, INFO, DEBUG, All,off is off, do not output any information, the other levels in this order to reduce, if you specify a level, such as info, then higher than the level of information will output, at this time will output FATAL, ERROR, WARN, info level information.

2, Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.option1=value1
Defines the output end type of the stdout, as well as the property option configuration, which can have the following types:
Org.apache.log4j.ConsoleAppender (output in console)
Org.apache.log4j.FileAppender (output to file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches a specified size) and can be set by LOG4J.APPENDER.R.MAXFILESIZE=100KB. can also be set to save a backup file through Log4j.appender.r.maxbackupindex=1
Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)

Output Side common options:
Threshold=warn: Specifies the lowest level of output for log messages.
Immediateflush=true: The default value is true, meaning that all messages will be immediately exported.

Consoleappender option:
Target=system.err: By default: System.out, specify output console

File output side common options:
File=mylog.txt: Specifies the message output to the Mylog.txt file.
Append=false: The default value is True, the message is incremented to the specified file, and false refers to overwriting the specified file contents with the message.

Dailyrollingfileappender option:
DATEPATTERN=YYYY-WW: Scrolls the file once a week, that is, a new file is generated each week. Of course, you can specify the month, week, day, time, and minutes. The corresponding format is as follows:
1) yyyy-mm: Monthly
2) YYYY-WW: Weekly
3) Yyyy-mm-dd: Every day
4) Yyyy-mm-dd-a: two times a day
5) Yyyy-mm-dd-hh: Per hour
6) yyyy-mm-dd-hh-mm: Per minute

Rollingfileappender option:
MAXFILESIZE=100KB: The suffix can be kb, MB, or GB. When the log file reaches that size, it scrolls automatically, moving the original content to the Mylog.log.1 file.
maxbackupindex=2: Specifies the maximum number of scrolling files that can be produced.

3, Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
The layout that defines the output of a stdout is a type and can have several types:
Org.apache.log4j.HTMLLayout (layout in HTML form),
Org.apache.log4j.PatternLayout (You can specify layout patterns flexibly),
Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)

4, log4j.appender.stdout.layout.conversionpattern= [APPName]%p [%t]%c.%m (%l) | %m%n
If you use pattern layout to specify the specific format of the printed information Conversionpattern, specific parameters:
%M the message specified in the output code
%p output priority, i.e. Debug,info,warn,error,fatal
%r output the number of milliseconds it takes to boot to output the log information
The class to which the%c output belongs, usually the full name of the class in which it is located
%t output The name of the thread that generated the log event
%n output a carriage return line feed, Windows platform "RN", UNIX platform "n"
%d output log point-in-time date or time, the default format is ISO8601, you can also specify the format after, such as:%d{yyyy MM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921%l output log event location, Includes the class name, the thread that occurred, and the number of lines in the code.
%x: The NDC (nested diagnostics environment) associated with the current line threads relative, especially in multiple client multi-threaded applications such as Java Servlets.
Percent%: output a "%" character
%F: The name of the file where the output log message was generated
%m: Output Execution method
%l: line number in output code
You can control the minimum width, the maximum width, and the alignment of text by adding modifiers between% and pattern characters. Such as:
1 C: Specifies the name of the output category, the minimum width is 20, and if the category name is less than 20, the default is right-aligned.
2)%-20c: Specifies the name of the output category, the minimum width is 20, if the category name is less than 20, the "-" number specifies left-aligned.
3)%.30c: Specifies the name of the output category, the maximum width is 30, if the category name is greater than 30, will be the left more characters truncated, but less than 30 words will not have spaces.
4). 30c: If the name of the category is less than 20 to fill the space, and the right alignment, if its name is longer than 30 characters, from the left side of the exported characters are cut off.
[APPName] is the beginning of log information, can be any character, generally for the project abbreviation.

5, Log4j.appender.r=org.apache.log4j.dailyrollingfileappender

6, Log4j.appender.r.file=d:\\logs\\appname.log
Defines the file name D:\\logs\\appname.log of the output end of R, which can be modified by itself.

7, Log4j.appender.r.layout=org.apache.log4j.patternlayout

8, Log4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n

9, Log4j.logger.com.johnny=debug

Customize the log level in each package, specifying that all classes under the Com.johnny package have a level of debug.

Second, loading their own configuration files
log4j the Log4j.properties file under the default load Classpath, you need to use Domconfigurator.configure when customizing the properties file for other names ("e:/study/log4j/ Log4j.xml ");//load. xml file
Propertyconfigurator.configure ("E:/study/log4j/log4j.properties"),//Loading the. properties file to load the configuration file,

Notice the path to the configuration file here:
For Java projects:
1, absolute path-f:\\test\\log4j.properties;
This is needless to say, if not a Web project, write absolute path that is the full path on the hard drive
2, relative path--src/youfilename.properties
log4j default current path project below, that is, with the SRC sibling directory, understand this believe that the relative path is all extremely easy, or class loader to obtain a path can also;

For Web projects:
Getrealpath Get path plus filename


Three: Dynamic change record level and policy and load in the Web, that is, modify log4j.properties, do not need to restart Web applications, which need to be set in the Web.xml.
1 The log file in/web-inf/logs/without the need to write an absolute path.
2. Log4j.properties can be put together with other properties in/web-inf/, not class-path.

First, we need to set up a web.xml

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value> web-inf/log4j.properties</param-value>
</context-param>

<context-param>
< param-name>log4jrefreshinterval</param-name>
<param-value>60000</param-value>
</context-param>

<listener>
<listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class>


The second part is the key to dynamically modifying the log4j.properties, which scans the log4j configuration file every 60 seconds. For log4j configuration file How to write, this is not much said, we can go to Google, there is a point is that if we use Rollingfileappender or Fileappender, you can pass ${webapp.root} To navigate to the server's publication under this project, which is where spring has pressed the path of the Web directory into the webapp.root system variable. Then, you can define the logfile position in the log4j.properties.

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.