logback日誌元件使用案例
關於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("程式結束!");
}
}
檢視控制檯和日誌檔案:
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
[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] - 程式結束!
...
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 - 程式異常!
...
相關文章
- Java 日誌框架 LogbackJava框架
- 【Logback日誌級別】動態調整Logback的日誌級別
- 【轉】java日誌 -logback的使用和logback.xml詳解JavaXML
- 在SpringBoot中使用Logback管理日誌Spring Boot
- logback 日誌輸出格式
- SpringBoot進階教程 | 第二篇:日誌元件logback實現日Spring Boot元件
- Spring Boot logback日誌配置Spring Boot
- Spring Boot 整合 Logback 日誌Spring Boot
- Java日誌框架:logback詳解Java框架
- springboot使用logback記錄日誌,配置檔案Spring Boot
- spring boot使用logback實現多環境日誌配置Spring Boot
- 使用logminer分析歸檔日誌案例
- 日誌工具logback的簡介與配置
- spring-boot-route(十六)使用logback生產日誌檔案Springboot
- Golang語言之Prometheus的日誌模組使用案例GolangPrometheus
- logback下日誌輸出前處理操作——以日誌脫敏為例
- Springboot 整合logback 日誌框架簡介Spring Boot框架
- Spring boot日誌---slf4j+logbackSpring Boot
- 禁用 Logback 中特定類的日誌記錄
- logminer 日誌分析案例
- logminer抽取日誌案例
- springboot專案配置logback日誌系統Spring Boot
- logback接入到阿里雲sls日誌服務阿里
- spring boot 日誌介紹 以及 logback配置示例Spring Boot
- logback日誌級別動態切換的終極方案(Java ASM使用)JavaASM
- .NetCore中的日誌(1)日誌元件解析NetCore元件
- Hyperf日誌檢視元件元件
- ASP.NET Core視覺化日誌元件使用ASP.NET視覺化元件
- Logback中使用TurboFilter實現日誌級別等內容的動態修改Filter
- springboot+logback日誌非同步資料庫Spring Boot非同步資料庫
- Java中的日誌管理:SLF4J與LogbackJava
- 今天講講Java中的日誌—logging、logbackJava
- Logback詳細整理,基於springboot的日誌配置Spring Boot
- 老闆下了死命令,要把日誌系統切換到Logback
- springboot logback配置mybatis 日誌以及多環境配置Spring BootMyBatis
- 今天講講Java中的日誌---logging、logbackJava
- 開源日誌記錄元件Log4Net的使用元件
- Logback配置檔案這麼寫,還愁不會整理日誌?