成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

logback管理日志入門

bluesky / 1353人閱讀

摘要:是由創(chuàng)始人設(shè)計(jì)的又一個(gè)開源日志組件。此外完整實(shí)現(xiàn)使你可以很方便地更換成其它日志系統(tǒng)如或。訪問模塊與容器集成提供通過來訪問日志的功能。依賴配置的核心,包建議使用來管理日志,方便替換底層實(shí)現(xiàn),要用,就在依賴中加入包和包。

Logback是由log4j創(chuàng)始人設(shè)計(jì)的又一個(gè)開源日志組件。logback當(dāng)前分成三個(gè)模塊:logback-core,logback-classic和logback-access。logback-core是其它兩個(gè)模塊的基礎(chǔ)模塊。logback-classic是log4j的一個(gè)改良版本。此外logback-classic完整實(shí)現(xiàn)SLF4J API使你可以很方便地更換成其它日志系統(tǒng)如log4j或JDK14 Logging。logback-access訪問模塊與Servlet容器集成提供通過Http來訪問日志的功能。

依賴配置

logback的核心,logback-core

logback建議使用slf4j來管理日志,方便替換底層實(shí)現(xiàn),要用slf4j,就在依賴中加入logback-classic包和slf4j-api包。

加入junit測試。

pom.xml:


    4.0.0
    com.fengyuan
    logback-demo
    0.0.1-SNAPSHOT

    
        
            org.slf4j
            slf4j-api
            1.7.12
        
        
            ch.qos.logback
            logback-core
            1.1.2
        
        
            ch.qos.logback
            logback-classic
            1.1.3
        
        
            junit
            junit
            4.12
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

配置文件

在項(xiàng)目classpath下加入logback.xml:



    
    
    
        
            %d{yyyy-MM-dd HH:mm:ss} [%level] - %m%n
        
    

    
    
    
        
        logs/error.log 
        
            %d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n
        
        
            ERROR
            ACCEPT
            DENY
        
        
            error.%d{yyyy-MM-dd}.log.zip
            
            30
        
    

    
    
    
        logs/info.log
        
            %d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n
        
        
            INFO
            ACCEPT
            DENY
        
        
            info.%i.log
            1
            3
        
        
            5MB
        
    

    
    
    
        logs/debug.log
        
            %d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n
        
        
            DEBUG
            ACCEPT
            DENY
        
        
            debug-%d{yyyy-MM-dd}.%i.log
            
                100MB
            
        
    

    
    
        
        
        
        
    

包括日志文件的路徑,保存的時(shí)間,已經(jīng)滾動(dòng)的策略都在配置文件中配置,具體看注釋。配置文件是從欞楓的這篇博客里學(xué)習(xí)的,感謝。

測試

測試類:

