Skip to content

Commit

Permalink
Implementing log_level configuration spec (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalkoren authored Oct 11, 2020
1 parent 2259495 commit beb27df
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ endif::[]
This makes it easier to differ distinct snapshot builds of the same version.
Example: `1.18.1-SNAPSHOT.4655910`
* Add support for sampling weight with propagation in `tracestate` W3C header {pull}1384[#1384]
* Adding two more valid options to the `log_level` config: `WARNING` (equivalent to `WARN`) and `CRITICAL`
(will be treated as `ERROR`) - {pull}1431[1431]
[float]
===== Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public Configuration getConfiguration() {
builder.setStatusLevel(Level.ERROR)
.setConfigurationName("ElasticAPM");

Level level = Level.valueOf(getValue(LOG_LEVEL_KEY, sources, getValue(DEPRECATED_LOG_LEVEL_KEY, sources, Level.INFO.toString())));
Level level = getLogLevel();
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(level);
List<AppenderComponentBuilder> appenders = createAppenders(builder);
for (AppenderComponentBuilder appender : appenders) {
Expand All @@ -148,6 +148,12 @@ public Configuration getConfiguration() {
return builder.build();
}

private Level getLogLevel() {
String rawLogLevelValue = getValue(LOG_LEVEL_KEY, sources, getValue(DEPRECATED_LOG_LEVEL_KEY, sources, Level.INFO.toString()));
LogLevel logLevel = LoggingConfiguration.mapLogLevel(new EnumValueConverter<>(LogLevel.class).convert(rawLogLevelValue));
return Level.valueOf(logLevel.toString());
}

private List<AppenderComponentBuilder> createAppenders(ConfigurationBuilder<BuiltConfiguration> builder) {
List<AppenderComponentBuilder> appenders = new ArrayList<>();
String logFile = getActualLogFile(ElasticApmAgent.getAgentHome(), getValue(LOG_FILE_KEY, sources, getValue(DEPRECATED_LOG_FILE_KEY, sources, DEFAULT_LOG_FILE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
enum LogLevel {
OFF,
ERROR,
CRITICAL,
WARN,
WARNING,
INFO,
DEBUG,
TRACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.source.ConfigurationSource;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

Expand Down Expand Up @@ -77,6 +78,10 @@ public class LoggingConfiguration extends ConfigurationOptionProvider {
* However, the registry initializes logging by declaring a static final logger variable.
* In order to break up the cyclic dependency and to not accidentally initialize logging before we had the chance to configure the logging,
* we manually resolve these options.
*
* NOTE: on top of the above, this specific option should never be accessed through the ConfigurationOption as it
* allows {@link LogLevel} that are effectively mapped to other values.
*
* See {@link Log4j2ConfigurationFactory#getValue}
*/
@SuppressWarnings("unused")
Expand All @@ -85,17 +90,36 @@ public class LoggingConfiguration extends ConfigurationOptionProvider {
.aliasKeys(DEPRECATED_LOG_LEVEL_KEY)
.configurationCategory(LOGGING_CATEGORY)
.description("Sets the logging level for the agent.\n" +
"This option is case-insensitive.\n" +
"\n" +
"This option is case-insensitive.")
"NOTE: `CRITICAL` is a valid option, but it is mapped to `ERROR`; `WARN` and `WARNING` are equivalent.")
.dynamic(true)
.addChangeListener(new ConfigurationOption.ChangeListener<LogLevel>() {
@Override
public void onChange(ConfigurationOption<?> configurationOption, LogLevel oldValue, LogLevel newValue) {
newValue = mapLogLevel(newValue);
setLogLevel(newValue);
}
})
.buildWithDefault(LogLevel.INFO);

/**
* Maps a {@link LogLevel} that is supported by the agent to a value that is also supported by the underlying
* logging framework.
* @param original the agent-supported {@link LogLevel}
* @return a {@link LogLevel} that is both supported by the agent and the underlying logging framework
*/
@Nonnull
static LogLevel mapLogLevel(LogLevel original) {
LogLevel mapped = original;
if (original == LogLevel.WARNING) {
mapped = LogLevel.WARN;
} else if (original == LogLevel.CRITICAL) {
mapped = LogLevel.ERROR;
}
return mapped;
}

@SuppressWarnings("unused")
public ConfigurationOption<String> logFile = ConfigurationOption.stringOption()
.key(LOG_FILE_KEY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*-
* #%L
* Elastic APM Java agent
* %%
* Copyright (C) 2018 - 2020 Elastic and contributors
* %%
* 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.
* #L%
*/
package co.elastic.apm.agent.resttemplate;

import co.elastic.apm.agent.bci.TracerAwareInstrumentation;
Expand Down
10 changes: 6 additions & 4 deletions docs/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1514,12 +1514,13 @@ The resulting documents in Elasticsearch look similar to this:
==== `log_level`

Sets the logging level for the agent.

This option is case-insensitive.

NOTE: `CRITICAL` is a valid option, but it is mapped to `ERROR`; `WARN` and `WARNING` are equivalent.

<<configuration-dynamic, image:./images/dynamic-config.svg[] >>

Valid options: `OFF`, `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`
Valid options: `OFF`, `ERROR`, `CRITICAL`, `WARN`, `WARNING`, `INFO`, `DEBUG`, `TRACE`

[options="header"]
|============
Expand Down Expand Up @@ -3054,10 +3055,11 @@ The default unit for this option is `ms`.
############################################
# Sets the logging level for the agent.
#
# This option is case-insensitive.
#
# NOTE: `CRITICAL` is a valid option, but it is mapped to `ERROR`; `WARN` and `WARNING` are equivalent.
#
# Valid options: OFF, ERROR, WARN, INFO, DEBUG, TRACE
# Valid options: OFF, ERROR, CRITICAL, WARN, WARNING, INFO, DEBUG, TRACE
# This setting can be changed at runtime
# Type: LogLevel
# Default value: INFO
Expand Down

0 comments on commit beb27df

Please sign in to comment.