Skip to content

Commit

Permalink
[#11906] Switch log4j2 configuration format from XML to YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
intr3p1d committed Jan 6, 2025
1 parent c03696c commit 91ef90c
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 1 deletion.
158 changes: 158 additions & 0 deletions agent-module/agent/src/main/resources/log4j2-agent.yaml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions agent-module/profiler-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
29 changes: 29 additions & 0 deletions agent-module/profiler-logging/src/test/resources/log4j2-test.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 91ef90c

Please sign in to comment.