SpringBoot整合logback 和 fastJson
程序员文章站
2024-03-18 22:33:34
...
整合logback
1.导入依赖
不用额外导入,因为在 Springboot-web-starter 中整合了 logback 的依赖
2. 在resource中新建 logback.xml 的文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true">
<springProperty scope="context" name="LOG_LEVEL" source="logging.level.root"/>
<springProperty scope="context" name="APPNAME" source="logging.name"/>
<property name="LOG_HOME" value="/Users/******" />
<jmxConfigurator />
<appender name="error_file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${APPNAME}/web_error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/${APPNAME}/web_error_%d{yyyy-MM-dd}_%i.log.zip
</fileNamePattern>
<maxHistory>7</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>%date [%thread] [%file:%line] [%level %logger{0}] - %msg%n
</pattern>
</encoder>
</appender>
<appender name="warn_file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${APPNAME}/web_warn.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/${APPNAME}/web_warn_%d{yyyy-MM-dd}_%i.log.zip
</fileNamePattern>
<maxHistory>7</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>%date [%thread] [%file:%line] [%level %logger{0}] - %msg%n
</pattern>
</encoder>
</appender>
<appender name="info_file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${APPNAME}/web_info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/${APPNAME}/web_info_%d{yyyy-MM-dd}_%i.log.zip
</fileNamePattern>
<maxHistory>7</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${LOG_LEVEL}</level>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>%date [%thread] [%file:%line] [%level %logger{0}] - %msg%n
</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%date [%thread] [%level %logger{0}:%line] - %msg%n
</pattern>
</encoder>
</appender>
<!--异步输出-->
<appender name="async_info_log" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>512</queueSize>
<includeCallerData>true</includeCallerData>
<appender-ref ref="info_file"/>
</appender>
<root level="${LOG_LEVEL}">
<appender-ref ref="async_info_log" />
<appender-ref ref="warn_file" />
<appender-ref ref="error_file" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
- 在 properties 标签中,value 代表日志写入文件的地址路径
- Spring properties 表示要配置到 yml文件中的参数,例如绑定xml地址路径,指定日志级别
logging:
name: #日志名称
config: classpath:logback-spring.xml #logback.xml 的名称
level:
root: info
3. 使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(CouponService.class);
4. 日志级别
常用的四种:从高到低一次是 erro、warn、info、debug。
整合 fastJson
在实际场景中,应用的更多的是 阿里系的 fastJson框架
1.导入依赖
在 Maven repository中选择一种
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
2. 调用方法
- Json.ToJsonString()
推荐阅读
-
SpringBoot整合logback 和 fastJson
-
SpringBoot2.0整合jackson配置日期格式化和反序列化的实现
-
SpringBoot2.0整合jackson配置日期格式化和反序列化的实现
-
Java笔记:SpringBoot整合Mybatis需要注意的一些细节和ResultMap的用法
-
eclipse下整合springboot和mybatis的方法步骤
-
编辑器Ueditor和SpringBoot 的整合方法
-
编辑器Ueditor和SpringBoot 的整合方法
-
详解Springboot整合Dubbo之代码集成和发布
-
eclipse下整合springboot和mybatis的方法步骤
-
SpringBoot整合Swagger和Actuator的使用教程详解