Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#11290] Add application logger pattern full-replacement support. #11293

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,9 @@ profiler.log4j.logging.transactioninfo=false
# variables/aliases: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
#profiler.log4j.logging.pattern.replace.enable=false
#profiler.log4j.logging.pattern.replace.search=%m
#profiler.log4j.logging.pattern.replace.with="TxId:%X{PtxId} %m"
#profiler.log4j.logging.pattern.replace.with=TxId:%X{PtxId} %m
# the logger pattern would be fully replaced with the configured value if replace.enable=true
#profiler.log4j.logging.pattern.full_replace.with=

###########################################################
# log4j2 (guide url : https://github.com/naver/pinpoint/blob/master/doc/per-request_feature_guide.md)
Expand All @@ -1171,7 +1173,9 @@ profiler.log4j2.logging.transactioninfo=false
# variables/aliases: https://logging.apache.org/log4j/2.x/manual/layouts.html under section "Patterns"
#profiler.log4j2.logging.pattern.replace.enable=false
#profiler.log4j2.logging.pattern.replace.search=%message,%msg,%m
#profiler.log4j2.logging.pattern.replace.with="TxId:%X{PtxId} %msg"
#profiler.log4j2.logging.pattern.replace.with=TxId:%X{PtxId} %msg
# the logger pattern would be fully replaced with the configured value if replace.enable=true
#profiler.log4j2.logging.pattern.full_replace.with=

###########################################################
# logback (guide url : https://github.com/naver/pinpoint/blob/master/doc/per-request_feature_guide.md)
Expand All @@ -1184,7 +1188,9 @@ profiler.logback.logging.transactioninfo=false
# variables/aliases: https://github.com/qos-ch/logback/blob/master/logback-classic/src/main/java/ch/qos/logback/classic/PatternLayout.java#L54-L149
#profiler.logback.logging.pattern.replace.enable=false
#profiler.logback.logging.pattern.replace.search=%message,%msg,%m
#profiler.logback.logging.pattern.replace.with="TxId:%X{PtxId} %msg"
#profiler.logback.logging.pattern.replace.with=TxId:%X{PtxId} %msg
# the logger pattern would be fully replaced with the configured value if replace.enable=true
#profiler.logback.logging.pattern.full_replace.with=

###########################################################
# google httpclient
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2024 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.it.plugin.log4j;

