Skip to content

Commit 6081d5c

Browse files
authored
Change logback logging level at runtime for Linux consumption plan (#1603)
1 parent 249cff7 commit 6081d5c

6 files changed

Lines changed: 37 additions & 0 deletions

File tree

agent/agent-tooling/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ dependencies {
6666
exclude group: 'commons-logging', module: 'commons-logging'
6767
}
6868
implementation group: 'org.slf4j', name: 'jcl-over-slf4j', version: versions.slf4j
69+
implementation group: 'ch.qos.logback', name: 'logback-classic', version: versions.logback
6970

7071
implementation project(":agent:agent-profiler:agent-profiler-api")
7172

agent/agent-tooling/gradle/dependency-locks/compileClasspath.lockfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# This is a Gradle generated file for dependency locking.
22
# Manual edits can break the build and are not advised.
33
# This file is expected to be part of source control.
4+
ch.qos.logback:logback-classic:1.2.3
5+
ch.qos.logback:logback-core:1.2.3
46
com.google.code.findbugs:jsr305:3.0.2
57
com.google.errorprone:error_prone_annotations:2.5.1
68
com.google.guava:failureaccess:1.0.1

agent/agent-tooling/gradle/dependency-locks/runtimeClasspath.lockfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# This is a Gradle generated file for dependency locking.
22
# Manual edits can break the build and are not advised.
33
# This file is expected to be part of source control.
4+
ch.qos.logback:logback-classic:1.2.3
5+
ch.qos.logback:logback-core:1.2.3
46
com.blogspot.mydailyjava:weak-lock-free:0.15
57
com.github.oshi:oshi-core:5.6.0
68
com.google.auto.service:auto-service-annotations:1.0-rc7

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/LazyConfigurationAccessor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
package com.microsoft.applicationinsights.agent.internal;
2323

24+
import ch.qos.logback.classic.Level;
25+
import ch.qos.logback.classic.LoggerContext;
2426
import com.google.common.base.Strings;
2527
import com.microsoft.applicationinsights.TelemetryConfiguration;
2628
import com.microsoft.applicationinsights.agent.internal.propagator.DelegatingPropagator;
@@ -30,6 +32,8 @@
3032
import org.slf4j.Logger;
3133
import org.slf4j.LoggerFactory;
3234

35+
import java.util.List;
36+
3337
public class LazyConfigurationAccessor implements AiLazyConfiguration.Accessor {
3438

3539
private static final Logger logger = LoggerFactory.getLogger(LazyConfigurationAccessor.class);
@@ -56,6 +60,7 @@ private void lazySetEnvVars() {
5660

5761
setConnectionString(System.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"), System.getenv("APPINSIGHTS_INSTRUMENTATIONKEY"));
5862
setWebsiteSiteName(System.getenv("WEBSITE_SITE_NAME"));
63+
setSelfDiagnosticsLevel(System.getenv("APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_LEVEL"));
5964
setInstrumentationLoggingLevel(System.getenv("APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL"));
6065
}
6166

@@ -91,6 +96,18 @@ static void setWebsiteSiteName(String websiteSiteName) {
9196
}
9297
}
9398

99+
static void setSelfDiagnosticsLevel(String loggingLevel) {
100+
if (loggingLevel != null && !loggingLevel.isEmpty()) {
101+
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
102+
List<ch.qos.logback.classic.Logger> loggerList = loggerContext.getLoggerList();
103+
logger.info("setting APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_LEVEL to {}", loggingLevel);
104+
loggerList.stream().forEach(tmpLogger -> tmpLogger.setLevel(Level.toLevel(loggingLevel)));
105+
if (Level.toLevel(loggingLevel) == Level.DEBUG) {
106+
logger.debug("This should get logged after the logging level update.");
107+
}
108+
}
109+
}
110+
94111
static boolean shouldSetConnectionString(boolean lazySetOptIn, String enableAgent) {
95112
if (lazySetOptIn) {
96113
// when LazySetOptIn is on, enable agent if APPLICATIONINSIGHTS_ENABLE_AGENT is null or true

agent/instrumentation/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ dependencies {
8282
// reference unshaded java.util.logging.Logger
8383
// (java.util.logging.Logger shading is not needed in any of the instrumentation modules,
8484
// but it is needed for the dependencies, e.g. guava, which use java.util.logging.Logger)
85+
// -- AND ALSO --
86+
// need to perform shading in two steps in order to avoid shading ch.qos.logback.*
87+
// in opentelemetry-javaagent-logback-spans-1.0 since that instrumentation needs to
88+
// reference unshaded ch.qos.logback.*
89+
// (ch.qos.logback.* shading is not needed in any of the instrumentation modules,
90+
// but it is needed for agent-tooling, which use logback to update levels dynamically in LazyConfigurationAccessor)
8591
task shadowJarStep1(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
8692

8793
archiveClassifier.set('step1')
@@ -91,17 +97,22 @@ task shadowJarStep1(type: com.github.jengelman.gradle.plugins.shadow.tasks.Shado
9197

9298
dependencies {
9399
exclude(dependency('io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-java-util-logging-spans'))
100+
exclude(dependency('io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-logback-spans-1.0'))
94101

95102
// exclude bootstrap dependencies from shadowJar
96103
exclude(dependency('io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-bootstrap'))
97104
exclude(dependency('io.opentelemetry:opentelemetry-api'))
98105
exclude(dependency('io.opentelemetry:opentelemetry-context-prop'))
99106
exclude(dependency('io.grpc:grpc-context'))
100107
exclude(dependency('org.slf4j:slf4j-api'))
108+
exclude(dependency('ch.qos.logback:logback-classic'))
109+
exclude(dependency('ch.qos.logback:logback-core'))
101110
}
102111

103112
// rewrite dependencies calling Logger.getLogger
104113
relocate 'java.util.logging.Logger', 'io.opentelemetry.javaagent.bootstrap.PatchLogger'
114+
115+
relocate 'ch.qos.logback', "com.microsoft.applicationinsights.agent.shadow.ch.qos.logback"
105116
}
106117

107118

@@ -122,6 +133,8 @@ shadowJar {
122133
exclude(dependency('io.opentelemetry:opentelemetry-context-prop'))
123134
exclude(dependency('io.grpc:grpc-context'))
124135
exclude(dependency('org.slf4j:slf4j-api'))
136+
exclude(dependency('ch.qos.logback:logback-classic'))
137+
exclude(dependency('ch.qos.logback:logback-core'))
125138
}
126139

127140
mergeServiceFiles()

agent/instrumentation/gradle/dependency-locks/runtimeClasspath.lockfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# This is a Gradle generated file for dependency locking.
22
# Manual edits can break the build and are not advised.
33
# This file is expected to be part of source control.
4+
ch.qos.logback:logback-classic:1.2.3
5+
ch.qos.logback:logback-core:1.2.3
46
com.azure:azure-core-tracing-opentelemetry:1.0.0-beta.8
57
com.blogspot.mydailyjava:weak-lock-free:0.15
68
com.github.oshi:oshi-core:5.6.0

0 commit comments

Comments
 (0)