Commit 11cf3853c7a0422e5f3a65c77136005d5c2d16ed

Authored by dark
1 parent 0997e456

定义logback的Layout以及Converter,用于日志打印

src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java
@@ -30,7 +30,9 @@ public class TracerUtils { @@ -30,7 +30,9 @@ public class TracerUtils {
30 } 30 }
31 } catch (Throwable ignore) {} 31 } catch (Throwable ignore) {}
32 // TODO 芋艿 多次调用会问题 32 // TODO 芋艿 多次调用会问题
33 - return UUID.randomUUID().toString(); 33 +
  34 + // TODO 麻薯 定义一个给外部扩展的接口,默认在未接入Skywalking时,输出UUID
  35 + return "UUID:" + UUID.randomUUID().toString();
34 } 36 }
35 37
36 } 38 }
src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java 0 → 100644
  1 +/*
  2 + * Licensed to the Apache Software Foundation (ASF) under one or more
  3 + * contributor license agreements. See the NOTICE file distributed with
  4 + * this work for additional information regarding copyright ownership.
  5 + * The ASF licenses this file to You under the Apache License, Version 2.0
  6 + * (the "License"); you may not use this file except in compliance with
  7 + * the License. You may obtain a copy of the License at
  8 + *
  9 + * http://www.apache.org/licenses/LICENSE-2.0
  10 + *
  11 + * Unless required by applicable law or agreed to in writing, software
  12 + * distributed under the License is distributed on an "AS IS" BASIS,
  13 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 + * See the License for the specific language governing permissions and
  15 + * limitations under the License.
  16 + *
  17 + */
  18 +
  19 +
  20 +package cn.iocoder.dashboard.framework.tracer.skywalking;
  21 +
  22 +import ch.qos.logback.classic.pattern.ClassicConverter;
  23 +import ch.qos.logback.classic.spi.ILoggingEvent;
  24 +import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
  25 +
  26 +/**
  27 + * Created by mashu on 2021/3/6.
  28 + */
  29 +public class LocalLogbackPatternConverter extends ClassicConverter {
  30 + /**
  31 + * 作为默认的方式, 从{@link TracerUtils}中获取traceId,
  32 + * 需要用这个去替代logback的Layout,
  33 + * 同时避免了sky-walking agent生效的情况下仍然输出定值的问题.
  34 + *
  35 + * @param iLoggingEvent the event
  36 + * @return the traceId: UUID, or the real traceId.
  37 + */
  38 + @Override
  39 + public String convert(ILoggingEvent iLoggingEvent) {
  40 + return TracerUtils.getTraceId();
  41 + }
  42 +}
src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java 0 → 100644
  1 +/*
  2 + * Licensed to the Apache Software Foundation (ASF) under one or more
  3 + * contributor license agreements. See the NOTICE file distributed with
  4 + * this work for additional information regarding copyright ownership.
  5 + * The ASF licenses this file to You under the Apache License, Version 2.0
  6 + * (the "License"); you may not use this file except in compliance with
  7 + * the License. You may obtain a copy of the License at
  8 + *
  9 + * http://www.apache.org/licenses/LICENSE-2.0
  10 + *
  11 + * Unless required by applicable law or agreed to in writing, software
  12 + * distributed under the License is distributed on an "AS IS" BASIS,
  13 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 + * See the License for the specific language governing permissions and
  15 + * limitations under the License.
  16 + *
  17 + */
  18 +
  19 +
  20 +package cn.iocoder.dashboard.framework.tracer.skywalking;
  21 +
  22 +import ch.qos.logback.classic.PatternLayout;
  23 +
  24 +/**
  25 + * Based on the logback-compoenent convert register mechanism,
  26 + * register {@link LocalLogbackPatternConverter} as a new convert, match to "tid".
  27 + * You can use "%tid" in logback config file, "Pattern" section.
  28 + * If sky-walking agent is not active mode, it will use UUID as tid.
  29 + * <p>
  30 + * Created by mashu on 2021/3/6.
  31 + */
  32 +public class TraceIdPatternLogbackLayout extends PatternLayout {
  33 + static {
  34 + defaultConverterMap.put("tid", LocalLogbackPatternConverter.class.getName());
  35 + }
  36 +}
src/main/resources/logback-spring.xml 0 → 100644
  1 +<configuration>   
  2 + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">     
  3 + <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
  4 + <layout class="cn.iocoder.dashboard.framework.tracer.skywalking.TraceIdPatternLogbackLayout">
  5 + <pattern>%d{ISO8601} | %tid | %thread | %-5level | %msg%n</pattern>
  6 + </layout>
  7 + </encoder>
  8 + </appender>
  9 + <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  10 + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  11 + <FileNamePattern>./logs/ruoyi-vue-pro-%d{yyyy-MM-dd_HH}.log</FileNamePattern>
  12 + <MaxHistory>3</MaxHistory>
  13 + </rollingPolicy>
  14 + <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
  15 + <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
  16 + <pattern>%d{ISO8601} | %tid | %thread | %-5level | %msg%n</pattern>
  17 + </layout>
  18 + </encoder>
  19 + <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  20 + <MaxFileSize>10MB</MaxFileSize>
  21 + </triggeringPolicy>
  22 + </appender>
  23 + <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
  24 + <discardingThreshold>0</discardingThreshold>
  25 + <queueSize>10</queueSize>
  26 + <appender-ref ref="FILE"/>
  27 + </appender>
  28 +
  29 + <springProfile name="dev">
  30 + <logger name="cn.iocoder.dashboard" level="INFO" additivity="false">
  31 + <appender-ref ref="STDOUT"/>
  32 + <appender-ref ref="ASYNC"/>
  33 + </logger>
  34 + </springProfile>
  35 + <springProfile name="local">
  36 + <logger name="cn.iocoder.dashboard" level="INFO" additivity="false">
  37 + <appender-ref ref="STDOUT"/>
  38 + </logger>
  39 + </springProfile>
  40 + <springProfile name="default">
  41 + <logger name="cn.iocoder.dashboard" level="INFO" additivity="false">
  42 + <appender-ref ref="STDOUT"/>
  43 + <appender-ref ref="ASYNC"/>
  44 + </logger>
  45 + </springProfile>
  46 +
  47 + <root level="DEBUG">      
  48 + <appender-ref ref="STDOUT"/>   
  49 + <appender-ref ref="ASYNC"/> 
  50 + </root>
  51 +
  52 + <logger name="cn.iocoder.dashboard" level="INFO">
  53 + <appender-ref ref="STDOUT"/>
  54 + </logger>
  55 +</configuration>
0 \ No newline at end of file 56 \ No newline at end of file