Skip to content

Commit eeb43ec

Browse files
committed
Initial logging module prototype
1 parent 1ac9108 commit eeb43ec

File tree

720 files changed

+2766
-3785
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

720 files changed

+2766
-3785
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/fs/AvailableIndexFoldersBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
*/
88
package org.elasticsearch.benchmark.fs;
99

10-
import org.elasticsearch.common.logging.LogConfigurator;
1110
import org.elasticsearch.common.settings.Settings;
1211
import org.elasticsearch.env.Environment;
1312
import org.elasticsearch.env.NodeEnvironment;
13+
import org.elasticsearch.logging.internal.LogConfigurator;
1414
import org.openjdk.jmh.annotations.Benchmark;
1515
import org.openjdk.jmh.annotations.BenchmarkMode;
1616
import org.openjdk.jmh.annotations.Fork;

libs/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ configure(subprojects - project('elasticsearch-log4j')) {
2424
if (depProject != null
2525
&& false == depProject.path.equals(':libs:elasticsearch-x-content')
2626
&& false == depProject.path.equals(':libs:elasticsearch-core')
27+
&& false == depProject.path.equals(':libs:elasticsearch-cli')
2728
&& depProject.path.startsWith(':libs')
2829
&& depProject.name.startsWith('elasticsearch-')) {
2930
throw new InvalidUserDataException("projects in :libs "

libs/logging/build.gradle

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
apply plugin: 'elasticsearch.publish'
10+
11+
dependencies {
12+
moduleCompileOnly project(':libs:elasticsearch-x-content') // TODO: for JsonStringEncoder
13+
moduleCompileOnly project(':libs:elasticsearch-cli') // TODO: prob remove this, if just for exception types
14+
moduleCompileOnly "org.apache.logging.log4j:log4j-api:${versions.log4j}"
15+
moduleCompileOnly "org.apache.logging.log4j:log4j-core:${versions.log4j}"
16+
17+
moduleCompileOnly "co.elastic.logging:log4j2-ecs-layout:${versions.ecsLogging}"
18+
moduleCompileOnly "co.elastic.logging:ecs-logging-core:${versions.ecsLogging}"
19+
20+
testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
21+
testImplementation "junit:junit:${versions.junit}"
22+
testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}"
23+
24+
testImplementation(project(":test:framework")) {
25+
exclude group: 'org.elasticsearch', module: 'elasticsearch-logging'
26+
}
27+
}
28+
29+
//tasks.named('forbiddenApisMain').configure {
30+
// // :libs:elasticsearch-core does not depend on server
31+
// // TODO: Need to decide how we want to handle for forbidden signatures with the changes to server
32+
// replaceSignatureFiles 'jdk-signatures'
33+
//}
34+
//
35+
tasks.named("compileJava").configure {
36+
options.compilerArgs.add("-Xlint:-requires-automatic")
37+
options.compilerArgs.add("-Xlint:-module") // qualified exports
38+
options.compilerArgs.add("-Xlint:-exports") // implements Message!!
39+
}
40+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
module org.elasticsearch.logging {
10+
requires org.elasticsearch.cli;
11+
requires org.elasticsearch.xcontent;
12+
requires log4j2.ecs.layout;
13+
requires ecs.logging.core;
14+
requires org.apache.logging.log4j;
15+
requires org.apache.logging.log4j.core;
16+
17+
exports org.elasticsearch.logging;
18+
exports org.elasticsearch.logging.internal to org.elasticsearch.server;
19+
exports org.elasticsearch.logging.internal.spi to org.elasticsearch.server;
20+
21+
}

server/src/main/java/org/elasticsearch/common/logging/DeprecatedMessage.java renamed to libs/logging/src/main/java/org/elasticsearch/logging/DeprecatedMessage.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.logging;
9+
package org.elasticsearch.logging;
1010

11-
import org.elasticsearch.common.Strings;
12-
import org.elasticsearch.common.SuppressLoggerChecks;
11+
//import org.elasticsearch.common.SuppressLoggerChecks;
1312

1413
import java.util.Locale;
1514

15+
import static org.elasticsearch.logging.internal.Util.isNullOrEmpty;
16+
1617
/**
1718
* A logger message used by {@link DeprecationLogger}, enriched with fields
1819
* named following ECS conventions. Carries x-opaque-id field if provided in the headers.
1920
* Will populate the x-opaque-id field in JSON logs.
2021
*/
21-
public class DeprecatedMessage {
22+
public final class DeprecatedMessage {
2223
public static final String ELASTIC_ORIGIN_FIELD_NAME = "elasticsearch.elastic_product_origin";
2324
public static final String KEY_FIELD_NAME = "event.code";
2425
public static final String X_OPAQUE_ID_FIELD_NAME = "elasticsearch.http.request.x_opaque_id";
2526
public static final String ECS_VERSION = "1.2.0";
2627

27-
@SuppressLoggerChecks(reason = "safely delegates to logger")
28+
private DeprecatedMessage() {}
29+
30+
// @SuppressLoggerChecks(reason = "safely delegates to logger")
2831
public static ESLogMessage of(
2932
DeprecationCategory category,
3033
String key,
@@ -36,7 +39,7 @@ public static ESLogMessage of(
3639
return getEsLogMessage(category, key, xOpaqueId, productOrigin, messagePattern, args);
3740
}
3841

39-
@SuppressLoggerChecks(reason = "safely delegates to logger")
42+
// @SuppressLoggerChecks(reason = "safely delegates to logger")
4043
public static ESLogMessage compatibleDeprecationMessage(
4144
String key,
4245
String xOpaqueId,
@@ -47,7 +50,7 @@ public static ESLogMessage compatibleDeprecationMessage(
4750
return getEsLogMessage(DeprecationCategory.COMPATIBLE_API, key, xOpaqueId, productOrigin, messagePattern, args);
4851
}
4952

50-
@SuppressLoggerChecks(reason = "safely delegates to logger")
53+
// @SuppressLoggerChecks(reason = "safely delegates to logger")
5154
private static ESLogMessage getEsLogMessage(
5255
DeprecationCategory category,
5356
String key,
@@ -62,7 +65,7 @@ private static ESLogMessage getEsLogMessage(
6265
.field(KEY_FIELD_NAME, key)
6366
.field("elasticsearch.event.category", category.name().toLowerCase(Locale.ROOT));
6467

65-
if (Strings.isNullOrEmpty(xOpaqueId)) {
68+
if (isNullOrEmpty(xOpaqueId)) {
6669
return esLogMessage;
6770
}
6871

server/src/main/java/org/elasticsearch/common/logging/DeprecationCategory.java renamed to libs/logging/src/main/java/org/elasticsearch/logging/DeprecationCategory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.logging;
9+
package org.elasticsearch.logging;
1010

1111
/**
1212
* Deprecation log messages are categorised so that consumers of the logs can easily aggregate them.

server/src/main/java/org/elasticsearch/common/logging/DeprecationLogger.java renamed to libs/logging/src/main/java/org/elasticsearch/logging/DeprecationLogger.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.logging;
9+
package org.elasticsearch.logging;
1010

11-
import org.apache.logging.log4j.Level;
12-
import org.apache.logging.log4j.LogManager;
13-
import org.apache.logging.log4j.Logger;
11+
import org.elasticsearch.logging.internal.HeaderWarningAppender;
12+
import org.elasticsearch.logging.internal.RateLimitingFilter;
13+
import org.elasticsearch.logging.internal.ServerSupportImpl;
1414

1515
import java.security.AccessController;
1616
import java.security.PrivilegedAction;
@@ -29,12 +29,12 @@
2929
* key is combined with the <code>X-Opaque-Id</code> request header value, if supplied, which allows for per-client
3030
* message limiting.
3131
*/
32-
public class DeprecationLogger {
32+
public final class DeprecationLogger {
3333
/**
3434
* Deprecation messages are logged at this level.
3535
* More serious that WARN by 1, but less serious than ERROR
3636
*/
37-
public static Level CRITICAL = Level.forName("CRITICAL", Level.WARN.intLevel() - 1);
37+
public static Level CRITICAL = Level.of("CRITICAL", Level.WARN.getSeverity() - 1);
3838

3939
private final Logger logger;
4040

@@ -97,8 +97,8 @@ public DeprecationLogger warn(final DeprecationCategory category, final String k
9797
private DeprecationLogger logDeprecation(Level level, DeprecationCategory category, String key, String msg, Object[] params) {
9898
assert category != DeprecationCategory.COMPATIBLE_API
9999
: "DeprecationCategory.COMPATIBLE_API should be logged with compatibleApiWarning method";
100-
String opaqueId = HeaderWarning.getXOpaqueId();
101-
String productOrigin = HeaderWarning.getProductOrigin();
100+
String opaqueId = ServerSupportImpl.INSTANCE.getXOpaqueIdHeader();
101+
String productOrigin = ServerSupportImpl.INSTANCE.getProductOriginHeader();
102102
ESLogMessage deprecationMessage = DeprecatedMessage.of(category, key, opaqueId, productOrigin, msg, params);
103103
doPrivilegedLog(level, deprecationMessage);
104104
return this;
@@ -130,8 +130,8 @@ public DeprecationLogger compatibleCritical(final String key, final String msg,
130130
* so that it can be returned to the client.
131131
*/
132132
public DeprecationLogger compatible(final Level level, final String key, final String msg, final Object... params) {
133-
String opaqueId = HeaderWarning.getXOpaqueId();
134-
String productOrigin = HeaderWarning.getProductOrigin();
133+
String opaqueId = ServerSupportImpl.INSTANCE.getXOpaqueIdHeader();
134+
String productOrigin = ServerSupportImpl.INSTANCE.getProductOriginHeader();
135135
ESLogMessage deprecationMessage = DeprecatedMessage.compatibleDeprecationMessage(key, opaqueId, productOrigin, msg, params);
136136
logger.log(level, deprecationMessage);
137137
return this;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.logging;
10+
11+
import org.elasticsearch.logging.internal.ESLogMessageImpl;
12+
13+
import java.util.Map;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
16+
17+
/**
18+
* Custom logger messages. Carries additional fields which will populate JSON fields in logs.
19+
*/
20+
public final class ESLogMessage implements Message {
21+
22+
private final ESLogMessageImpl impl;
23+
24+
public ESLogMessage() {
25+
impl = new ESLogMessageImpl();
26+
}
27+
28+
public ESLogMessage(String messagePattern, Object... args) {
29+
impl = new ESLogMessageImpl(messagePattern, args);
30+
31+
}
32+
33+
public ESLogMessage argAndField(String key, Object value) {
34+
return this.argAndField(key, value);
35+
}
36+
37+
public ESLogMessage withFields(Map<String, Object> jsonFields) {
38+
return this.withFields(jsonFields);
39+
}
40+
41+
public ESLogMessage field(String key, Object value) {
42+
return this.field(key, value);
43+
}
44+
45+
public String get(String key) {
46+
return impl.get(key);
47+
}
48+
49+
@Override
50+
public String getFormattedMessage() {
51+
return impl.getFormattedMessage();
52+
}
53+
54+
@Override
55+
public String getFormat() {
56+
return impl.getFormat();
57+
}
58+
59+
@Override
60+
public Object[] getParameters() {
61+
return impl.getParameters();
62+
}
63+
64+
@Override
65+
public Throwable getThrowable() {
66+
return impl.getThrowable();
67+
}
68+
69+
public static String asJsonArray(Stream<String> stream) {
70+
return "[" + stream.map(ESLogMessageImpl::inQuotes).collect(Collectors.joining(", ")) + "]";
71+
}
72+
73+
// static ESLogMessage of() {
74+
// return new ESLogMessageImpl();
75+
// }
76+
//
77+
// static ESLogMessage of(String messagePattern, Object... args) {
78+
// return new ESLogMessageImpl(messagePattern, args);
79+
// }
80+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.logging;
10+
11+
import org.elasticsearch.logging.internal.StandardLevels;
12+
13+
import java.util.Locale;
14+
import java.util.Objects;
15+
import java.util.concurrent.ConcurrentHashMap;
16+
import java.util.concurrent.ConcurrentMap;
17+
18+
public final class Level {
19+
20+
public static final Level OFF = new Level("OFF", StandardLevels.OFF);
21+
22+
public static final Level FATAL = new Level("FATAL", StandardLevels.FATAL);
23+
24+
public static final Level ERROR = new Level("ERROR", StandardLevels.ERROR);
25+
26+
public static final Level WARN = new Level("WARN", StandardLevels.WARN);
27+
28+
public static final Level INFO = new Level("INFO", StandardLevels.INFO);
29+
30+
public static final Level DEBUG = new Level("DEBUG", StandardLevels.DEBUG);
31+
32+
public static final Level TRACE = new Level("TRACE", StandardLevels.TRACE);
33+
34+
public static final Level ALL = new Level("ALL", StandardLevels.ALL);
35+
36+
private static final ConcurrentMap<String, Level> LEVELS = new ConcurrentHashMap<>();
37+
38+
private final String name;
39+
40+
private final int severity;
41+
42+
/*package*/ static Level of(String name, int severity) {
43+
var level = new Level(name, severity);
44+
if (LEVELS.putIfAbsent(name, level) != null) {
45+
throw new IllegalStateException("Level " + name + " is already been defined.");
46+
}
47+
return level;
48+
}
49+
50+
private Level(String name, int severity) {
51+
this.name = name;
52+
this.severity = severity;
53+
}
54+
55+
/**
56+
* Returns the name of this level.
57+
*/
58+
public String name() {
59+
return name;
60+
}
61+
62+
public int getSeverity() {
63+
return severity;
64+
}
65+
66+
/** Return the Level associated with the name. */
67+
public static Level valueOf(final String name) {
68+
Objects.requireNonNull(name);
69+
final String levelName = name.trim().toUpperCase(Locale.ROOT);
70+
final Level level = LEVELS.get(levelName);
71+
if (level != null) {
72+
return level;
73+
}
74+
throw new IllegalArgumentException("Unknown level constant [" + levelName + "].");
75+
}
76+
77+
public boolean isMoreSpecificThan(Level level) {
78+
return this.severity <= level.severity;
79+
}
80+
81+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.logging;
10+
11+
public class LogManager {
12+
13+
public static Logger getLogger(final String name) {
14+
return null;
15+
}
16+
17+
public static Logger getLogger(final Class<?> clazz) {
18+
return null;
19+
}
20+
21+
private LogManager() {}
22+
23+
}

0 commit comments

Comments
 (0)