关于logback:

http://www.cnblogs.com/warking/p/5710303.html

官方文档

使用logback:

idea创建maven项目

修改pom.xml添加依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.dylan.java</groupId>
<artifactId>javacore</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
<scope>test</scope>
</dependency> </dependencies>
</project>

配置logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds"> <property name="log.base" value="E:/ideaProjects/log/" /> <appender name="startup.console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%-5level][%d{yyyy-MM-dd HH:mm:ss}][%F:%L] - %msg%n</pattern>
</encoder>
</appender> <appender name="startup.file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Encoding>UTF-8</Encoding>
<File>${log.base}startup.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.base}startup_%d{yyyy-MM-dd}.log</FileNamePattern>
<!--保留30天-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<!-- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%-5level][%d{yyyy-MM-dd HH:mm:ss}][%F:%L] - %msg%n</pattern>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder> -->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
</pattern>
</layout>
</appender> <root level="ERROR">
<appender-ref ref="startup.console"/>
</root> <root level="DEBUG">
<appender-ref ref="startup.console"/>
</root> <root level="DEBUG">
<appender-ref ref="startup.file"/>
</root> </configuration>

创建测试类:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.Random; /**
* Created by dylan-pc on 2017/10/23.
*/
public class TestLogback {
private static final transient Logger log= LoggerFactory.getLogger(TestLogback.class); public static void main(String args[]){
//System.out.println("Hello World!");
log.info("程序开始!");
try {
for (int i=1; i<=20; i++){
int r1=(int)(Math.random()*10); //被除数
int r2=(int)(Math.random()*100+99); //除数
log.debug("第"+i+"次计算表达式为:"+r2+"/"+r1);
log.debug("计算结果:"+(r2/r1));
log.info("----------------------------------");
} }catch (ArithmeticException e){
log.error("程序异常!除数等于0!");
//log.error(e.getStackTrace().toString());
}catch (Exception e){
log.error("程序异常!"+e.getMessage());
} log.trace("程序结束!"); } }

查看控制台和日志文件:

"C:\Program Files\Java\jdk1.7.0_80\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:\program\IntelliJ IDEA 14.1.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_80\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\zipfs.jar;E:\ideaProjects\test\javacore\target\test-classes;E:\ideaProjects\test\javacore\target\classes;D:\program\apache-maven-3.5.0\local-repo\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;D:\program\apache-maven-3.5.0\local-repo\ch\qos\logback\logback-core\1.1.7\logback-core-1.1.7.jar;D:\program\apache-maven-3.5.0\local-repo\ch\qos\logback\logback-classic\1.1.7\logback-classic-1.1.7.jar;D:\program\IntelliJ IDEA 14.1.5\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain TestLogback

22:37:31,675 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]

22:37:31,675 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]

22:37:31,675 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/E:/ideaProjects/test/javacore/target/classes/logback.xml]

22:37:31,765 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set

22:37:31,777 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/E:/ideaProjects/test/javacore/target/classes/logback.xml]

22:37:31,777 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 30 seconds

22:37:31,789 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]

22:37:31,793 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [startup.console]

22:37:31,863 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]

22:37:31,866 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [startup.file]

22:37:31,871 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@13:19 - no applicable action for [Encoding], current ElementPath  is [[configuration][appender][Encoding]]

22:37:31,880 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1026546903 - No compression will be used

22:37:31,882 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1026546903 - Will use the pattern E:/ideaProjects/log/startup_%d{yyyy-MM-dd}.log for the active file

22:37:31,887 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'E:/ideaProjects/log/startup_%d{yyyy-MM-dd}.log'.

22:37:31,887 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.

22:37:31,890 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Mon Oct 23 22:36:58 CST 2017

22:37:31,894 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - This appender no longer admits a layout as a sub-component, set an encoder instead.

22:37:31,894 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.

22:37:31,894 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details

22:37:31,895 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - Active log file name: E:/ideaProjects/log/startup.log

22:37:31,895 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - File property is set to [E:/ideaProjects/log/startup.log]

22:37:31,897 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ERROR

22:37:31,897 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [startup.console] to Logger[ROOT]

22:37:31,898 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG

22:37:31,898 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [startup.console] to Logger[ROOT]

22:37:31,898 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG

22:37:31,898 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [startup.file] to Logger[ROOT]

22:37:31,898 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.

22:37:31,899 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@114bc6a3 - Registering current configuration as safe fallback point
[INFO ][2017-10-23 22:37:31][TestLogback.java:14] - 程序开始!

