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

Do not create two loggers for DeprecationLogger #58435

Merged
merged 4 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.analysis.common;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.CharArraySet;
import org.apache.lucene.analysis.StopFilter;
Expand Down Expand Up @@ -157,7 +156,7 @@

public class CommonAnalysisPlugin extends Plugin implements AnalysisPlugin, ScriptPlugin {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(CommonAnalysisPlugin.class));
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(CommonAnalysisPlugin.class);

private final SetOnce<ScriptService> scriptService = new SetOnce<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.analysis.common;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.synonym.SynonymFilter;
Expand All @@ -43,8 +42,7 @@

public class SynonymTokenFilterFactory extends AbstractTokenFilterFactory {

private static final DeprecationLogger DEPRECATION_LOGGER
= new DeprecationLogger(LogManager.getLogger(SynonymTokenFilterFactory.class));
private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(SynonymTokenFilterFactory.class);

private final String format;
private final boolean expand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.ingest.useragent;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.IngestDocument;
Expand All @@ -41,7 +40,7 @@

public class UserAgentProcessor extends AbstractProcessor {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(UserAgentProcessor.class));
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(UserAgentProcessor.class);

public static final String TYPE = "user_agent";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package org.elasticsearch.index.reindex;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.util.automaton.Automata;
import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
Expand All @@ -42,8 +40,7 @@
import java.util.List;

class ReindexValidator {
private static final Logger logger = LogManager.getLogger(ReindexValidator.class);
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(ReindexValidator.class);
static final String SORT_DEPRECATED_MESSAGE = "The sort option in reindex is deprecated. " +
"Instead consider using query filtering to find the desired subset of data.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class AzureDiscoveryPlugin extends Plugin implements DiscoveryPlugin {
public static final String AZURE = "azure";
protected final Settings settings;
private static final Logger logger = LogManager.getLogger(AzureDiscoveryPlugin.class);
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(AzureDiscoveryPlugin.class);

public AzureDiscoveryPlugin(Settings settings) {
this.settings = settings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ final class Ec2ClientSettings {

private static final Logger logger = LogManager.getLogger(Ec2ClientSettings.class);

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(Ec2ClientSettings.class);

/** Credentials to authenticate with ec2. */
final AWSCredentials credentials;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void testLocationInfoTest() throws IOException, UserException {
public void testConcurrentDeprecationLogger() throws IOException, UserException, BrokenBarrierException, InterruptedException {
setupLogging("deprecation");

final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger("deprecation"));
final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger("deprecation");

final int numberOfThreads = randomIntBetween(2, 4);
final CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
Expand Down Expand Up @@ -194,7 +194,7 @@ public void testConcurrentDeprecationLogger() throws IOException, UserException,
public void testDeprecationLoggerMaybeLog() throws IOException, UserException {
setupLogging("deprecation");

final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger("deprecation"));
final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger("deprecation");

final int iterations = randomIntBetween(1, 16);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void testJsonInStacktraceMessageIsNotSplitted() throws IOException {


public void testDuplicateLogMessages() throws IOException {
final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger("test"));
final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger("test");

// For the same key and X-Opaque-ID deprecation should be once
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.http;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
Expand All @@ -41,8 +40,7 @@
public class TestDeprecatedQueryBuilder extends AbstractQueryBuilder<TestDeprecatedQueryBuilder> {
public static final String NAME = "deprecated_match_all";

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(
LogManager.getLogger(TestDeprecatedQueryBuilder.class));
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(TestDeprecatedQueryBuilder.class);

public TestDeprecatedQueryBuilder() {
// nothing to do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package org.elasticsearch.http;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.Setting;
Expand All @@ -46,8 +44,7 @@
* and returns their values.
*/
public class TestDeprecationHeaderRestAction extends BaseRestHandler {
private static final Logger logger = LogManager.getLogger(TestDeprecationHeaderRestAction.class);
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(TestDeprecationHeaderRestAction.class);

public static final Setting<Boolean> TEST_DEPRECATED_SETTING_TRUE1 =
Setting.boolSetting("test.setting.deprecated.true1", true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@

package org.elasticsearch.action.ingest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
Expand All @@ -45,10 +42,6 @@
import java.util.Objects;

public class SimulatePipelineRequest extends ActionRequest implements ToXContentObject {

private static final Logger logger = LogManager.getLogger(SimulatePipelineRequest.class);
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);

private String id;
private boolean verbose;
private BytesReference source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
*/
public class MetadataCreateIndexService {
private static final Logger logger = LogManager.getLogger(MetadataCreateIndexService.class);
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(MetadataCreateIndexService.class);

public static final int MAX_INDEX_NAME_BYTES = 255;

Expand Down
4 changes: 1 addition & 3 deletions server/src/main/java/org/elasticsearch/common/joda/Joda.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.common.joda;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.time.DateFormatter;
Expand Down Expand Up @@ -50,8 +49,7 @@
import java.util.regex.Pattern;

public class Joda {

private static DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(Joda.class));
private static DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(Joda.class);

/**
* Parses a joda based pattern, including some named ones (similar to the built in Joda ISO ones).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@ public class DeprecationLogger {
* it replaces "org.elasticsearch" with "org.elasticsearch.deprecation" to maintain
* the "org.elasticsearch" namespace.
*/
public DeprecationLogger(Logger parentLogger) {
deprecationLogger = new ThrottlingAndHeaderWarningLogger(deprecatedLoggerName(parentLogger));
private DeprecationLogger(Logger parentLogger) {
deprecationLogger = new ThrottlingAndHeaderWarningLogger(parentLogger);
}

private static Logger deprecatedLoggerName(Logger parentLogger) {
String name = parentLogger.getName();
public static DeprecationLogger getLogger(Class<?> aClass) {
return getLogger(toLoggerName(aClass));
}

public static DeprecationLogger getLogger(String name) {
return new DeprecationLogger(deprecatedLoggerName(name));
}

private static Logger deprecatedLoggerName(String name) {
if (name.startsWith("org.elasticsearch")) {
name = name.replace("org.elasticsearch.", "org.elasticsearch.deprecation.");
} else {
Expand All @@ -59,6 +66,11 @@ private static Logger deprecatedLoggerName(Logger parentLogger) {
return LogManager.getLogger(name);
}

private static String toLoggerName(final Class<?> cls) {
String canonicalName = cls.getCanonicalName();
return canonicalName != null ? canonicalName : cls.getName();
}

public static void setThreadContext(ThreadContext threadContext) {
HeaderWarning.setThreadContext(threadContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.elasticsearch.common.settings;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.lucene.util.SetOnce;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
Expand Down Expand Up @@ -319,7 +318,7 @@ public boolean hasValue(String key) {
* {@link Setting} object constructed in, for example, {@link org.elasticsearch.env.Environment}.
*/
static class DeprecationLoggerHolder {
static DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(Settings.class));
static DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(Settings.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.common.time;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.joda.time.DateTimeZone;

Expand Down Expand Up @@ -52,7 +51,7 @@ public static DateTimeZone zoneIdToDateTimeZone(ZoneId zoneId) {
return DateTimeZone.forID(zoneId.getId());
}

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(DateFormatters.class));
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(DateUtils.class);
// pkg private for tests
static final Map<String, String> DEPRECATED_SHORT_TIMEZONES;
public static final Set<String> DEPRECATED_SHORT_TZ_IDS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.common.unit;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
Expand All @@ -42,7 +41,7 @@ public class ByteSizeValue implements Writeable, Comparable<ByteSizeValue>, ToXC
* {@link ByteSizeValue} object constructed in, for example, settings in {@link org.elasticsearch.common.network.NetworkService}.
*/
static class DeprecationLoggerHolder {
static DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(ByteSizeValue.class));
static DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(ByteSizeValue.class);
}

public static final ByteSizeValue ZERO = new ByteSizeValue(0, ByteSizeUnit.BYTES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.common.xcontent;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.logging.DeprecationLogger;

Expand All @@ -44,7 +43,7 @@ public class LoggingDeprecationHandler implements DeprecationHandler {
* Changing that will require some research to make super duper
* sure it is safe.
*/
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(ParseField.class));
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(ParseField.class);

private LoggingDeprecationHandler() {
// Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class AbstractIndexComponent implements IndexComponent {
*/
protected AbstractIndexComponent(IndexSettings indexSettings) {
this.logger = Loggers.getLogger(getClass(), indexSettings.getIndex());
this.deprecationLogger = new DeprecationLogger(logger);
this.deprecationLogger = DeprecationLogger.getLogger(getClass());
this.indexSettings = indexSettings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.index.analysis;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.miscellaneous.DisableGraphAttribute;
import org.apache.lucene.analysis.shingle.ShingleFilter;
Expand All @@ -31,8 +30,7 @@

public class ShingleTokenFilterFactory extends AbstractTokenFilterFactory {

private static final DeprecationLogger DEPRECATION_LOGGER =
new DeprecationLogger(LogManager.getLogger(ShingleTokenFilterFactory.class));
private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(ShingleTokenFilterFactory.class);

private final Factory factory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.elasticsearch.index.mapper;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.codecs.PostingsFormat;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexOptions;
Expand Down Expand Up @@ -349,7 +348,7 @@ public static class Builder extends FieldMapper.Builder<Builder> {
private boolean preserveSeparators = Defaults.DEFAULT_PRESERVE_SEPARATORS;
private boolean preservePositionIncrements = Defaults.DEFAULT_POSITION_INCREMENTS;

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(Builder.class));
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(Builder.class);

/**
* @param name of the completion field to build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.index.mapper;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.search.Query;
Expand All @@ -44,7 +43,7 @@
*/
public class FieldNamesFieldMapper extends MetadataFieldMapper {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(FieldNamesFieldMapper.class));
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(FieldNamesFieldMapper.class);

public static final String NAME = "_field_names";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.index.mapper;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexOptions;
Expand Down Expand Up @@ -65,7 +64,7 @@
* queries.
*/
public class IdFieldMapper extends MetadataFieldMapper {
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(IdFieldMapper.class));
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(IdFieldMapper.class);
static final String ID_FIELD_DATA_DEPRECATION_MESSAGE =
"Loading the fielddata on the _id field is deprecated and will be removed in future versions. "
+ "If you require sorting or aggregating on this field you should also include the id in the "
Expand Down
Loading