logback demo

<?xml version="1.0" encoding="UTF-8" ?>


<configuration>
  <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />  


  <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<!--     <evaluator class="ch.qos.logback.classic.boolex.GEventEvaluator">
      <expression>
        (marker != null) &amp;&amp;
        (marker.contains("DEV") || marker.contains("TRANSACTION_FAILURE"))
      </expression>
    </evaluator> -->
    <SMTPHost>${smtpHost}</SMTPHost>
    <To>a@newegg.com</To>
    <From>b@newegg.com</From>
    <smtpHost>1.1.1.1</smtpHost>
    <smtpPort>215</smtpPort>
    <layout class="ch.qos.logback.classic.html.HTMLLayout"/>
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
            <evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
                <expression>
        (marker != null) &amp;&amp;
        (marker.contains("DEV") &amp;&amp; level == ERROR)           
                </expression>  
            </evaluator>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>    
        </filter>
  </appender>


  <root>
    <level value ="debug"/>
    <appender-ref ref="EMAIL" />
  </root>  

</configuration>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Logback Log Messages</title>
<style  type="text/css">
table { margin-left: 2em; margin-right: 2em; border-left: 2px solid #AAA; } TR.even { background: #FFFFFF; } TR.odd { background: #EAEAEA; } TR.warn TD.Level, TR.error TD.Level, TR.fatal TD.Level {font-weight: bold; color: #FF4040 } TD { padding-right: 1ex; padding-left: 1ex; border-right: 2px solid #AAA; } TD.Time, TD.Date { text-align: right; font-family: courier, monospace; font-size: smaller; } TD.Thread { text-align: left; } TD.Level { text-align: right; } TD.Logger { text-align: left; } TR.header { background: #596ED5; color: #FFF; font-weight: bold; font-size: larger; } TD.Exception { background: #A2AEE8; font-family: courier, monospace;} </style>


  </head>
<body>
<hr/>
<p>Log session start time Sat Dec 26 17:17:47 CST 2015</p><p></p>


<table cellspacing="0">
<tr class="header">
<td class="Date">Date</td>
<td class="Thread">Thread</td>
<td class="Level">Level</td>
<td class="Logger">Logger</td>
<td class="MDC">MDC</td>
<td class="Message">Message</td>
</tr>


<tr class="error even">
<td class="Date">2015-12-26 17:17:47,228</td> <td class="Thread">http-bio-80-exec-6</td>
<td class="Level">ERROR</td>
<td class="Logger">com.log.MyLogServlet</td>
<td class="MDC"></td>
<td class="Message">hello</td>
</tr>
</table>
</body></html>



/**
 * Logback: the reliable, generic, fast and flexible logging framework.
 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *
 *   or (per the licensee's choosing)
 *
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 */
package chapters.appenders;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;


import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
import chapters.appenders.sub.sample.Bar;


/**
 * 
 * This class can be used to check the result of a configuration file.
 * 
 * @author S&eacute;bastien Pennec
 */
public class ConfigurationTester {


  public static void main(String[] args) throws InterruptedException {
    Logger logger = (Logger) LoggerFactory.getLogger(ConfigurationTester.class);
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();


    try {
      JoranConfigurator configurator = new JoranConfigurator();
      configurator.setContext(lc);
      lc.reset();
      configurator.doConfigure(args[0]);
    } catch (JoranException je) {
      je.printStackTrace();
    }
    // After we've called Joran, let's print information about the
    // internal status of logback
    StatusPrinter.print(lc);


    logger.debug("**Hello {}", new Bar());
    MDC.put("testKey", "testValueFromMDC");
    MDC.put("testKey2", "value2");
    for (int i = 0; i < 10; i++) {
      logger.debug("logging statement " + i);
      Thread.sleep(100);
    }
    Bar bar = new Bar();
    bar.createLoggingRequest();
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值