logback、log4j2 sql日志
日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出
<!--输出到控制台-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<!--展示格式 layout-->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15} %line - %machine{${machine:-0}} %X{taskName:-0} - %msg%n</pattern>
</pattern>
</layout>
</appender>
<!-- 输出到滚动文件 -->
<appender name="preposition" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Append>true</Append>
<!-- 本地日志文件名称和路径 -->
<File>${logDir}/preposition/import.log</File>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15} %line - %machine{${machine:-0}} %X{taskName:-0} - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<!-- 本地最多保存几份滚动文件 -->
<maxIndex>5</maxIndex>
<FileNamePattern>${logDir}/preposition/preposition.log.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<!-- 滚动文件的大小 -->
<MaxFileSize>300MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!--root节点是对所有appender的管理,添加哪个appender就会打印哪个appender的日志-->
<!--root节点的level是总的日志级别控制,如果appender的日志级别设定比root的高,会按照appender的日志级别打印日志,-->
<!--如果appender的日志级别比root的低,会按照root设定的日志级别进行打印日志-->
<!--也就是说root设定的日志级别是最低限制,如果root设定级别为最高ERROR,那么所有appender只能打印最高级别的日志-->
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="preposition"/>
<appender-ref ref="ASYNC"/>
</root>
public static void main(String[] args) {
/**
* 禁用nutz日志输出,如果需要可以取消下面的注释。
*/
Logs.setAdapter(NOP_ADAPTER);
SpringApplication.run(XXXxx.class, args);
}
自定义变量%machine
public static void main(String[] args) {
PatternLayout.DEFAULT_CONVERTER_MAP.put("machine", MachineConverter.class.getName());
SpringApplication.run(XmhApplication.class, args);
}
public class MachineConverter extends ClassicConverter {
public static final String KEY_MACHINE = "myMachine";
public static final String DEFAULT_MACHINE = "0";
private static final Map<String, String> CACHE = new ConcurrentHashMap();
public String convert(ILoggingEvent event) {
String machine = CACHE.get(KEY_MACHINE);
if (machine == null) {
CACHE.put(KEY_MACHINE, "231");
String machine2 = CACHE.get(KEY_MACHINE);
return machine2 == null ? DEFAULT_MACHINE : machine2;
}
return machine;
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>manage</contextName>
<!--设置变量-->
<springProperty name="applicationName" scope="context" source="spring.application.name"/>
<property name="log.colorPattern" value="%magenta(%d{yyyy-MM-dd HH:mm:ss}) %highlight(%-5level) %yellow(%thread) %green(%logger)-****%machine**-[%c]-[%F:%L] %msg%n"/>
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5level %thread %logger-[%c]-[%F:%L] %msg%n"/>
<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.colorPattern}</pattern>
</encoder>
</appender>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15} %line - %machine{${machine:-0}} %X{taskName:-0} - %msg%n</pattern>
</encoder>
%X表示提取MDC中的值
如果设置级别为INFO,则优先级大于等于INFO级别(如:INFO、WARN、ERROR)的日志信息将可以被输出
log4j2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<appenders>
<Console name="console" target="SYSTEM_OUT ">
<PatternLayout pattern="%d{yyy MMM dd HH:mm:ss,SSS}: %p %t %c- %m%n"></PatternLayout>
</Console>
<RollingFile name="test" fileName="../logs/test.log" filePattern="../logs/test.log.%d{yyyyMMdd}.log">
<PatternLayout>
<Pattern>%d{yyy MMM dd HH:mm:ss,SSS}: %p %t %c{1}- %m%n</Pattern>
</PatternLayout>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismath="DENY"/>
<!-- 没隔一段时间产生一个新的日志 -->
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
</RollingFile>
</appenders>
<Loggers>
<logger name="cn.com" level="INFO">
<appender-ref ref="test"/>
</logger>
<root level="INFO">
<appender-ref ref="console"/>
</root>
</Loggers>
</Configuration>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.0</version>
</dependency>
<!--log4j到log4j2的连接包-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.13.0</version>
</dependency>
<!--非必须-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.0</version>
</dependency>
mybatis sql日志打印
logging.level.cn.com.xxx.dao=debug -- 数据库mybatis的sql日志需要设置为debug
Springboot 默认使用的是logback ,所以一定要在 pom 文件中 移除
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
<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-web</artifactId>
<exclusions><!-- 去掉默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
logging.level.cn.com.dao=@mytabis.logging.level@ 实现不同环境的不同级别设置
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
<mytabis.logging.level>debug</mytabis.logging.level>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
<mytabis.logging.level>info</mytabis.logging.level>
</properties>
</profile>
</profiles>
log4j.xml
如果使用debug模式,都要改为debug
<logger name="cn.com" level="debug">
<appender-ref ref="test"/>
</logger>
<root level="debug">
<appender-ref ref="console"/>
</root>
@Slf4j
public class XmhController {
public String hello() {
log.info();
Logger logger = LogManager.getLogger(SampleController.class);
Logger logger = LogManager.getLogger();
public String hello() {
logger.info();
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
</parent>
log4j-api等相关包里,要删除报错信息提示的class

浙公网安备 33010602011771号