package com.fengyuan.client;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Main {
    
    private static Logger log = LoggerFactory.getLogger(Main.class);
    
    @Test
    public void testLogBack() {
        log.debug("I am debug");
        log.info("I am info");
        log.error("I am error");
        
        try {
            throw new IllegalStateException("I am exception");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

執(zhí)行結(jié)果:

控制臺(tái)輸出:

10:35:19,739 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
10:35:19,739 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
10:35:19,739 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/scm-workspace/logback-demo/target/classes/logback.xml]
10:35:19,803 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 30 seconds
10:35:19,803 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[D:scm-workspacelogback-demo	argetclasseslogback.xml]] every 30 seconds. 
10:35:19,803 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
10:35:19,810 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
10:35:19,812 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
10:35:19,826 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
10:35:19,848 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
10:35:19,850 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [ERROR-OUT]
10:35:19,854 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
10:35:19,866 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use zip compression
10:35:19,868 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern error.%d{yyyy-MM-dd}.log for the active file
10:35:19,875 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is "yyyy-MM-dd" from file name pattern "error.%d{yyyy-MM-dd}.log.zip".
10:35:19,875 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
10:35:19,877 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Fri Aug 26 10:35:19 CST 2016
10:35:19,879 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ERROR-OUT] - Active log file name: logs/error.log
10:35:19,879 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ERROR-OUT] - File property is set to [logs/error.log]
10:35:19,880 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
10:35:19,880 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [INFO-OUT]
10:35:19,881 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
10:35:19,884 |-INFO in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@234bef66 - No compression will be used
10:35:19,887 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[INFO-OUT] - Active log file name: logs/info.log
10:35:19,887 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[INFO-OUT] - File property is set to [logs/info.log]
10:35:19,887 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
10:35:19,887 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [DEBUG-OUT]
10:35:19,888 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
10:35:19,893 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
10:35:19,893 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern debug-%d{yyyy-MM-dd}.%i.log for the active file
10:35:19,894 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@39fb3ab6 - The date pattern is "yyyy-MM-dd" from file name pattern "debug-%d{yyyy-MM-dd}.%i.log".
10:35:19,894 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@39fb3ab6 - Roll-over at midnight.
10:35:19,894 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@39fb3ab6 - Setting initial period to Fri Aug 26 10:35:19 CST 2016
10:35:19,895 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[DEBUG-OUT] - Active log file name: logs/debug.log
10:35:19,895 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[DEBUG-OUT] - File property is set to [logs/debug.log]
10:35:19,895 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.db.DBAppender]
10:35:19,899 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [DB]
10:35:20,088 |-INFO in ch.qos.logback.core.db.DriverManagerConnectionSource@63753b6d - Driver name=MySQL Connector Java
10:35:20,088 |-INFO in ch.qos.logback.core.db.DriverManagerConnectionSource@63753b6d - Driver version=mysql-connector-java-5.1.38 ( Revision: fe541c166cec739c74cc727c5da96c1028b4834a )
10:35:20,088 |-INFO in ch.qos.logback.core.db.DriverManagerConnectionSource@63753b6d - supportsGetGeneratedKeys=true
10:35:20,090 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
10:35:20,091 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
10:35:20,091 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [ERROR-OUT] to Logger[ROOT]
10:35:20,091 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [INFO-OUT] to Logger[ROOT]
10:35:20,091 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [DEBUG-OUT] to Logger[ROOT]
10:35:20,091 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
10:35:20,092 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@731a74c - Registering current configuration as safe fallback point
2016-08-26 10:35:20 [DEBUG] - I am debug
2016-08-26 10:35:20 [INFO] - I am info
2016-08-26 10:35:20 [ERROR] - I am error
2016-08-26 10:35:20 [ERROR] - I am exception
java.lang.IllegalStateException: I am exception
    at com.fengyuan.client.Main.testLogBack(Main.java:19) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) [junit-4.12.jar:4.12]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) [junit-4.12.jar:4.12]
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) [junit-4.12.jar:4.12]
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) [junit-4.12.jar:4.12]
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]

在項(xiàng)目路徑下多了一個(gè)logs文件夾,里面有3個(gè)文件,每個(gè)文件記錄對應(yīng)的日志:

Lombok

可以借助Lombok來簡化代碼的編寫。
有了Lombok,只需要在要寫日志的類中加入@Slf4j等注解,不用再寫private static Logger log = LoggerFactory.getLogger(Main.class);

使用Lombok后的測試類:

package com.fengyuan.client;

