日志_log4j 总结

 

[日志_log4j] 总结

分类: Java基础   72人阅读  评论(0)  收藏  举报

目录(?)[+]

1. log4j基础

  • 概述 
    java装载了自己的日志API(JDK1.4起),并且Tomcat6绑定了它的容器。日志API的友好实现,成为JULI。不过log4j由于它的完备和开发友好性,仍然是应用广泛的实现。容易配置log4j来记录运行的消息,不会影响应用程序的性能。
  • jar包下载地址:官网下载地址

2. log4j入门示例

  • java代码

    <code class="hljs d has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">Logger logger = Logger.getLogger(HelloLog4j.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//参数为该类的类名</span>
    logger.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">debug</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This is debug message."</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 记录 debug 级别的信息</span>
    logger.info(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This is info message."</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 记录 info 级别的信息</span>
    logger.error(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This is error message."</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 记录 error 级别的信息</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
  • 配置文件信息

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#可以设置级别: debug>info>warn>error</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#debug:显示 debug、 info、 error信息</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#info:显示 info、 error信息</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#error:只显示error信息</span>
log4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.rootLogger</span>=debug,appender1
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#log4j.rootLogger=info,appender1</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#log4j.rootLogger=error,appender1</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#输出到控制台</span>
log4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span>1=org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ConsoleAppender</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#样式为 TTCCLayout</span>
log4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span>1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.layout</span>=org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.TTCCLayout</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
  • 总结 
    通过配置文件可知,我们需要配置3个方面的内容(三个重要的组件构成): 
    1、根目录(级别和目的地); 
    2、目的地(控制台、文件等等); 
    3、输出样式。

3. log4j配置文件

  1. log4j类关系图 
    这里写图片描述
  2. 说明

    Logger:日志写出器,供程序员输出日志信息 
    Appender:日志目的地,把格式化好的日志信息输出到指定的地方去 
    ConsoleAppender:目的地为控制台的 
    AppenderFileAppender:目的地为文件的 
    AppenderRollingFileAppender:目的地为大小受限的文件的 
    AppenderLayout:日志格式化器,用来把程序员的loggingrequest格式化成字符串 
    PatternLayout:用指定的pattern格式化loggingrequest的Layout

  3. Log4j配置文件说明

    • 配置根Logger 
      语法:log4j.rootLogger=[level],appenderName,appenderName,…… 
      说明:level是日志记录的优先级,分为OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或者您定义的级别。Log4j建议只使用四个级别,优先级从高到低分别是ERROR、WARN、INFO、DEBUG。通过在这里定义的级别,您可以控制到应用程序中相应级别的日志信息的开关。比如在这里定义了INFO级别,则应用程序中所有DEBUG级别的日志信息将不被打印出来。appenderName就是指日志信息输出到哪个地方。您可以同时指定多个输出目的地。
    • 配置日志信息输出目的地Appender, 
      语法

      <code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">log4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appenderName</span>=fully<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.qualified</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.name</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.of</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class</span>
      log4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appenderName</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.option</span>1=value1……
      log4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appenderName</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.option</span>=valueN</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

      说明

      <code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ConsoleAppender</span>(控制台),
      org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.FileAppender</span>(文件),
      org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.DailyRollingFileAppender</span>(每天产生一个日志文件),
      org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.RollingFileAppender</span>(文件大小到达指定尺寸的时候产生一个新的文件),
      org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WriterAppender</span>(将日志信息以流格式发送到任意指定的地方)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
    • 配置日志信息的格式(布局) 
      语法

      <code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">log4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appenderName</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.layout</span>=fully<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.qualified</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.name</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.of</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.layout</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class</span>
      log4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appenderName</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.layout</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.option</span>1=value1……
      log4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appender</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.appenderName</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.layout</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.option</span>=valueN</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

      **说明:**Log4j提供的layout有以几种

      <code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.HTMLLayout</span>(以HTML表格形式布局),
      org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.PatternLayout</span>(可以灵活地指定布局模式),
      org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.SimpleLayout</span>(包含日志信息的级别和信息字符串),
      org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.apache</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.log</span>4j<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.TTCCLayout</span>(包含日志产生的时间、线程、类别等等信息)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
    • 格式化打印日志信息

      %p输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL 
      %r输出自应用启动到输出该log信息耗费的毫秒数 
      %c输出所属的类目,通常就是所在类的全名 
      %t输出产生该日志事件的线程名 
      %n输出一个回车换行符,Windows平台为“rn”,Unix平台为“n” 
      %d输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如: 
      %d{yyyMMMddHH:mm:ss,SSS},输出类似:2002年10月18日22:10:28,921 
      %l输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数

4. 在代码中使用log4j

<code class="hljs delphi has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">///1.获取日志记录器</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> Logger getLogger( <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">String</span> name);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//这个记录器将负责控制日志信息</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">///2. 读取配置文件</span>
BasicConfigurator.configure () : 自动快速地使用缺省 Log4j 环境。
PropertyConfigurator.configure ( <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">String</span> configFilename) :读取使用Java 的特性文件编写的配置文件。
DOMConfigurator.configure ( <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">String</span> filename ) :读取 XML 形式的配置文件。
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">///3. 插入记录信息(格式化日志信息)</span>
Logger.debug ( <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Object</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">message</span> ) ;
Logger.info ( <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Object</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">message</span> ) ;
Logger.warn ( <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Object</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">message</span> ) ;
Logger.error ( <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Object</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">message</span> ) ;</code>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值