一、log4j的组成:Loggers, Appenders and Layouts
Log4j has three main components: loggers,appenders andlayouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.
log4j主要由三部分组成:loggers,appenders,layouts
(1)Loggers
Loggers are named entities. Logger names are case-sensitive and they follow the hierarchical naming rule
logger的命名,区分大小写,并且遵循层级命名规则。例如,logger A 名为"com.foo",logger B 名为"com.foo.Bar",那么A 是B的父亲
The root logger resides at the top of the logger hierarchy.Invoking the class staticLogger.getRootLogger method retrieves it. All other loggers are instantiated and retrieved with the class staticLogger.getLogger method.
rootLogger位于最顶层,并且可通过静态方法Logger.getRootLogger获得。
Loggers may be assigned levels. The set of possible levels, that isTRACE,DEBUG,INFO,WARN,ERROR andFATAL are defined in theorg.apache.log4j.Level class.If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level
loggers的记录级别:TRACE,DEBUG,INFO,WARN, ERROR and FATAL 。记录级别具有继承性,例如logger A 未定义记录级别,则会找其父类记录级别
(2)Appenders and Layouts
Log4j allows logging requests to print to multiple destinations. In log4j speak, an output destination is called anappender. Currently, appenders exist for theconsole,files, GUI components,remote socket servers,JMS,NT Event Loggers, and remote UNIX Syslog daemons. It is also possible to log asynchronously.
log4j允许将日志输出 到过个 destination。例如:console,files。。。。
The addAppender method adds an appender to a given logger. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy.
日志输出目的地地 具有 累加性,即日志会输出到logger对象指定的所有输出destination和该logger对象祖先指定的输出destination
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender -------这句话的意思是定义CONSOLE为org.apache.log4j.ConsoleAppender(log4j的控制台输出),Log4j提供的appender有以下几种:
org.apache.log4j.ConsoleAppender(控制台)
org.apache.log4j.FileAppender(文件)
org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生新文件)
org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
More often than not, users wish to customize not only the output destination but also the output format. This is accomplished by associating alayout with an appender.
可通过layout指定日志输出的样式
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout -------定义log4j使用的输出布局(输出格式)。
log4j提供以下4种布局样式:
org.apache.log4j.HTMLLayout(以HTML表格形式布局)
org.apache.log4j.PatternLayout(可以灵活地指定布局模式,就是可以自定义输出样式),
org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
log4j.appender.CONSOLE.layout.ConversionPattern=%d{MM-ddHH:mm:ss}[%c-%L][%t][%-4r] - %m%n -------这个就是针对PatternLayou你自定义的输出格式,重点讲解一下打印参数, Log4J采用的是类似C语言中的printf函数的打印格式格式化日志信息的
%m 输出代码中指定的消息
%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL
%r 输出自应用启动到输出该log信息耗费的毫秒数
%c 输出所属的类目,通常就是所在类的全名
%t 输出产生该日志事件的线程名
%n 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”,也就是一跳消息占用一行
%d 输出日志时间点的日期或时间,紧跟一对花括号进行自定义格式
%c 输出所属的类目,通常就是所在类的全名
%l 精确到行
%x 输出对齐
As a side note, let me mention that in log4j child loggers link only to their existing ancestors. In particular, the logger namedcom.foo.Bar is linked directly to theroot logger, thereby circumventing the unusedcom or com.foo loggers. This significantly increases performance and reduces log4j's memory footprint.
child loggers 使用共有的祖先,这样可以明显提高性能
log4j配置实例1:
[html] view plaincopy
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配置实例2
[html] view plaincopy
log4j.rootLogger=DEBUG, CONSOLE, FILE
## for console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{MM-ddHH:mm:ss}[%c-%L][%t][%-4r] - %m%n
## for file
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=D:/logs/log4j.log
log4j.appender.FILE.MaxFileSize=1MB
log4j.appender.FILE.Append = true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-ddHH\:mm\:ss} [%t] %-5p %c(line-%L) %-4r %x - %m%n
分享到:
相关推荐
log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档log4j配置文档
log4j配置和加载方法log4j配置和加载方法
log4j 详细配置 log4j 详细配置 log4j 详细配置 log4j 详细配置
Log4j配置说明.rarLog4j配置说明.rarLog4j配置说明.rarLog4j配置说明.rar,Log4j配置,Log4j Log4j配置说明.rarLog4j配置说明.rarLog4j配置说明.rarLog4j配置说明.rar,Log4j配置,Log4j
常用log4j配置 [转] 常用log4j配置,
log4j中配置日志文件相对路径方法分析 方法一、 解决的办法自然是用相对路径代替绝对路径,其实log4j的FileAppender本身就有这样的机制,如:log4j.appender.logfile.File=${WORKDIR}/logs/app.log 其中“${...
tomcat下的log4j日志配置,给tomcat配置log4j有好几种方法,我知道的有: 一、tomcat级别的统一日志管理 二、每个webapp分别配置log4j
log4j配置文件以及配置文件说明 包里有一个log4j配置文件 和一个配置详解
主要讲述log4j的简介,以及log4j的配置和在实际应用的使用。如果您不知道如何配置log4j,那么这本书将对您有很大的帮助。
完整的log4j配置文件
Log4j2学习笔记,引入log4j2的依赖-log4j2.xml配置模板-application.properties文件配置-使用
Log4j配置详解 特别清晰,大家可以下载来参考学习,有问题可以反馈
概述了log4j的基本配置信息。很适合初学者学习log4j的配置使用。
Log4j日志配置说明,Log4j日志配置说明Log4j日志配置说明,Log4j日志配置说明
log4j配置文件例子,可以让你轻松配置log4j
tomcat6.0 配log4j日志的必须文件及配置过程 tomcat6-------lib | |--------tomcat-juli-adapters.jar | |--------log4j.jar | |--------log4j.properites | |----bin |--------tomcat-juli.jar 最后把log4...
SpringBoot框架配置log4j和log4j2的配置代码,项目中用到的配置文件,可下载使用
log4j2配置,文件分割,自动清除等相关配置。
Log4j配置资料大全 log4j学习 log4j配置 log4j资料
log4及配置文件 有log4j文件 log4及配置文件 有log4j文件 log4及配置文件 有log4j文件