[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第1次计算表达式为:107/9

[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:11

[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------

[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第2次计算表达式为:143/9

[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:15

[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------

[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第3次计算表达式为:192/7

[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:27

[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------

[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第4次计算表达式为:163/4

[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:40

[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------

[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第5次计算表达式为:148/4

[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:37

[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------

[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第6次计算表达式为:193/0

[ERROR][2017-10-23 22:37:31][TestLogback.java:25] - 程序异常!除数等于0!

[DEBUG][2017-10-23 22:37:31][TestLogback.java:31] - 程序结束!
Process finished with exit code 0

start

...

19:39:14.928 [main] DEBUG TestLogback - 第9次计算表达式为:194/5
19:39:14.928 [main] DEBUG TestLogback - 计算结果:38
19:39:14.928 [main] INFO  TestLogback - ----------------------------------
19:39:14.928 [main] DEBUG TestLogback - 第10次计算表达式为:160/2
19:39:14.928 [main] DEBUG TestLogback - 计算结果:80
19:39:14.928 [main] INFO  TestLogback - ----------------------------------
19:44:39.451 [main] INFO  TestLogback - 程序开始!
19:44:39.459 [main] DEBUG TestLogback - 第1次计算表达式为:116/8
19:44:39.459 [main] DEBUG TestLogback - 计算结果:14
19:44:39.459 [main] INFO  TestLogback - ----------------------------------
19:44:39.459 [main] DEBUG TestLogback - 第2次计算表达式为:194/5
19:44:39.459 [main] DEBUG TestLogback - 计算结果:38
19:44:39.459 [main] INFO  TestLogback - ----------------------------------
19:44:39.459 [main] DEBUG TestLogback - 第3次计算表达式为:109/3
19:44:39.459 [main] DEBUG TestLogback - 计算结果:36
19:44:39.459 [main] INFO  TestLogback - ----------------------------------
19:44:39.459 [main] DEBUG TestLogback - 第4次计算表达式为:109/0
19:44:39.459 [main] ERROR TestLogback - 程序异常!

...

logback日志组件使用案例的更多相关文章

  1. Logback 日志组件的使用

    Logback 是由 log4j 创始人设计的又一个开源日志组件. 一. logback 的介绍 ​ logback 当前分成三个模块:logback-core,logback- classic 和 ...

  2. 五分钟秒懂Java日志组件

    Java中有许多种日志记录方式,有些API有占位符,有些API没占位符,初学的人可能会搞不清楚这些日志组件的由来.我一开始的时候也是很懵逼的,后来一点点弄懂了于是就又了这篇文章. 在Java中进行日志 ...

  3. java日志组件介绍(common-logging,log4j,slf4j,logback )

    转自:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging是apache提供的一个通用的日志 ...

  4. 转:java日志组件介绍(common-logging,log4j,slf4j,logback )

    原网址:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging common-logging是 ...

  5. 日志组件logback的介绍及配置使用方法

    一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-acc ...

  6. 日志组件 logback

    一.简介 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access.logb ...

  7. 【转】java日志组件介绍(common-logging,log4j,slf4j,logback )

    common-logging common-logging是apache提供的一个通用的日志接口.用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的logging, comm ...

  8. 日志处理(二) 日志组件logback的介绍及配置使用方法(转)

    本文转自:http://www.cnblogs.com/yuanermen/archive/2012/02/13/2348942.html http://www.cnblogs.com/yuanerm ...

  9. 转:日志组件logback的介绍及配置使用方法

    转自:http://blog.csdn.net/zgmzyr/article/details/8267072 一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.lo ...

  10. 日志组件logback的介绍及配置使用方法(一)

    一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-acc ...

随机推荐

  1. [转帖]一文读懂 | 如何快速部署 OceanBase 开源版

    2021-11-281398 版权 本文涉及的产品 云数据库 RDS MySQL Serverless,0.5-2RCU 50GB 推荐场景: 学生管理系统数据库设计搭建个人博客 立即试用 云防火墙, ...

  2. 【转帖】什么是RLHF

    什么是RLHF? **字面翻译:**RLHF (Reinforcement Learning from Human Feedback) ,即以强化学习方式依据人类反馈优化语言模型. 强化学习从人类反馈 ...

  3. [转帖]Linux中的Page cache和Buffer cache详解

    1.内存情况 在讲解Linux内存管理时已经提到,当你在Linux下频繁存取文件后,即使系统上没有运行许多程序,也会占用大量的物理内存.这是因为当你读写文件的时候,Linux内核为了提高读写的性能和速 ...

  4. Sysbench的简单学习-编译与安装

    sysbench的简单学习-编译与安装 摘要 github上面获取一下最新版本 https://github.com/akopytov/sysbench 注意现在 2023.2.17 最新版是 sys ...

  5. 基于eBPF的微服务网络安全(Cilium 1)

    基于eBPF的微服务网络安全 翻译自:Network security for microservices with eBPF 一些开源的kubernetes工具已经开始使用eBPF,这些工具大多数与 ...

  6. 原生js拖拽元素(onmouseup不能够触发的原因)

    我们经常会遇见拖拽某一个元素的场景,拖拽也是很常用的: 这次拖拽遇见一个问题,有时在拖拽的时候吗,鼠标松开,元素仍然可以拖拽: 经过查阅资料,发现: 会触发H5原生的拖拽事件.并且不会监听到onmou ...

  7. vim 从嫌弃到依赖(18)——查找模式进阶

    上一篇文章中,我们初步结识了如何使用查找模式,也能够通过n和 N进行查找.这篇将会介绍搜索中更高级的用法.另外在写上一篇文章的时候我发现介绍查找相关内容的时候不能用动图来演示,主要是因为输入的内容太多 ...

  8. Matplotlib配置图例legend()设置透明和并排显示

    1.多排显示 x=np.linspace(start=-np.pi,stop=np.pi,num=300) plt.style.use('classic') Fig,Axes=plt.subplots ...

  9. C++ CryptoPP使用RSA加解密

    Crypto++ (CryptoPP) 是一个用于密码学和加密的 C++ 库.它是一个开源项目,提供了大量的密码学算法和功能,包括对称加密.非对称加密.哈希函数.消息认证码 (MAC).数字签名等.C ...

  10. 4、Web前端学习规划:JavaScript - 学习规划系列文章

    JavaScript作为Web前端里的第3重要的语言,笔者认为该重点进行学习.因为JavaScript衍生出来的框架和类库有不少,而且很强大.所以JavaScript的学习要抓好重点,在基本的语法及应 ...