wooodyhuang 2013-12-09
log4j是类似于java.util.logging的日志作用,即记录一些有用信息,是一个日志框架;
log4j == log for Java
日志框架的作用:
(1)函数参数是否正确;
(2)软件发布后,记录用户的每一步操作;
(3)记录程序运行出错位置;
log4j在http://logging.apache.org/log4j/1.2/download.html 中进行下载;
log4j的api文档在log4j/site/apidocs/index.html中;
log4j可以改变日志记录的形式比如HTML、Simple等;
log4j可以改变日志输出的目的地,比如File、DB等;
原本是一个zip包,取出jar包,并在Eclipse的某个Project build path导入;
log4j需要配置文件log4j.properties进行配置,log4j.properties位于src目录 下;
log4j.properties规则:
(1)键值对;
(2)#代表注释;
(3)不支持中文;
Log4jDemo.java
package org.impl; import org.apache.log4j.Logger; public class Log4jDemo { public static void main(String args[]){ Logger logger = Logger.getLogger(org.impl.Log4jDemo.class); logger.debug("debug"); logger.info("info"); logger.error("error"); } }
package org.impl; import org.apache.log4j.Logger; public class Log4jDemo { public static void main(String args[]){ Logger logger = Logger.getLogger(org.impl.Log4jDemo.class); logger.debug("debug"); logger.info("info"); logger.error("error"); } }
log4j.rootLogger=debug,appender1 log4j.appender.appender1=org.apache.log4j.FileAppender log4j.appender.appender1.File=./java0.log log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
log4j.rootLogger=debug,appender1 log4j.appender.appender1=org.apache.log4j.FileAppender log4j.appender.appender1.File=./java0.log log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
这样就指定了日志输出到工程目录下的java0.log文件中;
注意:这个日志文件时追加类型的。
任务2:将日志输出以HTML格式输出(效果很好!)
修改配置文件为:
log4j.rootLogger=debug,appender1,appender2 log4j.appender.appender1=org.apache.log4j.FileAppender log4j.appender.appender1.File=./log4j.HTML log4j.appender.appender1.layout=org.apache.log4j.HTMLLayout log4j.appender.appender2=org.apache.log4j.ConsoleAppender log4j.appender.appender2.layout=org.apache.log4j.SimpleLayout
log4j.rootLogger=debug,appender1,appender2 log4j.appender.appender1=org.apache.log4j.FileAppender log4j.appender.appender1.File=./log4j.HTML log4j.appender.appender1.layout=org.apache.log4j.HTMLLayout log4j.appender.appender2=org.apache.log4j.ConsoleAppender log4j.appender.appender2.layout=org.apache.log4j.SimpleLayout
1.Logger
设定了显示日志的级别;
常用级别为debug,info,warn,error,fatal;
设定了日志的输出目的;
常用有:
(1)ConsoleAppender
(2)FileAppender
(3)DailyRollingFileAppender 一天换一个文件
(4)JdbcAppender 输出到数据库
设定日志的内容格式;
常用有:
(1)HTMLLayout;
(2)TTCCLayout;
(3)PatternLayout;制定布局样式
如果要设置PatternLayout,必须将配置文件修改为以下形式:
log4j.rootLogger=debug,appender1,appender2 log4j.appender.appender1=org.apache.log4j.FileAppender log4j.appender.appender1.File=./log4j.HTML log4j.appender.appender1.layout=org.apache.log4j.HTMLLayout log4j.appender.appender2=org.apache.log4j.ConsoleAppender log4j.appender.appender2.layout=org.apache.log4j.PatternLayout log4j.appender.appender2.layout.ConversionPattern= 格式名称
log4j.rootLogger=debug,appender1,appender2 log4j.appender.appender1=org.apache.log4j.FileAppender log4j.appender.appender1.File=./log4j.HTML log4j.appender.appender1.layout=org.apache.log4j.HTMLLayout log4j.appender.appender2=org.apache.log4j.ConsoleAppender log4j.appender.appender2.layout=org.apache.log4j.PatternLayout log4j.appender.appender2.layout.ConversionPattern= 格式名称
格式形式如下:
%p:日志优先级(debug、info)
%t:输出日志的线程名;
%d{yyyy-MM-dd HH:mm:ss}:时间;
%c:所属类名;
%r:日志输出所花时间;
%n:换行;
%l:日志所在行;
%m:信息;
%M:方法;
%r [%t] -%l %p %d{yyyy-MM-dd HH:mm:ss} %n
显示如下:
3 [main] -org.impl.Log4jDemo.main(Log4jDemo.java:10) ERROR 2012-01-11 17:11:37
3表示所花时间;
[main]表示线程;
org.impl.Log4jDemo.main(Log4jDemo.java:10) 表示 %l ;
ERROR表示%p;