分享一个基于log42+skywalking+springboot的日志模板配置

版本

  • springboot 2.6.9
  • skywalking 8.7.0

关于skywalking的配置可以查看我的另一篇文章 《Kubernetes+SpirngCloud+SkyWalking实现链路追踪

Maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions><!-- 去掉springboot默认配置 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 引入log4j2依赖 -->
<dependency> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

<!-- skywalking-log依赖 -->
<dependency>
    <groupId>org.apache.skywalking</groupId>
    <artifactId>apm-toolkit-log4j-2.x</artifactId>
    <version>8.7.0</version>
</dependency>

log4j2-spring.xml(放在springboot项目的resources目录下会自动生效)

<?xml version="1.0" encoding="UTF-8"?>
<!--日志的级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!-- Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,
     你会看到log4j2内部各种详细输出。可以设置成OFF(关闭) 或 Error(只输出错误信息)。
-->
<!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
<configuration status="WARN" monitorInterval="30">

    <Properties>
        <Property name="log.path">/usr/local/logs</Property>
        <Property name="moduleName">${sys:appName}</Property>
        <Property name="podIp">${sys:podIp}</Property>
        <Property name="appEnv">${sys:appEnv}</Property>
        <Property name="logging.pattern">
            [%date{yyyy-MM-dd HH:mm:ss.SSS}][${appEnv}][${moduleName}][${podIp}][%level][%traceId][%thread][%C][%M][%line][%X{X-B3-TraceId} %X{X-B3-SpanId} %X{X-B3-ParentSpanId}]-%m%n
        </Property>
    </Properties>

    <Appenders>
        <!-- 输出控制台日志的配置 -->
        <Console name="Console" target="SYSTEM_OUT">
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <!-- 输出日志的格式 -->
            <PatternLayout pattern="${logging.pattern}"/>
        </Console>

        <RollingRandomAccessFile name="debugRollingFile" fileName="${log.path}/${moduleName}/${appEnv}_${moduleName}.log"
                                 filePattern="${log.path}/${moduleName}/backup/${appEnv}_${moduleName}.%d{yyyyMMddHH}.zip">
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout charset="UTF-8" pattern="${logging.pattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="30"/>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="errorRollingFile" fileName="${log.path}/${moduleName}/${appEnv}_${moduleName}.log"
                                 filePattern="${log.path}/${moduleName}/backup/${appEnv}_${moduleName}.%d{yyyyMMddHH}.zip">
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout charset="UTF-8" pattern="${logging.pattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="30"/>
        </RollingRandomAccessFile>

        <GRPCLogClientAppender name="grpc-log">
            <PatternLayout pattern="${logging.pattern}"/>
        </GRPCLogClientAppender>
    </Appenders>
    <Loggers>
        <logger name="RocketmqRemoting" level="warn" additivity="false"/>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="debugRollingFile"/>
            <AppenderRef ref="errorRollingFile"/>
            <AppenderRef ref="grpc-log"/>
        </Root>
    </Loggers>
</configuration>

创建应用启动成功的监听器,在监听器里面动态统统system变量传递log的变量

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {

    @Value("${spring.application.name}")
    public String appName;

    @Value("${spring.profiles.active}")
    public String appEnv;

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        System.setProperty("appName", appName);
        System.setProperty("appEnv", appEnv);
        String podIp = new InetUtils(new InetUtilsProperties()).findFirstNonLoopbackHostInfo().getIpAddress();
        if (!StringUtils.isBlank(podIp)) {
            System.setProperty("podIp", podIp);
        } else {
            System.setProperty("podIp", "0.0.0.0");
        }
    }

}

项目启动,即可观察到链路追踪日志已经成功配置。关于skywalking的配置可以查看我的另一篇文章 《Kubernetes+SpirngCloud+SkyWalking实现链路追踪

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迪八戈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值