import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {
    
    @Test
    public void testLogBack() {
        log.debug("I am debug");
        log.info("I am info");
        log.error("I am error");
        
        try {
            throw new IllegalStateException("I am exception");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
日志輸出到數(shù)據(jù)庫 建表

首先在數(shù)據(jù)庫中建三張表,用來存儲(chǔ)日志記錄:

BEGIN;
DROP TABLE IF EXISTS logging_event_property;
DROP TABLE IF EXISTS logging_event_exception;
DROP TABLE IF EXISTS logging_event;
COMMIT;

BEGIN;
CREATE TABLE logging_event( 
timestmp BIGINT NOT NULL, 
formatted_message  TEXT NOT NULL, 
logger_name VARCHAR(254) NOT NULL, 
level_string VARCHAR(254) NOT NULL, 
thread_name  VARCHAR(254), 
reference_flag SMALLINT, 
arg0 VARCHAR(254),
arg1 VARCHAR(254),
arg2 VARCHAR(254), 
arg3 VARCHAR(254), 
caller_filename VARCHAR(254) NOT NULL, 
caller_class VARCHAR(254) NOT NULL, 
caller_method VARCHAR(254) NOT NULL, 
caller_line CHAR(4) NOT NULL, 
event_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
COMMIT;

BEGIN;
CREATE TABLE logging_event_property( 
event_id BIGINT NOT NULL, 
mapped_key VARCHAR(254) NOT NULL, 
mapped_value TEXT, 
PRIMARY KEY(event_id, mapped_key), 
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
);
COMMIT;

BEGIN;
CREATE TABLE logging_event_exception (
event_id BIGINT NOT NULL, 
i SMALLINT NOT NULL, 
trace_line VARCHAR(254) NOT NULL, 
PRIMARY KEY(event_id, i), 
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)    
);
COMMIT;
配置

logback.xml中加入輸出到數(shù)據(jù)庫的appender,配置好數(shù)據(jù)源:


    
        
            com.mysql.jdbc.Driver
            jdbc:mysql://localhost:3306/test
            root
            123456
        
    

加入引用

加入數(shù)據(jù)庫驅(qū)動(dòng)依賴

這一步很多人容易忘記,配置好相應(yīng)的配置之后,要記得加入mysql驅(qū)動(dòng)的依賴:


    mysql
    mysql-connector-java
    5.1.38

然后,日志就會(huì)輸出到數(shù)據(jù)表中了。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65082.html

相關(guān)文章

  • 強(qiáng)推!大牛程序員必備的Java日志框架,性能無敵

    摘要:本文要來分享給大家程序員最常用的日志框架組件。沒有基礎(chǔ)的同學(xué)也不要著急,這套教程覆蓋了目前所有的日志框架,只要你學(xué),就一定用得到,先收藏,以備不時(shí)之需。 作為一名Java程序員,我們開發(fā)了很多Java應(yīng)用程序,包括桌面應(yīng)用、WEB應(yīng)用以及移動(dòng)應(yīng)用。然而日志系統(tǒng)是一個(gè)成熟Java應(yīng)用所必不可少的。在開發(fā)和調(diào)試階段,日志可以幫...

    zebrayoung 評(píng)論0 收藏0
  • LogBack入門實(shí)踐

    摘要:日志名的從開始,自動(dòng)遞增就是指拋出的異常,是顯示全部,如果在中寫入數(shù)字,則表示展示多少行重點(diǎn)來了,上邊都是輸出源。 一、簡介 LogBack是一個(gè)日志框架,它是Log4j作者Ceki的又一個(gè)日志組件。 LogBack,Slf4j,Log4j之間的關(guān)系 slf4j是The Simple Logging Facade for Java的簡稱,是一個(gè)簡單日志門面抽象框架,它本身只提供了日志F...

    馬永翠 評(píng)論0 收藏0
  • Activiti工作流從入門到入土:入門實(shí)例

    摘要:二環(huán)境準(zhǔn)備編譯器選擇這里我們使用進(jìn)行工作流開發(fā),雖然對于工作流的友好度不是很好,因?yàn)闀?huì)有一些小的,但是,對于的開發(fā)還是非常的好的。新建后出現(xiàn)下面的編輯頁面到現(xiàn)在,編輯插件就準(zhǔn)備好了。 文章源碼托管:https://github.com/OUYANGSIHA...歡迎 star ?。?! 一、前言 在上一節(jié)中我們對activiti進(jìn)行了基本的介紹activiti進(jìn)行了基本的介紹,同時(shí)介紹了...

    SwordFly 評(píng)論0 收藏0
  • 一次logback多線程調(diào)優(yōu)的經(jīng)歷

    摘要:由于不是線程安全的,故在方法上增加了同步操作,造成競爭等待。至此,整個(gè)多線程調(diào)優(yōu)結(jié)束,通過充分優(yōu)化同步競爭的方式,最終使得分線程記錄日志的性能比最原始的多線程寫同一文件提高了倍去鎖提高到倍,替換提高倍 背景 ??在一次項(xiàng)目的性能調(diào)優(yōu)中,發(fā)現(xiàn)出現(xiàn)競爭瓶頸,導(dǎo)致在資源未使用滿的情況下,TPS已經(jīng)無法提升。祭起JMC(JAVA MISSON CONTROL)飛行記錄器大法后,發(fā)現(xiàn)線程集中等待...

    khs1994 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<