import com.navercorp.pinpoint.it.plugin.utils.AgentPath;
import com.navercorp.pinpoint.it.plugin.utils.PluginITConstants;
import com.navercorp.pinpoint.test.plugin.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@PluginForkedTest
@PinpointAgent(AgentPath.PATH)
@Dependency({"log4j:log4j:[1.2.16,)", PluginITConstants.VERSION})
@ImportPlugin({"com.navercorp.pinpoint:pinpoint-log4j-plugin"})
@PinpointConfig("pinpoint-spring-bean-test.config")
@TransformInclude("org.apache.log4j.")
@JvmArgument("-Dprofiler.log4j.logging.pattern.full_replace.with=Log4jIT TxId:%X{PtxId} %m")
public class Log4jFullReplaceIT extends Log4jTestBase {

@Test
public void test() {
checkMDC();
}

@Test
public void patternUpdate() {
String log = checkPatternReplace();
Assertions.assertTrue(log.contains("Log4jIT"), "contains full-replace string Log4jIT");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@

import com.navercorp.pinpoint.it.plugin.utils.AgentPath;
import com.navercorp.pinpoint.it.plugin.utils.PluginITConstants;
import com.navercorp.pinpoint.it.plugin.utils.StdoutRecorder;
import com.navercorp.pinpoint.test.plugin.Dependency;
import com.navercorp.pinpoint.test.plugin.ImportPlugin;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointConfig;
import com.navercorp.pinpoint.test.plugin.PluginForkedTest;
import com.navercorp.pinpoint.test.plugin.TransformInclude;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@PluginForkedTest
Expand All @@ -35,63 +31,15 @@
@ImportPlugin({"com.navercorp.pinpoint:pinpoint-log4j-plugin"})
@PinpointConfig("pinpoint-spring-bean-test.config")
@TransformInclude("org.apache.log4j.")
public class Log4jIT {

private Logger logger;
public class Log4jIT extends Log4jTestBase {

@Test
public void test() {
Logger logger = Logger.getLogger(getClass());
logger.error("maru");

checkVersion(logger);

Assertions.assertNotNull(MDC.get("PtxId"), "txId");
Assertions.assertNotNull(MDC.get("PspanId"), "spanId");
checkMDC();
}

@Test
public void patternUpdate() {
final String msg = "pattern";


StdoutRecorder stdoutRecorder = new StdoutRecorder();
final String log = stdoutRecorder.record(new Runnable() {
@Override
public void run() {
logger = Logger.getLogger("patternUpdateLogback");
logger.error(msg);
}
});

System.out.println(log);
Assertions.assertNotNull(log, "log null");
Assertions.assertTrue(log.contains(msg), "contains msg");
Assertions.assertTrue(log.contains("TxId"), "contains TxId");

Assertions.assertNotNull(logger, "logger null");
checkVersion(logger);
checkPatternReplace();
}

private void checkVersion(Logger logger) {
final String location = getLoggerJarLocation(logger);
Assertions.assertNotNull(location, "location null");
System.out.println("Log4j jar location:" + location);

final String testVersion = getTestVersion();
Assertions.assertTrue(location.contains("/" + testVersion + "/"), "test version is not " + getTestVersion());
}

private String getTestVersion() {
final String[] threadInfo = Thread.currentThread().getName()
.replace(getClass().getName(), "")
.replace(" Thread", "")
.replace(" ", "").replace("log4j-", "").split(":");
return threadInfo[0];
}

private String getLoggerJarLocation(Object object) {
return object.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2018 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.it.plugin.log4j;

import com.navercorp.pinpoint.it.plugin.utils.StdoutRecorder;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.junit.jupiter.api.Assertions;

public class Log4jTestBase {

private Logger logger;

protected void checkMDC() {
Logger logger = Logger.getLogger(getClass());
logger.error("maru");

checkVersion(logger);
Object ptxId = MDC.get("PtxId");
Assertions.assertNotNull(ptxId, "TxId");
Assertions.assertInstanceOf(String.class, ptxId, "TxId type");
String id = (String) ptxId;
Assertions.assertTrue(id.contains("build.test.0^1"), "TxId value");
Assertions.assertNotNull(MDC.get("PspanId"), "spanId");
}

protected String checkPatternReplace() {
final String msg = "pattern";


StdoutRecorder stdoutRecorder = new StdoutRecorder();
final String log = stdoutRecorder.record(new Runnable() {
@Override
public void run() {
logger = Logger.getLogger("patternUpdateLogback");
logger.error(msg);
}
});

System.out.println(log);
Assertions.assertNotNull(log, "log null");
Assertions.assertTrue(log.contains(msg), "contains msg");
Assertions.assertTrue(log.contains("TxId"), "contains TxId");
Assertions.assertTrue(log.contains("build.test.0^1"), "contains TxId value");

Assertions.assertNotNull(logger, "logger null");
checkVersion(logger);
return log;
}

private void checkVersion(Logger logger) {
final String location = getLoggerJarLocation(logger);
Assertions.assertNotNull(location, "location null");
System.out.println("Log4j jar location:" + location);

final String testVersion = getTestVersion();
Assertions.assertTrue(location.contains("/" + testVersion + "/"), "test version is not " + getTestVersion());
}

private String getTestVersion() {
final String[] threadInfo = Thread.currentThread().getName()
.replace(getClass().getName(), "")
.replace(" Thread", "")
.replace(" ", "").replace("log4j-", "").split(":");
return threadInfo[0];
}

private String getLoggerJarLocation(Object object) {
return object.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.it.plugin.log4j2;

import com.navercorp.pinpoint.it.plugin.utils.AgentPath;
import com.navercorp.pinpoint.it.plugin.utils.PluginITConstants;
import com.navercorp.pinpoint.test.plugin.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@PluginForkedTest
@PinpointAgent(AgentPath.PATH)
@PinpointConfig("pinpoint-spring-bean-test.config")
@JvmVersion(8)
@Dependency({"org.apache.logging.log4j:log4j-core:[2.17.1,2.20)", PluginITConstants.VERSION})
@JvmArgument({"-DtestLoggerEnable=false", "-Dprofiler.log4j2.logging.pattern.full_replace.with=Log4j2IT TxId:%X{PtxId} %message"})
public class Log4J2PatternFullReplaceTestIT extends Log4j2PatternTestBase {

@Test
public void patternUpdate() {
String log = checkPatternUpdate();
Assertions.assertTrue(log.contains("Log4j2IT"), "contains full-replace string Log4j2IT");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.it.plugin.log4j2;

import com.navercorp.pinpoint.it.plugin.utils.AgentPath;
import com.navercorp.pinpoint.it.plugin.utils.PluginITConstants;
import com.navercorp.pinpoint.test.plugin.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@PluginForkedTest
@PinpointAgent(AgentPath.PATH)
@PinpointConfig("pinpoint-spring-bean-test.config")
@JvmVersion(11)
@Dependency({"org.apache.logging.log4j:log4j-core:[2.20,2.22]", PluginITConstants.VERSION})
@JvmArgument({"-DtestLoggerEnable=false", "-Dprofiler.log4j2.logging.pattern.full_replace.with=Log4j2IT TxId:%X{PtxId} %message"})
public class Log4J2PatternFullReplace_2_20_ITTest extends Log4j2PatternTestBase {

@Test
public void patternUpdate() {
String log = checkPatternUpdate();
Assertions.assertTrue(log.contains("Log4j2IT"), "contains full-replace string Log4j2IT");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.it.plugin.log4j2;

import com.navercorp.pinpoint.it.plugin.utils.AgentPath;
import com.navercorp.pinpoint.it.plugin.utils.PluginITConstants;
import com.navercorp.pinpoint.test.plugin.Dependency;
import com.navercorp.pinpoint.test.plugin.JvmArgument;
import com.navercorp.pinpoint.test.plugin.JvmVersion;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointConfig;
import com.navercorp.pinpoint.test.plugin.PluginForkedTest;
import org.junit.jupiter.api.Test;

@PluginForkedTest
@PinpointAgent(AgentPath.PATH)
@PinpointConfig("pinpoint-spring-bean-test.config")
@JvmVersion(8)
@Dependency({"org.apache.logging.log4j:log4j-core:[2.17.1,2.20)", PluginITConstants.VERSION})
@JvmArgument("-DtestLoggerEnable=false")
public class Log4J2PatternTestIT extends Log4j2PatternTestBase {

@Test
public void patternUpdate() {
checkPatternUpdate();
}

}
Loading
Loading