From 91ef90ce60f57b101806cb4545e24cea9458180c Mon Sep 17 00:00:00 2001 From: Seoyoung Park Date: Fri, 3 Jan 2025 15:16:24 +0900 Subject: [PATCH] [#11906] Switch log4j2 configuration format from XML to YAML --- .../src/main/resources/log4j2-agent.yaml | 158 ++++++++++++++++++ agent-module/profiler-logging/pom.xml | 9 + .../profiler/logging/Log4j2LoggingSystem.java | 2 +- .../logging/Log4j2LoggingSystemYamlTest.java | 34 ++++ .../src/test/resources/log4j2-test.yaml | 29 ++++ 5 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 agent-module/agent/src/main/resources/log4j2-agent.yaml create mode 100644 agent-module/profiler-logging/src/test/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystemYamlTest.java create mode 100644 agent-module/profiler-logging/src/test/resources/log4j2-test.yaml diff --git a/agent-module/agent/src/main/resources/log4j2-agent.yaml b/agent-module/agent/src/main/resources/log4j2-agent.yaml new file mode 100644 index 000000000000..b8638b9dbbcd --- /dev/null +++ b/agent-module/agent/src/main/resources/log4j2-agent.yaml @@ -0,0 +1,158 @@ +Configuration: + status: INFO + + Properties: + Property: + - name: "logging_dir" + value: "${sys:pinpoint.log}/${sys:pinpoint.agentId}/" + - name: "rolling-date-format" + value: "%d{yyyy-MM-dd}" + - name: "backupsize" + value: "100m" + - name: "lastmodified" + value: "7d" + - name: "line_message_pattern" + value: "%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.}:%-3L -- %msg%n" + - name: "simple_message_pattern" + value: "%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.} -- %msg%n" + - name: "console_message_pattern" + value: "${simple_message_pattern}" + - name: "file_message_pattern" + value: "${simple_message_pattern}" + + Select: + - SystemPropertyArbiter: + propertyName: "pinpoint.profiler.profiles.active" + propertyValue: "local" + Property: + - name: "default-rollover-strategy-max" + value: "5" + - name: "logger-level" + value: "DEBUG" + - SystemPropertyArbiter: + propertyName: "pinpoint.profiler.profiles.active" + propertyValue: "release" + Property: + - name: "default-rollover-strategy-max" + value: "10" + - name: "logger-level" + value: "INFO" + - DefaultArbiter: + Property: + - name: "default-rollover-strategy-max" + value: "5" + - name: "logger-level" + value: "DEBUG" + + Appenders: + Console: + name: console + target: system_out + PatternLayout: + pattern: ${console_message_pattern} + Select: + - SystemPropertyArbiter: + propertyName: "pinpoint.profiler.profiles.active" + propertyValue: "local" + - SystemPropertyArbiter: + propertyName: "pinpoint.profiler.profiles.active" + propertyValue: "release" + ThresholdFilter: + level: WARN + onMatch: ACCEPT + onMismatch: DENY + - DefaultArbiter: + ThresholdFilter: + level: WARN + onMatch: ACCEPT + onMismatch: DENY + RollingFile: + - name: rollingFile + fileName: ${logging_dir}/pinpoint.log + filePattern: ${logging_dir}/pinpoint-${rolling-date-format}-%i.log + PatternLayout: + pattern: ${file_message_pattern} + Policies: + TimeBasedTriggeringPolicy: + interval: 1 + modulate: true + SizeBasedTriggeringPolicy: + size: ${backupsize} + DefaultRolloverStrategy: + max: ${default-rollover-strategy-max} + Delete: + basePath: ${logging_dir}/ + maxDepth: 1 + IfFileName: + glob: "pinpoint-*.log" + IfLastModified: + age: ${lastmodified} + - name: statRollingFile + fileName: ${logging_dir}/pinpoint_stat.log + filePattern: ${logging_dir}/pinpoint_stat-${rolling-date-format}-%i.log + PatternLayout: + pattern: ${file_message_pattern} + Policies: + TimeBasedTriggeringPolicy: + interval: 1 + modulate: true + SizeBasedTriggeringPolicy: + size: ${backupsize} + DefaultRolloverStrategy: + max: ${default-rollover-strategy-max} + Delete: + basePath: ${logging_dir}/ + maxDepth: 1 + IfFileName: + glob: "pinpoint_stat-*.log" + IfLastModified: + age: ${lastmodified} + + Loggers: + Logger: + - name: com.navercorp.pinpoint.metric + level: INFO + additivity: false + AppenderRef: + - ref: statRollingFile + - name: com.navercorp.pinpoint + level: ${logger-level} + additivity: false + AppenderRef: + - ref: console + - ref: rollingFile + - name: io.grpc + level: ${logger-level} + additivity: false + Select: + - SystemPropertyArbiter: + propertyName: "pinpoint.profiler.profiles.active" + propertyValue: "local" + AppenderRef: + - ref: console + - SystemPropertyArbiter: + propertyName: "pinpoint.profiler.profiles.active" + propertyValue: "release" + - DefaultArbiter: + AppenderRef: + - ref: console + AppenderRef: + - ref: rollingFile + - name: io.grpc.Context + level: INFO + additivity: false + AppenderRef: + - ref: console + - ref: rollingFile + - name: io.grpc.netty.Utils + level: INFO + additivity: false + AppenderRef: + - ref: console + - ref: rollingFile + + Root: + level: INFO + AppenderRef: + - ref: console + - ref: rollingFile \ No newline at end of file diff --git a/agent-module/profiler-logging/pom.xml b/agent-module/profiler-logging/pom.xml index fc9d68b90495..0f26ed88ec10 100644 --- a/agent-module/profiler-logging/pom.xml +++ b/agent-module/profiler-logging/pom.xml @@ -44,6 +44,15 @@ compile + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + + diff --git a/agent-module/profiler-logging/src/main/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystem.java b/agent-module/profiler-logging/src/main/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystem.java index cb89fc6dac30..6191d7ed998f 100644 --- a/agent-module/profiler-logging/src/main/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystem.java +++ b/agent-module/profiler-logging/src/main/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystem.java @@ -21,7 +21,7 @@ public class Log4j2LoggingSystem implements LoggingSystem { private static final String[] LOOKUP = { "log4j2-test.properties", "log4j2-test.xml", - "log4j2-agent.properties", "log4j2-agent.xml", + "log4j2-agent.properties", "log4j2-agent.yaml", }; private LoggerContext loggerContext; diff --git a/agent-module/profiler-logging/src/test/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystemYamlTest.java b/agent-module/profiler-logging/src/test/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystemYamlTest.java new file mode 100644 index 000000000000..ee946a24330a --- /dev/null +++ b/agent-module/profiler-logging/src/test/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystemYamlTest.java @@ -0,0 +1,34 @@ +package com.navercorp.pinpoint.profiler.logging; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.Test; + +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Objects; + +public class Log4j2LoggingSystemYamlTest { + + @Test + public void start() throws URISyntaxException { + URL resource = this.getClass().getClassLoader().getResource(""); + Objects.requireNonNull(resource, "resource"); + Path profilePath = Paths.get(resource.toURI()); + String log4jFile = profilePath.resolve("log4j2-test.yaml").toString(); + System.setProperty("log4j2.component.properties", "YamlConfigurationFactory"); + + + try (Log4j2LoggingSystem loggingSystem = new Log4j2LoggingSystem(profilePath)) { + loggingSystem.start(); + + Logger test = LogManager.getLogger("test"); + test.debug("test"); + + String configLocation = loggingSystem.getConfigLocation(); +// Assertions.assertEquals(log4jFile, configLocation); + } + } +} \ No newline at end of file diff --git a/agent-module/profiler-logging/src/test/resources/log4j2-test.yaml b/agent-module/profiler-logging/src/test/resources/log4j2-test.yaml new file mode 100644 index 000000000000..2174e05b70a8 --- /dev/null +++ b/agent-module/profiler-logging/src/test/resources/log4j2-test.yaml @@ -0,0 +1,29 @@ +Configuration: + status: INFO + + Properties: + Property: + - name: "console_message_pattern" + value: "%d{MM-dd HH:mm:ss.sss} [%15.15t] %clr{%-5level} %clr{%-40.40logger{1.}}{cyan}:%3L -- %msg%n" + - name: "file_message_pattern" + value: "%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.}:%3L -- %msg%n" + + Appenders: + Console: + name: console + target: system_out + PatternLayout: + pattern: ${file_message_pattern} + + Loggers: + Logger: + - name: com.navercorp.pinpoint + level: DEBUG + additivity: false + AppenderRef: + - ref: console + + Root: + level: DEBUG + AppenderRef: + - ref: console \ No newline at end of file