spring整合日志框架

1.整个spring5框架的代码是基于Java8,运行时兼容jdk9,

2.spring5.0框架自带了通用的日志封装

(1)spring已经移除Log4configListener,官方建议使用log4j2

(2)spring5框架整合log4j2

3.添加依赖

  <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.33</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.17.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.5</version>
        </dependency>

4.添加log4j2.xml(xml名字只能是这个,不能用其他的

<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出-->
<!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
<configuration status="DEBUG" monitorInterval="30">
    <!--先定义所有的appender-->
    <appenders>
        <!--这个输出控制台的配置-->
        <console name="Console" target="SYSTEM_OUT">
            <!--输出日志的格式-->
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
        </console>
    </appenders>
        <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
        <File name="log" fileName="log/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>
    <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
        </root>
    </loggers>
</configuration>

5.测试,下面是日志信息

2022-03-15 17:26:20,597 main DEBUG Initializing configuration XmlConfiguration[location=D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml]
2022-03-15 17:26:20,628 main DEBUG Installed script engines
2022-03-15 17:26:22,663 main DEBUG Oracle Nashorn Version: 1.8.0_111, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
2022-03-15 17:26:22,663 main DEBUG PluginManager 'Core' found 97 plugins
2022-03-15 17:26:22,663 main DEBUG PluginManager 'Level' found 0 plugins
2022-03-15 17:26:22,666 main DEBUG Starting 1 Log4j2Scheduled threads
2022-03-15 17:26:22,668 main DEBUG PluginManager 'Lookup' found 13 plugins
2022-03-15 17:26:22,670 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2022-03-15 17:26:22,675 main DEBUG PluginManager 'TypeConverter' found 23 plugins
2022-03-15 17:26:22,686 main DEBUG PatternLayout$Builder(pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n", PatternSelector=null, Configuration(D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null")
2022-03-15 17:26:22,686 main DEBUG PluginManager 'Converter' found 38 plugins
2022-03-15 17:26:22,694 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2022-03-15 17:26:22,696 main DEBUG Not in a ServletContext environment, thus not loading WebLookup plugin.
2022-03-15 17:26:22,697 main DEBUG PluginManager 'Converter' found 38 plugins
2022-03-15 17:26:22,698 main DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2022-03-15 17:26:22,698 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false-2
2022-03-15 17:26:22,703 main DEBUG ConsoleAppender$Builder(PatternLayout([%d{HH:mm:ss:SSS}] [%p] - %l - %m%n), Filter=null, target="SYSTEM_OUT", name="Console", follow="null", ignoreExceptions="null")
2022-03-15 17:26:22,704 main DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2022-03-15 17:26:22,704 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false
2022-03-15 17:26:22,705 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2022-03-15 17:26:22,705 main DEBUG createAppenders(={Console})
2022-03-15 17:26:22,705 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2022-03-15 17:26:22,706 main DEBUG PatternLayout$Builder(pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n", PatternSelector=null, Configuration(D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null")
2022-03-15 17:26:22,707 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.FileAppender].
2022-03-15 17:26:22,711 main DEBUG createAppender(fileName="log/test.log", append="false", locking="null", name="log", immediateFlush="null", ignoreExceptions="null", bufferedIo="null", bufferSize="null", PatternLayout(%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n), Filter=null, advertise="null", advertiseUri="null", Configuration(D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml))
2022-03-15 17:26:22,714 main DEBUG Starting FileManager log/test.log
2022-03-15 17:26:22,714 main ERROR Unknown object "File" of type org.apache.logging.log4j.core.appender.FileAppender is ignored: try nesting it inside one of: ["Appenders", "Loggers", "Properties", "Scripts", "CustomLevels"].
2022-03-15 17:26:22,721 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2022-03-15 17:26:22,722 main DEBUG createAppenderRef(ref="Console", level="null", Filter=null)
2022-03-15 17:26:22,722 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2022-03-15 17:26:22,723 main DEBUG createLogger(additivity="null", level="INFO", includeLocation="null", ={Console}, ={}, Configuration(D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml), Filter=null)
2022-03-15 17:26:22,725 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2022-03-15 17:26:22,725 main DEBUG createLoggers(={root})
2022-03-15 17:26:22,726 main DEBUG Configuration XmlConfiguration[location=D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml] initialized
2022-03-15 17:26:22,726 main DEBUG Starting configuration XmlConfiguration[location=D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml]
2022-03-15 17:26:22,728 main DEBUG Started configuration XmlConfiguration[location=D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml] OK.
2022-03-15 17:26:22,728 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false-1
2022-03-15 17:26:22,729 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@5ba23b66 OK
2022-03-15 17:26:22,768 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2
2022-03-15 17:26:22,771 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=StatusLogger
2022-03-15 17:26:22,772 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=ContextSelector
2022-03-15 17:26:22,773 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=
2022-03-15 17:26:22,774 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=Console
2022-03-15 17:26:22,777 main DEBUG Reconfiguration complete for context[name=18b4aac2] at URI D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@26b3fd41) with optional ClassLoader: null
2022-03-15 17:26:22,777 main DEBUG Shutdown hook enabled. Registering a new one.
2022-03-15 17:26:22,778 main DEBUG LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@26b3fd41] started OK.
赵华
2022-03-15 17:26:23,317 pool-1-thread-1 DEBUG Stopping LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@26b3fd41]
2022-03-15 17:26:23,317 pool-1-thread-1 DEBUG Stopping LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@26b3fd41]...
2022-03-15 17:26:23,317 pool-1-thread-1 DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false
2022-03-15 17:26:23,317 pool-1-thread-1 DEBUG Stopping Log4j2Scheduled threads.
2022-03-15 17:26:23,317 pool-1-thread-1 DEBUG Stopped XmlConfiguration[location=D:\spring-study\spring-09-log4j2\target\classes\log4j2.xml] OK
2022-03-15 17:26:23,317 pool-1-thread-1 DEBUG Stopped LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@26b3fd41]...

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值