SpringBoot使用自定义的日志框架,日志的存储方式为json日志格式

首先SpringBoot实现了它自己的日志框架即默认日志Logback,可以直接在SpringBoot配置文件配置 比如logging.path=xx,logging.level.root=xx等等,启动项目会自动生成对应的日志文件并记录,但是不能实现json格式的日志存储,所以为了满足需要,我们可以选择自定义的的日志并配置,

SpringBoot自1.4以上的版本开始,不支持log4日志转而开始使用log4j2日志,要使用自定义的日志框架的话,必须把SpringBoot默认使用的日志排除,提示log4j2支持json日志格式

 log4j 2.x版本不再支持像1.x中的.properties后缀的文件配置方式,2.x版本配置文件后缀名只能为”.xml”,”.json”或者”.jsn”.

<dependency>
   <groupId>org.springframework.bootgroupId>
   <artifactId>spring-boot-starter-webartifactId>
   <exclusions>
      <exclusion>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-loggingartifactId>
      exclusion>
   exclusions>
dependency>

在引入log4j2 依赖

<dependency>
   <groupId>org.springframework.bootgroupId>
   <artifactId>spring-boot-starter-log4j2artifactId>
   <version>1.5.8.RELEASEversion>
dependency>


      .reporting.outputEncoding>UTF-8.reporting.outputEncoding>
      .version>1.8.version>
      .level>debug.level>
      .root.path>/data/springcloud/logs/xx/config/config.root.path>
      .error.path>/data/springcloud/logs/xx/config/config.error.path>

    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>

    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>

    dependency>
 <sourceDirectory>src/main/javasourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resourcesdirectory>
        <filtering>truefiltering>
        <includes>
          <include>**/*include>
        includes>
      resource>
    resources>

添加必须的插件

 <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <configuration>
            <encoding>utf-8encoding>
            <useDefaultDelimiters>trueuseDefaultDelimiters>
          configuration>
        plugin>

  <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
  plugin>

SpringBoot配置文件指定log4j.xml

logging.config=classpath:log4j2.xml

log4j2.xml的配置:
log4j2有很多的布局方式,其中 JosnLayout 表示记录方式是json方式


<configuration status="WARN">

    
    <Properties>
        <Property name="pattern">%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%l - %m%nProperty>
    Properties>


    <Loggers>
        <Root level="DEBUG">
            <AppenderRef ref="console">AppenderRef>
            <AppenderRef ref="rolling_file">AppenderRef>
        Root>
        
       
    Loggers>

    <Appenders>
        <Console name="console" target="SYSTEM_OUT" follow="true">
            
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>


             


            



        Console>
        
        <RollingFile name="rolling_file"
                     fileName="${log4j2.root.path}.log"
                     filePattern="${log4j2.root.path}_%d{yyyy-MM-dd}.log">
            <JsonLayout/>
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>

           


            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            Policies>


        RollingFile>

        <File name="file" fileName="${log4j2.package.path}.log">
            
            <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
           
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        File>

    Appenders>

configuration>

如果报下面这个错,说明SpringBoot自带的日志包为排除干净
解决方法:


<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>

      SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/repository/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.7/log4j-slf4j-impl-2.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
2018-08-29 14:20:29.261  INFO 13472 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@59474f18: startup date [Wed Aug 29 14:20:29 CST 2018]; root of context hierarchy
2018-08-29 14:20:29.614  INFO 13472 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-08-29 14:20:29.662  INFO 13472 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$738c003a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Logging system failed to initialize using configuration from 'classpath:log4j2.xml'
java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:17 - no applicable action for [Properties], current ElementPath  is [[configuration][Properties]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:34 - no applicable action for [Property], current ElementPath  is [[configuration][Properties][Property]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@12:14 - no applicable action for [Loggers], current ElementPath  is [[configuration][Loggers]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@13:29 - no applicable action for [Root], current ElementPath  is [[configuration][Loggers][Root]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@14:40 - no applicable action for [AppenderRef], current ElementPath  is [[configuration][Loggers][Root][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@16:37 - no applicable action for [AppenderRef], current ElementPath  is [[configuration][Loggers][Root][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:16 - no applicable action for [Appenders], current ElementPath  is [[configuration][Appenders]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@25:67 - no applicable action for [Console], current ElementPath  is [[configuration][Appenders][Console]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@27:79 - no applicable action for [ThresholdFilter], current ElementPath  is [[configuration][Appenders][Console][ThresholdFilter]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@57:114 - no applicable action for [File], current ElementPath  is [[configuration][Appenders][File]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@59:80 - no applicable action for [ThresholdFilter], current ElementPath  is [[configuration][Appenders][File][ThresholdFilter]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@69:61 - no applicable action for [JsonLayout], current ElementPath  is [[configuration][Appenders][File][JsonLayout]]
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:162)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:56)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:115)
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:308)
    at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:296)
    at com.yiche.UserApplication2.main(UserApplication2.java:34)
Exception in thread "main" java.lang.IllegalStateException: java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:17 - no applicable action for [Properties], current ElementPath  is [[configuration][Properties]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:34 - no applicable action for [Property], current ElementPath  is [[configuration][Properties][Property]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@12:14 - no applicable action for [Loggers], current ElementPath  is [[configuration][Loggers]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@13:29 - no applicable action for [Root], current ElementPath  is [[configuration][Loggers][Root]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@14:40 - no applicable action for [AppenderRef], current ElementPath  is [[configuration][Loggers][Root][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@16:37 - no applicable action for [AppenderRef], current ElementPath  is [[configuration][Loggers][Root][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:16 - no applicable action for [Appenders], current ElementPath  is [[configuration][Appenders]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@25:67 - no applicable action for [Console], current ElementPath  is [[configuration][Appenders][Console]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@27:79 - no applicable action for [ThresholdFilter], current ElementPath  is [[configuration][Appenders][Console][ThresholdFilter]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@57:114 - no applicable action for [File], current ElementPath  is [[configuration][Appenders][File]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@59:80 - no applicable action for [ThresholdFilter], current ElementPath  is [[configuration][Appenders][File][ThresholdFilter]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@69:61 - no applicable action for [JsonLayout], current ElementPath  is [[configuration][Appenders][File][JsonLayout]]
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:315)
    at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:296)
    at com.yiche.UserApplication2.main(UserApplication2.java:34)
Caused by: java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:17 - no applicable action for [Properties], current ElementPath  is [[configuration][Properties]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:34 - no applicable action for [Property], current ElementPath  is [[configuration][Properties][Property]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@12:14 - no applicable action for [Loggers], current ElementPath  is [[configuration][Loggers]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@13:29 - no applicable action for [Root], current ElementPath  is [[configuration][Loggers][Root]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@14:40 - no applicable action for [AppenderRef], current ElementPath  is [[configuration][Loggers][Root][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@16:37 - no applicable action for [AppenderRef], current ElementPath  is [[configuration][Loggers][Root][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:16 - no applicable action for [Appenders], current ElementPath  is [[configuration][Appenders]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@25:67 - no applicable action for [Console], current ElementPath  is [[configuration][Appenders][Console]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@27:79 - no applicable action for [ThresholdFilter], current ElementPath  is [[configuration][Appenders][Console][ThresholdFilter]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@57:114 - no applicable action for [File], current ElementPath  is [[configuration][Appenders][File]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@59:80 - no applicable action for [ThresholdFilter], current ElementPath  is [[configuration][Appenders][File][ThresholdFilter]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@69:61 - no applicable action for [JsonLayout], current ElementPath  is [[configuration][Appenders][File][JsonLayout]]
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:162)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:56)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:115)
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:308)
    ... 11 more

Process finished with exit code 1





参考:
https://www.cnblogs.com/baizhanshi/p/7503417.html

https://blog.csdn.net/rogger_chen/article/details/50587920

https://blog.csdn.net/zhou_kapenter/article/details/64123933

https://blog.csdn.net/yangzhuangqiu/article/details/49636661

http://book.51cto.com/art/201512/499576.htm

https://blog.csdn.net/wendingzhulu/article/details/52423522

SpringBoot自定义log4j2 日志
https://www.cnblogs.com/advancing/p/7922463.html

log4j2日志的布局和API
http://logging.apache.org/log4j/log4j-2.2/manual/layouts.html
http://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout

你可能感兴趣的:(日志)