- 浏览: 60262 次
- 性别:
- 来自: 深圳
文章分类
最新评论
文章出处:http://www.blogjava.net/rickhunter/articles/28133.html Log4j的概念 Log4j的配置文件 首先,是设置root,格式为<!--StartFragment --> log4j.rootLogger=[level],appenderName, ...,其中level就是设置需要输出信息的级别,后面是appender的输出的目的地,<!--StartFragment -->appenderName就是指定日志信息输出到哪个地方。您可以同时指定多个输出目的地。 配置日志信息输出目的地Appender,其语法为 配置日志信息的格式(布局),其语法为: %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL
<!--StartFragment --> Log4j中有三个主要的组件,它们分别是 Logger、Appender和Layout,L<!--StartFragment -->og4j 允许开发人员定义多个Logger,每个Logger拥有自己的名字,Logger之间通过名字来表明隶属关系。有一个Logger称为Root,它永远存在,且不能通过名字检索或引用,可以通过Logger.getRootLogger()方法获得,其它Logger通过 Logger.getLogger(String name)方法。
Appender则是用来指明将所有的log信息存放到什么地方,Log4j中支持多种appender,如<!--StartFragment --> console、files、GUI components、NT Event Loggers等,一个Logger可以拥有多个Appender,也就是你既可以将Log信息输出到屏幕,同时存储到一个文件中。
Layout的作用是控制Log信息的输出方式,也就是格式化输出的信息。
Log4j中将要输出的Log信息定义了5种级别,依次为DEBUG、INFO、WARN、ERROR和FATAL,当输出时,只有级别高过配置中规定的级别的信息才能真正的输出,这样就很方便的来配置不同情况下要输出的内容,而不需要更改代码,这点实在是方便啊。
虽然可以不用配置文件,而在程序中实现配置,但这种方法在如今的系统开发中显然是不可取的,能采用配置文件的地方一定一定要用配置文件。Log4j支持两种格式的配置文件:XML格式和Java的property格式,本人更喜欢后者,首先看一个简单的例子吧,如下:
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize= 100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = value1
...
log4j.appender.appenderName.option = valueN
Log4j提供的appender有以下几种:
org.apache.log4j.ConsoleAppender(控制台)
org.apache.log4j.FileAppender(文件)
org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生新文件)
org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = value1
...
log4j.appender.appenderName.option = valueN
Log4j提供的appender有以下几种:
org.apache.log4j.ConsoleAppender(控制台)
org.apache.log4j.FileAppender(文件)
org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生新文件)
org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = value1
...
log4j.appender.appenderName.option = valueN
Log4j提供的appender有以下几种:
org.apache.log4j.ConsoleAppender(控制台)
org.apache.log4j.FileAppender(文件)
org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生新文件)
org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
log4j.appender.appenderName.layout.option1 = value1
....
log4j.appender.appenderName.layout.option = valueN
Log4j提供的layout有以下几种:
org.apache.log4j.HTMLLayout(以HTML表格形式布局),
org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
Log4J采用类似C语言中的printf函数的打印格式格式化日志信息,打印参数如下: %m 输出代码中指定的消息
%r 输出自应用启动到输出该log信息耗费的毫秒数
%c 输出所属的类目,通常就是所在类的全名
%t 输出产生该日志事件的线程名
%n 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似: 2002年10月18日 22:10:28,921
%l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java:10)
Log4j在程序中的使用
要在自己的程序中使用Log4j,首先需要将commons-logging.jar和logging-log4j-1.2.9.jar导入到构建路径中。然后再将log4j.properties放到src根目录下。这样就可以在程序中使用log4j了。在类中使用log4j, 首先声明一个静态变量 Logger logger=Logger.getLog("classname");现在就可以使用了,用法如下:logger.debug("debug message")或者logger.info("info message"),看下面一个小例子: import com.foo.Bar;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class MyApp {
static Logger logger = Logger.getLogger(MyApp.class.getName());
public static void main(String[] args) {
// BasicConfigurator replaced with PropertyConfigurator.
PropertyConfigurator.configure(args[0]);
logger.info("Entering application.");
Bar bar = new Bar();
bar.doIt();
logger.info("Exiting application.");
}
}
发表评论
-
清理mongodb最老的数据
2017-01-18 14:20 2722public class MongoDBDataClean ... -
db2
2012-02-20 17:37 665Class.forName("com.ibm.db2 ... -
网上银行one
2011-10-24 23:08 862关于网银的一些认识, ... -
send email by java api
2010-12-21 17:46 986First ,create a properies file ... -
ireport using javabean as the datasource
2010-10-28 14:11 1793转自:http://hi.baidu.com/nieweigu ... -
Export the excel/txt file of report list by java
2010-09-21 16:16 1680First of all, we should click a ... -
batch execution
2010-02-26 17:09 768Statement st = null; try{ ... -
Socket -learning a little from IBM tutorial
2010-01-14 18:43 943There are three files to comple ... -
Simple expample of MessageDigest
2009-11-16 17:57 731When use the encrypted arithmet ... -
judgement method of collections
2009-10-19 18:18 590When the collections want to ev ... -
The difference of some collections
2009-09-24 18:05 716The content of this chapter is ... -
Aop of Spring2.5 doesnot support well in the jdk1.6
2009-08-20 20:53 691The exception throwed by the ap ... -
The usage of time and date
2009-07-30 20:46 700There are some usage of time we ... -
Widget developpment by JIL SDK
2009-07-30 19:49 899Widget is an application that c ... -
Testing of override of toString()
2009-07-27 19:15 496At fisrt,thanks for the tips of ... -
Bubble Sort - The arithmetic always queried by interviewer
2009-07-27 18:26 774/** * * @author tanglei ...
相关推荐
- Support for a log formatter (similar to Layouts in Log4J) - Support for console log - Support for smtp formatter - Support for XML logs - Support for syslog - Support for changing format and level ...
In order to better support interactive or batch usage, most of the tools in the Identity Toolbox accept either floating point or string arguments. String arguments, either for a file name or a ...
Such coding is within 1 byte of the Shannon limit, log 1/P(y), so compression depends almost entirely on the goodness of the model, P, i.e. how well it estimates the probability distribution of ...
Khalid Belhajjame (The University of Manchester) Norman W. Paton (The University of Manchester) Alvaro A. A. Fernandes (The University of Manchester) Suzanne M. Embury (The University of Manchester) ...
Access, Excel 95/97 (VBA) Windows Scripting Host (cscript / wscript) Perl 5.x for Win32 Inprise Delphi 4 Microsoft Visual C++ 4.x/5.x/6.x Microsoft Visual J++ 1.x/6.0 (Java) <END>...
Every Apktool release contains internally the most up to date AOSP framework at the time of the release. This allows you to decode and build most apks without a problem. However, manufacturers add ...
- Animal Crossing (Currently cannot be run in this mode, force usage of DMA mode). - Hidamari Sketch Dokodemo Sugoroku x 365 + Game Fixes: - Grand Theft Auto Chinatown Wars (U) Thanks gelu. - ...
--timestamps <format> emit a timestamp at the start of each output line (using optional format string as per strftime(3)) -d, --debug emit debugging output -v, --version show version information ...
‘‘,‘ -j[arguments] = join (multiplex) two RealMedia streams‘,‘ arguments:‘,{ __UNIX__}‘ firstfilename = pathname of the file containing first stream,‘,‘ default is stream00.rm‘,‘ ...
ufw-stats ... -j, --json produce JSON optput instead of plain text -o, --output=FILE direct output to the FILE -f, --follow tail the log (continuously print new entries) -n, --num-actio
@echo Now initializing the program,please wait a minite... @format X: /q/u/autoset (format 这个命令是不可以使用/y这个参数的,可喜的是微软留了个autoset这个参数给我们,效果和/y是一样的。) 3.Goto 命令 ...
J. History Commands K. A Sample .bashrc File L. Converting DOS Batch Files to Shell Scripts M. Exercises M.1. Analyzing Scripts M.2. Writing Scripts N. Revision History O. Mirror Sites P. To Do ...
J. History Commands K. A Sample .bashrc File L. Converting DOS Batch Files to Shell Scripts M. Exercises M.1. Analyzing Scripts M.2. Writing Scripts N. Revision History O. Mirror Sites P. To Do List Q...
motd(Message of The Day)是一个系统信息文件(system information file)。 #### mozilla=MOsaicGodZILLa Mozilla(MOsaic God ZILLa)是一个开源的Web浏览器。 #### mtab=MountTABle mtab(Mount TABle)是一个动态...
Using Log4J ............................................................................................. 14 II. What’s New in Spring Framework 4.x ......................................................
Using Log4J ............................................................................................. 14 II. What’s New in Spring Framework 4.x ......................................................