log4 use Description

log4 use:

log4 is a logging function primarily to a program monitored by a profile
in two configurations: A program configurations, one configuration file
has three main units to understand, Logger, appender, layout.

logger is the main class recording, appender is a record of the way, layout is recorded format
logger seven kinds of log level: debug, info, warn, error , fatal, all, off
the most commonly used should be the debug () and info () ; and warn (), error (), fatal () is used only after the corresponding event occurs

appender three main types, the output terminal, the output file stream output
the ConsoleAppender
the FileAppender
WriterAppender

layout can be divided into three categories: simple format, html format matching the format
The SimpleLayout
HTMLLayout
the PatternLayout

Output :( shorthand used in the PatternLayout)
% m output code specified in the message
% p output priority
% r output from the application starts to output the log information on the number of milliseconds spent
% c output category belongs to the class where usually the whole name
% t output to produce the event log name of thread
output% n Enter a newline, Windows platform "\ r \ n", Unix platform "\ n"
date or time% d output log point in time, the default format is ISO8601, you can specify the format thereafter, for example:% d {yyy MMM dd HH : mm: ss, SSS}, similar output: 22 October 18, 2002: 10: 28,921
% L output log events occurrence locations, including categories were the threads, as well as the number of lines in the code. General use it without the use of% c


Examples of action:
Configuration General procedure:
1 to give a specific name logger
2. obtained wherein a layout
3. The layout obtained appender
added 4.logger appender
5.logger set level
6.logger level log information output is

Program:
Logger = Logger Logger.getLogger (Test.class.getName ()) // get Logger
SimpleLayout simpleLayut = new new SimpleLayout () // obtain layout
FileAppender FA = new new FileAppender (SimpleLayout, "test.log") // get appender
logger.addAppender (fa) // add appender
logger.set ((level) Level.DEBUG) // set the level
logger.debug ( "hihihihih") // record information


Configuration file:
1. get a particular name Logger
2. loading a configuration file (configuration file to do the job 2-5)
3.logger output is level log information

File Configuration program:
1.Logger Logger = Logger.getLogger (Test.class.getName ()) // obtain Logger
2. Loading the following configuration
3. The output information


Sub-file configuration properties, xml
read Properties:
PropertyConfigurator.configure ( "logTest.properties");

读取xml:
DOMConfigurator.configure("src/logTest.xml");

Reproduced in: https: //www.cnblogs.com/licheng/archive/2008/08/23/1274567.html

Guess you like

Origin blog.csdn.net/weixin_34413103/article/details/93800880