相关文章推荐
犯傻的绿茶  ·  Alook浏览器老旧版本大全_所有历史官方版 ...·  6 月前    · 
强悍的大蒜  ·  2024年中国科学院理论物理研究所考研报名时 ...·  1 年前    · 
想出家的葫芦  ·  HY-【两山联游】:大美黄山.莲花佛国九华山 ...·  1 年前    · 
帅呆的长颈鹿  ·  -凯兴资本-案例展示·  1 年前    · 
文质彬彬的豆芽  ·  南京市建邺区2023年民办学校、热点公办学校 ...·  1 年前    · 
小百科  ›  十一、springboot 外观设计log4j2以及打包成zip文件-腾讯云开发者社区-腾讯云
docker directory log4j
越狱的排球
1 年前
作者头像
程序员爱酸奶
0 篇文章

十一、springboot 配置log4j2以及打包成zip文件

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 程序员爱酸奶 > 十一、springboot 配置log4j2以及打包成zip文件

十一、springboot 配置log4j2以及打包成zip文件

作者头像
程序员爱酸奶
发布 于 2020-02-29 17:26:32
760 0
发布 于 2020-02-29 17:26:32
举报

前言

其实我们前面已经配置了日志,但是最近总感觉日志日志格式看的不舒服,并且每次打包都是一个jar 文件,lib都包含在jar 中,每次做很小的修改都需要重新替换jar文件,jar文件会比较大,传输起来比较慢。所以做一些改进。

配置log4j2

好了,废话不多说了,先来在Springboot中配置log4j2吧。

pom.xml

springboot 项目默认的是使用logback 的,所以我们想要使用log4j ,需要将原来的logback 框架屏蔽掉,再引入log4j. 首先我们在pom.xml 文件中加入

 <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

编写log4j2.xml

<Configuration status="WARN" monitorInterval="300" packages="cn.mastercom.cat">
    <properties>
        <property name="MtnoWebRoot" >${sys:user.dir}/logs</property>
        <property name="INFO_FILE">zlflovemm_log</property>
        <property name="ERROR_FILE">zlflovemm__error</property>
    </properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <ThresholdFilter level="ALL" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <RollingRandomAccessFile name="infolog"
                                 fileName="${MtnoWebRoot}/${INFO_FILE}.log"
                                 filePattern="${MtnoWebRoot}/${INFO_FILE}-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout
                    pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%X{sessionID}] [%X{imei}] %-5level %logger{36} - %msg%n" />
            <!--  -->
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <SizeBasedTriggeringPolicy size="2MB" />
            </Policies>
            <DefaultRolloverStrategy max="1000">
                <Delete basePath="${MtnoWebRoot}" maxDepth="1">
                    <IfFileName glob="${INFO_FILE}*.log" />
                    <IfLastModified age="30d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="errorlog"
                                 fileName="${MtnoWebRoot}/${ERROR_FILE}.log"
                                 filePattern="${MtnoWebRoot}/${ERROR_FILE}-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout
                    pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <SizeBasedTriggeringPolicy size="200MB" />
            </Policies>
            <DefaultRolloverStrategy max="1000">
                <Delete basePath="${MtnoWebRoot}" maxDepth="1">
                    <IfFileName glob="${INFO_FILE}*.log" />
                    <IfLastModified age="30d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>
        <Async name="Async">
            <AppenderRef ref="infolog"/>
            <AppenderRef ref="errorlog"/>
        </Async>
    </Appenders>
    <Loggers>
        <asyncRoot level="INFO">
            <AppenderRef ref="infolog"/>
            <AppenderRef ref="errorlog" level="error"/>
            <AppenderRef ref="Console" />
        </asyncRoot>
    </Loggers>
</Configuration>

上面配置的是生成日志的格式,大家可以自行修改。以及配置了单个日志文件最大为200M ,只保留最近30天的文件。

application.properties 配置

#日志配置
logging.config=classpath:log4j2.xml
debug=false

实现上面这三步,就轻松的在项目中使用log4j日志啦。

打包外置配置文件

上面配置的日志,先不测试了,等这个打包的配置也配置好了,再来一起测试。

