fix bugs
This commit is contained in:
38
blade-core-log4j2/pom.xml
Normal file
38
blade-core-log4j2/pom.xml
Normal file
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<parent>
|
||||
<artifactId>BladeX-Tool</artifactId>
|
||||
<groupId>org.springblade</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>blade-core-log4j2</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<version>${project.parent.version}</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springblade</groupId>
|
||||
<artifactId>blade-core-launch</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-log4j2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lmax</groupId>
|
||||
<artifactId>disruptor</artifactId>
|
||||
</dependency>
|
||||
<!-- Auto -->
|
||||
<dependency>
|
||||
<groupId>org.springblade</groupId>
|
||||
<artifactId>blade-core-auto</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: DreamLu (596392912@qq.com)
|
||||
*/
|
||||
package org.springblade.core.log4j2;
|
||||
|
||||
import org.springblade.core.auto.service.AutoService;
|
||||
import org.springblade.core.launch.service.LauncherService;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
|
||||
/**
|
||||
* 日志启动器
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
@AutoService(LauncherService.class)
|
||||
public class LogLauncherServiceImpl implements LauncherService {
|
||||
|
||||
@Override
|
||||
public void launcher(SpringApplicationBuilder builder, String appName, String profile, boolean isLocalDev) {
|
||||
System.setProperty("logging.config", String.format("classpath:log/log4j2_%s.xml", profile));
|
||||
// RocketMQ-Client 4.2.0 Log4j2 配置文件冲突问题解决:https://www.jianshu.com/p/b30ae6dd3811
|
||||
System.setProperty("rocketmq.client.log.loadconfig", "false");
|
||||
// RocketMQ-Client 4.3 设置默认为 slf4j
|
||||
System.setProperty("rocketmq.client.logUseSlf4j", "true");
|
||||
// 非本地 将 全部的 System.err 和 System.out 替换为log
|
||||
if (!isLocalDev) {
|
||||
System.setOut(LogPrintStream.out());
|
||||
System.setErr(LogPrintStream.err());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: DreamLu (596392912@qq.com)
|
||||
*/
|
||||
package org.springblade.core.log4j2;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 替换 系统 System.err 和 System.out 为log
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
@Slf4j
|
||||
public class LogPrintStream extends PrintStream {
|
||||
private final boolean error;
|
||||
|
||||
private LogPrintStream(boolean error) {
|
||||
super(error ? System.err : System.out);
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public static LogPrintStream out() {
|
||||
return new LogPrintStream(false);
|
||||
}
|
||||
|
||||
public static LogPrintStream err() {
|
||||
return new LogPrintStream(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String s) {
|
||||
if (error) {
|
||||
log.error(s);
|
||||
} else {
|
||||
log.info(s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写掉它,因为它会打印很多无用的新行
|
||||
*/
|
||||
@Override
|
||||
public void println() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(String x) {
|
||||
if (error) {
|
||||
log.error(x);
|
||||
} else {
|
||||
log.info(x);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStream printf(String format, Object... args) {
|
||||
if (error) {
|
||||
log.error(String.format(format, args));
|
||||
} else {
|
||||
log.info(String.format(format, args));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStream printf(Locale l, String format, Object... args) {
|
||||
if (error) {
|
||||
log.error(String.format(l, format, args));
|
||||
} else {
|
||||
log.info(String.format(l, format, args));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT" follow="true">
|
||||
<PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}"/>
|
||||
</Console>
|
||||
<RollingFile name="RollingFile"
|
||||
fileName="${logdir}/${appName}.log"
|
||||
filePattern="${logdir}/${appName}.%d{yyyy-MM-dd}.%i.log.gz">
|
||||
<PatternLayout pattern="[%d] [%thread] ${LOG_LEVEL_PATTERN} ${appName} ${sys:PID} %c %m%n"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" />
|
||||
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" />
|
||||
</Filters>
|
||||
<Policies>
|
||||
<SizeBasedTriggeringPolicy size="200MB" />
|
||||
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="30"/>
|
||||
</RollingFile>
|
||||
<!-- 只显示error级别的信息 -->
|
||||
<RollingFile name="RollingFileError"
|
||||
fileName="${logdir}/${appName}-error.log"
|
||||
filePattern="${logdir}/${appName}-error.%d{yyyy-MM-dd}.%i.log.gz">
|
||||
<PatternLayout pattern="[%d] [%thread] ${LOG_LEVEL_PATTERN} ${appName} ${sys:PID} %c %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
<Policies>
|
||||
<SizeBasedTriggeringPolicy size="200MB"/>
|
||||
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="30"/>
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
32
blade-core-log4j2/src/main/resources/log/log4j2_dev.xml
Normal file
32
blade-core-log4j2/src/main/resources/log/log4j2_dev.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration status="off" xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||
<Properties>
|
||||
<Property name="appName">${sys:spring.application.name}</Property>
|
||||
<Property name="logdir">${env:LOG_BASE:-logs}/${appName}</Property>
|
||||
<Property name="PID">????</Property>
|
||||
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
|
||||
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
|
||||
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
|
||||
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%C{36}.%M:%L}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
</Properties>
|
||||
<xi:include href="log4j2_appenders.xml">
|
||||
<xi:fallback/>
|
||||
</xi:include>
|
||||
<Loggers>
|
||||
<!-- 减少部分debug日志 -->
|
||||
<AsyncLogger name="org.springframework.context" level="WARN"/>
|
||||
<AsyncLogger name="org.springframework.beans" level="WARN"/>
|
||||
<AsyncLogger name="springfox.bean.validators" level="ERROR"/>
|
||||
<!-- 基础组件 -->
|
||||
<AsyncLogger name="RocketmqClient" level="WARN"/>
|
||||
<!-- mongo no sql -->
|
||||
<AsyncLogger name="org.springframework.data.mongodb.core" level="DEBUG"/>
|
||||
<!-- blade日志 -->
|
||||
<AsyncLogger name="org.springblade.core" level="INFO"/>
|
||||
<Root level="INFO" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="RollingFile"/>
|
||||
<AppenderRef ref="RollingFileError"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</configuration>
|
||||
31
blade-core-log4j2/src/main/resources/log/log4j2_ontest.xml
Normal file
31
blade-core-log4j2/src/main/resources/log/log4j2_ontest.xml
Normal file
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration status="off" xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||
<Properties>
|
||||
<Property name="appName">${sys:spring.application.name}</Property>
|
||||
<Property name="logdir">${env:LOG_BASE:-logs}/${appName}</Property>
|
||||
<Property name="PID">????</Property>
|
||||
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
|
||||
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
|
||||
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
|
||||
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
</Properties>
|
||||
<xi:include href="log4j2_appenders.xml">
|
||||
<xi:fallback/>
|
||||
</xi:include>
|
||||
<Loggers>
|
||||
<!-- 减少部分debug日志 -->
|
||||
<AsyncLogger name="org.springframework.context" level="WARN"/>
|
||||
<AsyncLogger name="org.springframework.beans" level="WARN"/>
|
||||
<AsyncLogger name="springfox.bean.validators" level="ERROR"/>
|
||||
<!-- 基础组件 -->
|
||||
<AsyncLogger name="RocketmqClient" level="WARN"/>
|
||||
<!-- blade日志 -->
|
||||
<AsyncLogger name="org.springblade.core" level="WARN" />
|
||||
<AsyncLogger name="org.springblade.core.http" level="INFO"/>
|
||||
<Root level="INFO" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="RollingFile"/>
|
||||
<AppenderRef ref="RollingFileError"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</configuration>
|
||||
29
blade-core-log4j2/src/main/resources/log/log4j2_prod.xml
Normal file
29
blade-core-log4j2/src/main/resources/log/log4j2_prod.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration status="off" xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||
<Properties>
|
||||
<Property name="appName">${sys:spring.application.name}</Property>
|
||||
<Property name="logdir">${env:LOG_BASE:-logs}/${appName}</Property>
|
||||
<Property name="PID">????</Property>
|
||||
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
|
||||
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
|
||||
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
|
||||
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
</Properties>
|
||||
<xi:include href="log4j2_appenders.xml">
|
||||
<xi:fallback/>
|
||||
</xi:include>
|
||||
<Loggers>
|
||||
<!-- 减少部分debug日志 -->
|
||||
<AsyncLogger name="springfox.bean.validators" level="ERROR"/>
|
||||
<!-- 基础组件 -->
|
||||
<AsyncLogger name="RocketmqClient" level="ERROR"/>
|
||||
<!-- blade日志 -->
|
||||
<AsyncLogger name="org.springblade.core" level="ERROR"/>
|
||||
<AsyncLogger name="org.springblade.core.http" level="INFO"/>
|
||||
<Root level="WARN" additivity="false">
|
||||
<AppenderRef ref="Console" />
|
||||
<AppenderRef ref="RollingFile" />
|
||||
<AppenderRef ref="RollingFileError" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</configuration>
|
||||
30
blade-core-log4j2/src/main/resources/log/log4j2_test.xml
Normal file
30
blade-core-log4j2/src/main/resources/log/log4j2_test.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration status="off" xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||
<Properties>
|
||||
<Property name="appName">${sys:spring.application.name}</Property>
|
||||
<Property name="logdir">${env:LOG_BASE:-logs}/${appName}</Property>
|
||||
<Property name="PID">????</Property>
|
||||
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
|
||||
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
|
||||
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
|
||||
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%C{36}.%M:%L}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
</Properties>
|
||||
<xi:include href="log4j2_appenders.xml">
|
||||
<xi:fallback/>
|
||||
</xi:include>
|
||||
<Loggers>
|
||||
<!-- 减少部分debug日志 -->
|
||||
<AsyncLogger name="org.springframework.context" level="WARN"/>
|
||||
<AsyncLogger name="org.springframework.beans" level="WARN"/>
|
||||
<AsyncLogger name="springfox.bean.validators" level="ERROR"/>
|
||||
<!-- 基础组件 -->
|
||||
<AsyncLogger name="RocketmqClient" level="WARN"/>
|
||||
<!-- blade日志 -->
|
||||
<AsyncLogger name="org.springblade.core" level="INFO"/>
|
||||
<Root level="INFO" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="RollingFile"/>
|
||||
<AppenderRef ref="RollingFileError"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user