2019.3.5 控制檯輸出log4j得有些報錯信息

報錯內容以下

log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

解決辦法以下:

建立一個log4j.properties

### 設置###
log4j.rootLogger = off

### 輸出信息到控制擡 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 輸出DEBUG 級別以上的日誌到=D://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 輸出ERROR 級別以上的日誌到=D://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =D://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n


############ 輸出到數據庫logginfo表############
log4j.appender.database.Threshold=info
#定義什麼級別的錯誤將寫入到數據庫中
log4j.appender.database.BufferSize=1
#設置緩存大小,就是當有1條日誌信息時才往數據庫插一次
log4j.appender.database=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.database.driver=oracle.jdbc.OracleDriver
#設置要將日誌插入到數據庫的驅動
log4j.appender.database.URL=jdbc:oracle:thin:@119.23.211.14:1521:orcl
log4j.appender.database.user=Legend
log4j.appender.database.password=Legend
log4j.appender.database.sql=insert into loginfo (Class,Mothod,createTime,LogLevel,MSG) values ('%C','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m')
log4j.appender.database.layout=org.apache.log4j.PatternLayout

建立一個log4j2.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
        </Console>
 
        <RollingFile name="RollingFile" filename="log/test.log"
            filepattern="${logPath}/%d{YYYYMMddHHmmss}-fargo.log">
            <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingFile>
 
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="RollingFile" />
        </Root>
    </Loggers>
</Configuration>

將上面的兩個文件放入到項目的src目錄下面spring

測試

很舒服哈哈哈哈 歡迎關注

相關文章
相關標籤/搜索