-
Notifications
You must be signed in to change notification settings - Fork 29
dynamic log level #613
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
Merged
Merged
dynamic log level #613
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
93a436f
wip
SylvainJuge b3eff30
first working version
SylvainJuge c44ee17
use log4j level directly
SylvainJuge 230cad1
reformat
SylvainJuge 7e8163c
move to another module and cleanup
SylvainJuge 6c7fcf2
relocate internal logging impl.
SylvainJuge a01182c
provide elastic config option for log level
SylvainJuge 4cf0048
simplify custom log level usage
SylvainJuge a2e07dc
set custom log format
SylvainJuge 12941a0
Merge branch 'main' of github.com:elastic/elastic-otel-java into dyna…
SylvainJuge 9c01a43
cleanup
SylvainJuge 32cf14c
attempt to preserve upstream log options
SylvainJuge b866dd1
cleanup
SylvainJuge 17c7f73
Merge branch 'main' of github.com:elastic/elastic-otel-java into dyna…
SylvainJuge ddef5e9
Merge branch 'main' of github.com:elastic/elastic-otel-java into dyna…
SylvainJuge d6d14e4
disable system properties and env variables for log4j
SylvainJuge 0b05c84
prevent explicit 'simple' config issue
SylvainJuge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar | ||
| import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer | ||
|
|
||
| plugins { | ||
| id("elastic-otel.library-packaging-conventions") | ||
| id("com.gradleup.shadow") | ||
| } | ||
|
|
||
| dependencies { | ||
|
|
||
| compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling") | ||
| compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-bootstrap") | ||
| compileOnly(libs.slf4j.api) | ||
| implementation(libs.bundles.log4j2) | ||
|
|
||
| } | ||
|
|
||
| tasks { | ||
| val shadowJar by existing(ShadowJar::class) { | ||
| // required for META-INF/services files relocation | ||
| mergeServiceFiles() | ||
|
|
||
| // Excluding property source SPI prevents log4j system properties and env variables that might | ||
| // be set at the application level to change the behavior of this internal log4j instance. | ||
| exclude("**/*.log4j.util.PropertySource") | ||
|
|
||
| transform(Log4j2PluginsCacheFileTransformer::class.java) | ||
|
|
||
| // relocate slf4j and log4j for internal logging to prevent any conflict | ||
| relocate("org.slf4j", "co.elastic.otel.logging.slf4j") | ||
| relocate("org.apache.logging.log4j", "co.elastic.otel.logging.log4j") | ||
| relocate("org.apache.logging.slf4j", "co.elastic.otel.logging.log4j.slf4j") | ||
| } | ||
|
|
||
| assemble { | ||
| dependsOn(shadowJar) | ||
| } | ||
| } |
69 changes: 69 additions & 0 deletions
69
internal-logging/src/main/java/co/elastic/otel/logging/AgentLog.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.otel.logging; | ||
|
|
||
| import org.apache.logging.log4j.Level; | ||
| import org.apache.logging.log4j.core.config.Configurator; | ||
| import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; | ||
| import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory; | ||
| import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; | ||
|
|
||
| public class AgentLog { | ||
|
|
||
| private static final String PATTERN = "%d{DEFAULT} [%t] %-5level %logger{36} - %msg{nolookups}%n"; | ||
|
|
||
| // logger is an empty string | ||
| private static final String ROOT_LOGGER_NAME = ""; | ||
|
|
||
| private AgentLog() {} | ||
|
|
||
| public static void init() { | ||
|
|
||
| ConfigurationBuilder<BuiltConfiguration> conf = | ||
|
SylvainJuge marked this conversation as resolved.
|
||
| ConfigurationBuilderFactory.newConfigurationBuilder(); | ||
|
|
||
| conf.add( | ||
| conf.newAppender("stdout", "Console") | ||
| .add(conf.newLayout("PatternLayout").addAttribute("pattern", PATTERN))); | ||
|
|
||
| conf.add(conf.newRootLogger().add(conf.newAppenderRef("stdout"))); | ||
|
|
||
| Configurator.initialize(conf.build(false)); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the agent log level at runtime | ||
| * | ||
| * @param level log level | ||
| */ | ||
| public static void setLevel(Level level) { | ||
| // Using log4j2 implementation allows to change the log level programmatically at runtime | ||
| // which is not directly possible through the slf4j API and simple implementation used in | ||
| // upstream distribution. | ||
|
|
||
| Configurator.setAllLevels(ROOT_LOGGER_NAME, level); | ||
|
|
||
| // when debugging we should avoid very chatty http client debug messages | ||
| // this behavior is replicated from the upstream distribution. | ||
| if (level.intLevel() >= Level.DEBUG.intLevel()) { | ||
| Configurator.setLevel("okhttp3.internal.http2", Level.INFO); | ||
| Configurator.setLevel("okhttp3.internal.concurrent.TaskRunner", Level.INFO); | ||
| } | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
internal-logging/src/main/java/co/elastic/otel/logging/ElasticLoggingCustomizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.otel.logging; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.javaagent.bootstrap.InternalLogger; | ||
| import io.opentelemetry.javaagent.tooling.LoggingCustomizer; | ||
| import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig; | ||
| import org.apache.logging.log4j.Level; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @AutoService(LoggingCustomizer.class) | ||
| public class ElasticLoggingCustomizer implements LoggingCustomizer { | ||
|
|
||
| @Override | ||
| public String name() { | ||
| // must match "otel.javaagent.logging" system property for SPI lookup | ||
| return "elastic"; | ||
| } | ||
|
|
||
| @Override | ||
| public void init(EarlyInitAgentConfig earlyConfig) { | ||
|
|
||
| // trigger loading the slf4j provider from the agent CL, this should load log4j implementation | ||
| LoggerFactory.getILoggerFactory(); | ||
|
|
||
| // make the agent internal logger delegate to slf4j, which will delegate to log4j | ||
| InternalLogger.initialize(Slf4jInternalLogger::create); | ||
|
|
||
| AgentLog.init(); | ||
|
|
||
| Level level = null; | ||
| if (earlyConfig.getBoolean("otel.javaagent.debug", false)) { | ||
| // set debug logging when enabled through configuration to behave like the upstream | ||
| // distribution | ||
| level = Level.DEBUG; | ||
| } else { | ||
| String levelConfig = earlyConfig.getString("elastic.otel.javaagent.log.level"); | ||
| if (levelConfig != null) { | ||
| level = Level.getLevel(levelConfig); | ||
| } | ||
| } | ||
| AgentLog.setLevel(level != null ? level : Level.INFO); | ||
| } | ||
|
|
||
| @Override | ||
| public void onStartupSuccess() {} | ||
|
|
||
| @SuppressWarnings("CallToPrintStackTrace") | ||
| @Override | ||
| public void onStartupFailure(Throwable throwable) { | ||
| throwable.printStackTrace(); | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
internal-logging/src/main/java/co/elastic/otel/logging/Slf4jInternalLogger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.otel.logging; | ||
|
|
||
| import io.opentelemetry.javaagent.bootstrap.InternalLogger; | ||
| import javax.annotation.Nullable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** Internal logger implementation that delegates to SLF4J */ | ||
| public class Slf4jInternalLogger implements InternalLogger { | ||
| private final Logger logger; | ||
|
|
||
| private Slf4jInternalLogger(String name) { | ||
| this.logger = LoggerFactory.getLogger(name); | ||
| } | ||
|
|
||
| public static InternalLogger create(String name) { | ||
| return new Slf4jInternalLogger(name); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isLoggable(Level level) { | ||
| switch (level) { | ||
| case TRACE: | ||
| return logger.isTraceEnabled(); | ||
| case DEBUG: | ||
| return logger.isDebugEnabled(); | ||
| case INFO: | ||
| return logger.isInfoEnabled(); | ||
| case WARN: | ||
| return logger.isWarnEnabled(); | ||
| case ERROR: | ||
| return logger.isErrorEnabled(); | ||
| default: | ||
| throw new IllegalArgumentException("Unsupported level: " + level); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void log(Level level, String s, @Nullable Throwable throwable) { | ||
| logger.atLevel(toSlf4jLevel(level)).setCause(throwable).log(s); | ||
| } | ||
|
|
||
| private static org.slf4j.event.Level toSlf4jLevel(Level level) { | ||
| switch (level) { | ||
| case TRACE: | ||
| return org.slf4j.event.Level.TRACE; | ||
| case DEBUG: | ||
| return org.slf4j.event.Level.DEBUG; | ||
| case INFO: | ||
| return org.slf4j.event.Level.INFO; | ||
| case WARN: | ||
| return org.slf4j.event.Level.WARN; | ||
| case ERROR: | ||
| return org.slf4j.event.Level.ERROR; | ||
| default: | ||
| throw new IllegalArgumentException("Unsupported level: " + level); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return logger.getName(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.