国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

logback管理日志入門

bluesky / 1344人閱讀

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

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

依賴配置

logback的核心,logback-core

logback建議使用slf4j來管理日志,方便替換底層實現,要用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
                
            
        
    

配置文件

在項目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
            
        
    

    
    
        
        
        
        
    

包括日志文件的路徑,保存的時間,已經滾動的策略都在配置文件中配置,具體看注釋。配置文件是從欞楓的這篇博客里學習的,感謝。

測試

測試類:

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);
        }
    }
}

執行結果:

控制臺輸出:

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]

在項目路徑下多了一個logs文件夾,里面有3個文件,每個文件記錄對應的日志:

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);
        }
    }
}
日志輸出到數據庫 建表

首先在數據庫中建三張表,用來存儲日志記錄:

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中加入輸出到數據庫的appender,配置好數據源:


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

加入引用

加入數據庫驅動依賴

這一步很多人容易忘記,配置好相應的配置之后,要記得加入mysql驅動的依賴:


    mysql
    mysql-connector-java
    5.1.38

然后,日志就會輸出到數據表中了。

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65082.html

相關文章

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

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

    zebrayoung 評論0 收藏0
  • LogBack入門實踐

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

    馬永翠 評論0 收藏0
  • Activiti工作流從入門到入土:入門實例

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

    SwordFly 評論0 收藏0
  • 一次logback多線程調優的經歷

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

    khs1994 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<