前言

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

配置log4j2

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

pom.xml

springboot 项目默认的是使用logback 的,所以我们想要使用log4j ,需要将原来的logback 框架屏蔽掉,再引入log4j.

首先我们在pom.xml 文件中加入

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions><!-- 去掉默认配置 -->
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-logging</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-log4j2</artifactId>
  14. </dependency>

编写log4j2.xml

  1. <Configuration status="WARN" monitorInterval="300" packages="cn.mastercom.cat">
  2. <properties>
  3. <property name="MtnoWebRoot" >${sys:user.dir}/logs</property>
  4. <property name="INFO_FILE">zlflovemm_log</property>
  5. <property name="ERROR_FILE">zlflovemm__error</property>
  6. </properties>
  7. <Appenders>
  8. <Console name="Console" target="SYSTEM_OUT">
  9. <ThresholdFilter level="ALL" onMatch="ACCEPT" onMismatch="DENY"/>
  10. <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
  11. </Console>
  12. <RollingRandomAccessFile name="infolog"
  13. fileName="${MtnoWebRoot}/${INFO_FILE}.log"
  14. filePattern="${MtnoWebRoot}/${INFO_FILE}-%d{yyyy-MM-dd}-%i.log">
  15. <PatternLayout
  16. pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%X{sessionID}] [%X{imei}] %-5level %logger{36} - %msg%n" />
  17. <!-- -->
  18. <Policies>
  19. <TimeBasedTriggeringPolicy interval="1" modulate="true" />
  20. <SizeBasedTriggeringPolicy size="2MB" />
  21. </Policies>
  22. <DefaultRolloverStrategy max="1000">
  23. <Delete basePath="${MtnoWebRoot}" maxDepth="1">
  24. <IfFileName glob="${INFO_FILE}*.log" />
  25. <IfLastModified age="30d" />
  26. </Delete>
  27. </DefaultRolloverStrategy>
  28. </RollingRandomAccessFile>
  29. <RollingRandomAccessFile name="errorlog"
  30. fileName="${MtnoWebRoot}/${ERROR_FILE}.log"
  31. filePattern="${MtnoWebRoot}/${ERROR_FILE}-%d{yyyy-MM-dd}-%i.log">
  32. <PatternLayout
  33. pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
  34. <Policies>
  35. <TimeBasedTriggeringPolicy interval="1" modulate="true" />
  36. <SizeBasedTriggeringPolicy size="200MB" />
  37. </Policies>
  38. <DefaultRolloverStrategy max="1000">
  39. <Delete basePath="${MtnoWebRoot}" maxDepth="1">
  40. <IfFileName glob="${INFO_FILE}*.log" />
  41. <IfLastModified age="30d" />
  42. </Delete>
  43. </DefaultRolloverStrategy>
  44. </RollingRandomAccessFile>
  45. <Async name="Async">
  46. <AppenderRef ref="infolog"/>
  47. <AppenderRef ref="errorlog"/>
  48. </Async>
  49. </Appenders>
  50. <Loggers>
  51. <asyncRoot level="INFO">
  52. <AppenderRef ref="infolog"/>
  53. <AppenderRef ref="errorlog" level="error"/>
  54. <AppenderRef ref="Console" />
  55. </asyncRoot>
  56. </Loggers>
  57. </Configuration>

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

application.properties 配置

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

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

打包外置配置文件

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

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