如果我们直接使用自带的mvn package 的话,会将我们依赖的jar 包已经配置文件统统打包成可运行的jar 文件。这样虽然方便,但是这样的话每次都需要重新打包,并且传输起来比较麻烦,所以我们就需要将lib 和配置文件从jar 文件中分离。这样项目修改了,只需要替换一下比较小的部分就可以了。

pom.xml 修改

打开我们的pom.xml 文件,最下面我们的中我们加入如下代码。因为我们的项目之前加入了打包成docker 镜像,所以整个的都贴出来,不需要打包成docker的可以去掉。

 <build>
        <!--打包后的项目名称-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}${file.separator}classes</targetPath>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <!--这里必须包含.xml否则Mybatis的xml无法打包-->
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <!--java编译插件-->
            <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>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <!--打jar包的插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib</classpathPrefix>
                            <!--程序启动入口-->
                            <mainClass>com.quellan.zlflovemm.ZlflovemmApplication</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./</Class-Path>
                        </manifestEntries>
                    </archive>
                    <excludes>
                        <exclude>config/**</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <!--not append assembly id in release file name-->
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <!--注意这里的路径-->
                        <descriptor>src/main/build/package.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Docker -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <!-- 将插件绑定在某个phase执行 -->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <!-- 用户只需执行mvn package ,就会自动执行mvn docker:build -->
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- 指定生成的镜像名 -->
                    <imageName>${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
                    <!-- 指定标签 -->
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                    </imageTags>
                    <!-- 指定 Dockerfile 路径 -->
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <!-- 指定远程 docker api地址 -->
                    <dockerHost>http://127.0.0.1:2375</dockerHost>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!-- jar包所在的路径此处配置的对应target目录 -->
                            <directory>${project.build.directory}</directory>
                            <!-- 需要包含的jar包,这里对应的是Dockerfile中添加的文件名 -->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

需要注意的是,如下两个地方,第一个di地方需要需改成我们自己项目的启动类。第二个地方需要配置我们的package.xml 文件路径。内容我们待会讲。

package.xml

我们在pom.xml 中配置好了后,我们在src/main 目录下创建一个build 包,然后在build 目录下创建package.xml 文件。路径就是上面配置的,大家可以按照自己的喜好来。 内容如下:

<?xml version="1.0" encoding="UTF-8"?>  
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">  
    <id>package</id>  
    <formats>  
        <format>zip</format>
    </formats>  
    <!-- 改为false不会出现两层相同的目录 -->
    <includeBaseDirectory>false</includeBaseDirectory>  
    <fileSets>  
        <fileSet>
            <directory>bin</directory>
            <outputDirectory>${file.separator}</outputDirectory>  
        </fileSet>  
        <fileSet>  
            <directory>src/main/resources</directory>  
            <outputDirectory>${file.separator}</outputDirectory>  
            <excludes>
                <exclude>static/**</exclude>
                <exclude>templates/**</exclude>
            </excludes>
        </fileSet>  
        <fileSet>  
            <directory>${project.build.directory}</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <includes>  
                <include>*.jar</include>  
            </includes>  
        </fileSet>  
    </fileSets>  
    <dependencySets>  
        <dependencySet>  
    <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>  
            <scope>runtime</scope>  
            <!--<unpack>false</unpack> -->
            <excludes>  
                <!--<exclude>${project.name}-${project.version}</exclude> -->
 
推荐文章
犯傻的绿茶  ·  Alook浏览器老旧版本大全_所有历史官方版安装下载_豌豆荚
6 月前
强悍的大蒜  ·  2024年中国科学院理论物理研究所考研报名时间是几号?-掌上考研
1 年前
想出家的葫芦  ·  HY-【两山联游】:大美黄山.莲花佛国九华山.水墨宏村.徽州文化 ...
1 年前
帅呆的长颈鹿  ·  -凯兴资本-案例展示
1 年前
文质彬彬的豆芽  ·  南京市建邺区2023年民办学校、热点公办学校电脑派位报名实施办法
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
小百科 - 百科知识指南
© 2024 ~ 沪ICP备11025650号