• 记录 springboot 日志输出到文件 遇到的坑


    概述: springboot + spring data jpa  整合后 日志输出到文件 始终不行,无奈查阅资料, 最后总算成功了

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.ztsj</groupId>
        <artifactId>ztsj-data</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.16.RELEASE</version>
        </parent>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Edgware.SR4</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                  <exclusion>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-logging</artifactId>
                  </exclusion>
               </exclusions>
            </dependency>
            <!-- springboot-log4j -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
            <!-- 加了该配置文件 会自动启动数据库 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>commons-configuration</groupId>
                <artifactId>commons-configuration</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
            
        </dependencies>
        
        <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
                <resource>
                    <directory>${project.basedir}/src/main/resources</directory>
                    <excludes>
                        <exclude>**/*.java</exclude>
                    </excludes>
                </resource>
            </resources>
    
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <dependencies>
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                            <version>1.2.7.RELEASE</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                    <exclude>**/Log4j2Plugins.dat</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring.handlers</resource>
                                    </transformer>
                                    <transformer
                                        implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                        <resource>META-INF/spring.factories</resource>
                                    </transformer>
                                    <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring.schemas</resource>
                                    </transformer>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                    <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>com.ims.Appstart</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>

    log4j2-spring.xml

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <properties>
            <!-- 文件输出格式 -->
            <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss,SSS} |-%-5level [%thread] %c [%L] -| %msg%n</property>
        </properties>
        <appenders>
            <!--添加一个控制台追加器-->
            <Console name="Console" target="system_out">
                <PatternLayout pattern="${PATTERN}" />
            </Console>
            <!--添加一个文本追加器,文件位于E:\logs目录下,名为HelloWorld.log-->
            <File name="File" fileName="E:\logs\api-service.log">
                <PatternLayout>
                    <pattern>${PATTERN}</pattern>
                </PatternLayout>
                <Policies>
                    <SizeBasedTriggeringPolicy size="20 MB" />
                </Policies>
            </File>
        </appenders>
        <loggers>
            <root level="info">
                <appenderref ref="File" />
           <!--为了直观,在控制天中也输出info级别的日志-->
                <appenderref ref="Console" />
            </root>
            <!--把org.springframework包下的所有日志输出到log文件,additivity="false"表示不输出到控制台-->
            <Logger name="org.springframework" level="info" additivity="true">
                <appenderRef ref="File" />
            </Logger>
        </loggers>
    </configuration>

    java,使用

    package com.ims.utils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    public class DateManagerUtils {
        private static final String FILE_PATH = "date.txt";
        
        private static final Logger logger = LoggerFactory.getLogger(DateManagerUtils.class);
        
        
        public static String log() throws IOException {
         logger.error("test");

    }
    }

    总结: springboot 需要排除内置日志, 然后是同log4j2, 貌似高版本不支持log4j了。  打包时 一定要 

      <exclude>**/Log4j2Plugins.dat</exclude>  加上这个, 不然日志有问题 ,
  • 相关阅读:
    记一份电网信息化建设企业信息分析平台规划
    2018年个人心灵历程记录
    OGG For Bigdata To Kafka同步问题处理
    Vue.js有赞商城(思路以及总结整理)
    汉诺塔-递归算法
    git合并分支
    js实现页面消息滚动效果
    chrome实现网页高清截屏(F12、shift+ctrl+p、capture)
    JS计算时间差
    Socket.io详解
  • 原文地址:https://www.cnblogs.com/bignew/p/12887345.html
Copyright © 2020-2023  润新知