pom.xml 修改

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

  1. <build>
  2. <!--打包后的项目名称-->
  3. <resources>
  4. <resource>
  5. <directory>src/main/resources</directory>
  6. <targetPath>${project.build.directory}${file.separator}classes</targetPath>
  7. </resource>
  8. <resource>
  9. <directory>src/main/java</directory>
  10. <includes>
  11. <!--这里必须包含.xml否则Mybatis的xml无法打包-->
  12. <include>**/*.xml</include>
  13. </includes>
  14. <filtering>true</filtering>
  15. </resource>
  16. </resources>
  17. <plugins>
  18. <!--java编译插件-->
  19. <plugin>
  20. <groupId>org.apache.maven.plugins</groupId>
  21. <artifactId>maven-compiler-plugin</artifactId>
  22. <configuration>
  23. <source>1.8</source>
  24. <target>1.8</target>
  25. <encoding>UTF-8</encoding>
  26. <fork>true</fork>
  27. </configuration>
  28. </plugin>
  29. <!--打jar包的插件-->
  30. <plugin>
  31. <groupId>org.apache.maven.plugins</groupId>
  32. <artifactId>maven-jar-plugin</artifactId>
  33. <configuration>
  34. <archive>
  35. <manifest>
  36. <addClasspath>true</addClasspath>
  37. <classpathPrefix>lib</classpathPrefix>
  38. <!--程序启动入口-->
  39. <mainClass>com.quellan.zlflovemm.ZlflovemmApplication</mainClass>
  40. </manifest>
  41. <manifestEntries>
  42. <Class-Path>./</Class-Path>
  43. </manifestEntries>
  44. </archive>
  45. <excludes>
  46. <exclude>config/**</exclude>
  47. </excludes>
  48. </configuration>
  49. </plugin>
  50. <plugin>
  51. <artifactId>maven-assembly-plugin</artifactId>
  52. <configuration>
  53. <!--not append assembly id in release file name-->
  54. <appendAssemblyId>false</appendAssemblyId>
  55. <descriptors>
  56. <!--注意这里的路径-->
  57. <descriptor>src/main/build/package.xml</descriptor>
  58. </descriptors>
  59. </configuration>
  60. <executions>
  61. <execution>
  62. <id>make-assembly</id>
  63. <phase>package</phase>
  64. <goals>
  65. <goal>single</goal>
  66. </goals>
  67. </execution>
  68. </executions>
  69. </plugin>
  70. <!-- Docker -->
  71. <plugin>
  72. <groupId>com.spotify</groupId>
  73. <artifactId>docker-maven-plugin</artifactId>
  74. <version>1.0.0</version>
  75. <!-- 将插件绑定在某个phase执行 -->
  76. <executions>
  77. <execution>
  78. <id>build-image</id>
  79. <!-- 用户只需执行mvn package ,就会自动执行mvn docker:build -->
  80. <phase>package</phase>
  81. <goals>
  82. <goal>build</goal>
  83. </goals>
  84. </execution>
  85. </executions>
  86. <configuration>
  87. <!-- 指定生成的镜像名 -->
  88. <imageName>${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
  89. <!-- 指定标签 -->
  90. <imageTags>
  91. <imageTag>${project.version}</imageTag>
  92. </imageTags>
  93. <!-- 指定 Dockerfile 路径 -->
  94. <dockerDirectory>src/main/docker</dockerDirectory>
  95. <!-- 指定远程 docker api地址 -->
  96. <dockerHost>http://127.0.0.1:2375</dockerHost>
  97. <resources>
  98. <resource>
  99. <targetPath>/</targetPath>
  100. <!-- jar包所在的路径此处配置的对应target目录 -->
  101. <directory>${project.build.directory}</directory>
  102. <!-- 需要包含的jar包,这里对应的是Dockerfile中添加的文件名 -->
  103. <include>${project.build.finalName}.jar</include>
  104. </resource>
  105. </resources>
  106. </configuration>
  107. </plugin>
  108. </plugins>
  109. </build>

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

package.xml

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

内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  4. <id>package</id>
  5. <formats>
  6. <format>zip</format>
  7. </formats>
  8. <!-- 改为false不会出现两层相同的目录 -->
  9. <includeBaseDirectory>false</includeBaseDirectory>
  10. <fileSets>
  11. <fileSet>
  12. <directory>bin</directory>
  13. <outputDirectory>${file.separator}</outputDirectory>
  14. </fileSet>
  15. <fileSet>
  16. <directory>src/main/resources</directory>
  17. <outputDirectory>${file.separator}</outputDirectory>
  18. <excludes>
  19. <exclude>static/**</exclude>
  20. <exclude>templates/**</exclude>
  21. </excludes>
  22. </fileSet>
  23. <fileSet>
  24. <directory>${project.build.directory}</directory>
  25. <outputDirectory>${file.separator}</outputDirectory>
  26. <includes>
  27. <include>*.jar</include>
  28. </includes>
  29. </fileSet>
  30. </fileSets>
  31. <dependencySets>
  32. <dependencySet>
  33. <useProjectArtifact>true</useProjectArtifact>
  34. <outputDirectory>lib</outputDirectory>
  35. <scope>runtime</scope>
  36. <!--<unpack>false</unpack> -->
  37. <excludes>
  38. <!--<exclude>${project.name}-${project.version}</exclude> -->
  39. <exclude>${groupId}:${artifactId}</exclude>
  40. </excludes>
  41. </dependencySet>
  42. </dependencySets>
  43. </assembly>

测试

好啦,上面的已经配置好啦,我们来测试一下。

直接mvn package成功后会生成如下文件,包含jar 和zip 文件。



zip 文件解压后,就是我们第一次部署的文件,后面修改代码只用替换jar文件就可以了。

我们生成的日志文件

番外

好了,就说这么多啦

代码上传到github:

https://github.com/QuellanAn/zlflovemm

后续加油♡

欢迎大家关注个人公众号 "程序员爱酸奶"

分享各种学习资料,包含java,linux,大数据等。资料包含视频文档以及源码,同时分享本人及投递的优质技术博文。

如果大家喜欢记得关注和分享哟❤

十一、springboot 配置log4j2以及打包成zip文件的更多相关文章

  1. eclipse通过maven建立java se工程配置log4j,打包成zip,将jar包和配置文件分开,并以bat和sh文件启动java程序

    一.新建maven的java工程 1.eclipse里file-new-other,选择maven Project 2.选中 Use default Workspace location,然后 nex ...

  2. php将文件夹打包成zip文件

    function addFileToZip($path,$zip){    $handler=opendir($path); //打开当前文件夹由$path指定.    while(($filenam ...

  3. c# 把网络图片http://....png 打包成zip文件

    思路: 1.把网络图片下载到服务器本地. 2.读取服务器图片的文件流 3.使用zip帮助类,把图片文件流写进zip文件流. 4.如果是文件服务器,把zip文件流 推送文件服务器,生成zip的下载url ...

  4. 【原】Python用例:将指定文件或目录打包成zip文件

    #This Demo is used to compress files to .zip file #Base on Windows import os import time #The files ...

  5. PHP将多级目录打包成zip文件

    最近接触PHP,需要用到zip压缩,在网上搜索的一大堆,发现代码都不低于50行.  而且调用还很费事(基础太少看不懂).让我收获的是Php提供有一个ZipArchive类,并有如下方法. bool a ...

  6. Vue -- webpack 项目自动打包压缩成zip文件

    这段时间用 Vue2.0 开发项目,每次打包都会用到 npm run build 命令,但是每次部署时给后端发包都要手动zip压缩,这样一两次还行,但遇到项目板块测试和临时加急功能测试的时候,一天可能 ...

  7. springboot中使用freemarker生成word文档并打包成zip下载(简历)

    一.设计出的简历模板图以及给的简历小图标切图         二.按照简历模板图新建简历word文件 :${字段名},同时将图片插入到word中,并将建好的word文件另存为xml文件:    三.直 ...

  8. Springboot生成二维码并下载图片png支持打包成zip

    pom.xml <!--二维码--> <dependency> <groupId>com.google.zxing</groupId> <arti ...

  9. 将多张图片打包成zip包,一起上传

    1.前端页面 <div class="mod-body" id="showRW" style="text-align: center;font- ...

随机推荐

  1. php 下载图片并打包成Zip格式压缩包

    前言:最近公司有个需要下载多个图片并打包成压缩包的需求,下面来看看具体是怎么做的 1.没什么说的,懒得说啥,直接看代码 /** * 下载图片并生成压缩包 * @param $data 图片数组,一维 ...

  2. .NET手撸绘制TypeScript类图——上篇

    .NET手撸绘制TypeScript类图--上篇 近年来随着交互界面的精细化,TypeScript越来越流行,前端的设计也越来复杂,而类图正是用简单的箭头和方块,反映对象与对象之间关系/依赖的好方式. ...

  3. PHP failed to ptrace(PEEKDATA) pid 13659: Input/output error错误解决方法

    PHP failed to ptrace(PEEKDATA) pid 13659: Input/output error错误解决方法 现在改linux内核文件打开限制<pre>ulimit ...

  4. PHP根据ip获取地理位置(通过高德地图接口)

    PHP根据ip获取地理位置(通过高德地图接口)<pre>//restapi.amap.com/v3/ip?key=2004f145cf3a39a72e9ca70ca4b2a1dc& ...

  5. js正则匹配的出链接地址

    content为需要匹配的值 var b=/<a([\s]+|[\s]+[^<>]+[\s]+)href=(\"([^<>"\']*)\"| ...

  6. Vue img的src使用数据绑定不显示

    不少人在vue的开发中遇到这样一个问题: img的src属性绑定url变量,然而图片加载失败. <img src="{{ imgUrl }}"/> 原因:写法错误 解决 ...

  7. 020.掌握Pod-Pod基础使用

    一 Pod定义详解 1.1 完整Pod定义文件 apiVersion: v1 #必选,版本号,例如v1,版本号必须可以用 kubectl api-versions 查询到 kind: Pod #必选, ...

  8. 一个ip, 两个域名, 两个ssl, 访问多个不同的项目

    在前面一篇中说过, 入了好几个坑. 后来使用了nginx+tomcat配置的方式. 终于成功了. 因为头一次使用nginx, 不知道具体怎么操作, 于是我在操作的时候, 按照以下几个步骤执行的: 导航 ...

  9. 检测当前IE浏览器的版本

    检测当前IE浏览器的版本(注意:在非IE浏览器中是看不到效果的) 使用示例如下:低于IE8弹窗提示 <!--[if lte IE 8]><script>alert('您当前浏览 ...

  10. head first 设计模式第一章笔记

    设计模式是告诉我们如何组织类和对象以解决某种问题. 学习设计模式,也就是学习其他开发人员的经验与智慧,解决遇到的相同的问题. 使用模式的最好方式是:把模式装进脑子,然后在设计的时候,寻找何处可以使用它 ...