Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 742ea49

Browse files
Nick WemekampNick Wemekamp
Nick Wemekamp
authored and
Nick Wemekamp
committed
Added additional JSON fields and included fat jar build with all the required dependencies.
Signed-off-by: Nick Wemekamp <[email protected]>
1 parent f626774 commit 742ea49

File tree

5 files changed

+97
-30
lines changed

5 files changed

+97
-30
lines changed

README.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ The `weblogic-logging-exporter.jar` will be available under the `target` directo
9696

9797
## Installation
9898

99-
This section outlines the steps that are required to add the Weblogic Logging Exporter to Weblogic Server.
99+
This section outlines the steps that are required to add the WebLogic Logging Exporter to WebLogic Server.
100100

101101
1. Download or build the WebLogic Logging Exporter as described above.
102102

@@ -119,17 +119,7 @@ This section outlines the steps that are required to add the Weblogic Logging Ex
119119
</startup-class>
120120
```
121121
122-
1. Add `weblogic-logging-exporter.jar` and `snakeyaml-1.27.jar` to your classpath.
123-
124-
This project requires `snakeyaml` to parse the YAML configuration file. If you built the project locally,
125-
you can find this JAR file in your local maven repository at `~/.m2/repository/org/yaml/snakeyaml/1.27/snakeyaml-1.27.jar`.
126-
Otherwise, you can download it from [Maven Central](https://search.maven.org/artifact/org.yaml/snakeyaml/1.27/bundle).
127-
128-
If you want to write the JSON logs to a file instead of sending it elasticsearch directly you will also need these jars:
129-
- ecs-logging-core-1.1.0.jar (used for JSON logging, https://mvnrepository.com/artifact/co.elastic.logging/ecs-logging-core)
130-
- jul-ecs-formatter-1.1.0.jar (used for JSON logging, https://mvnrepository.com/artifact/co.elastic.logging/jul-ecs-formatter/1.1.0)
131-
- slf4j-api-1.7.32.jar (optional and is used for MDC, https://mvnrepository.com/artifact/org.slf4j/slf4j-api)
132-
- slf4j-jdk14-1.7.32.jar (optional and is used for MDC, https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14)
122+
1. Add `weblogic-logging-exporter.jar` to your classpath.
133123
134124
Place the file(s) in a suitable location, e.g. your domain directory.
135125
@@ -138,7 +128,7 @@ This section outlines the steps that are required to add the Weblogic Logging Ex
138128
directory is `/u01/base_domain`):
139129
140130
```
141-
export CLASSPATH="/u01/base_domain/weblogic-logging-exporter.jar:/u01/base_domain/snakeyaml-1.27.jar:$CLASSPATH"
131+
export CLASSPATH="/u01/base_domain/weblogic-logging-exporter.jar:$CLASSPATH"
142132
```
143133
144134
1. Create a configuration file for the WebLogic Logging Exporter.
@@ -170,7 +160,7 @@ This section outlines the steps that are required to add the Weblogic Logging Ex
170160
`WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE` to point to the location of the file.
171161
172162
If you want to write the JSON logs to a file instead of sending it elasticsearch directly use the following configuration
173-
[file](samples/WeblogicFileLoggingExporter.yaml) and adjust it to your needs. Make sure to rename it to WebLogicLoggingExporter.yaml.
163+
[file](samples/WebLogicFileLoggingExporter.yaml) and adjust it to your needs. Make sure to rename it to WebLogicLoggingExporter.yaml.
174164
175165
6. Restart the servers to activate the changes. After restarting the servers, they will load the WebLogic
176166
Logging Exporter and start sending their logs to the specified Elasticsearch instance. You can then

pom.xml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
</plugin>
6464
<!-- for junit 5 -->
6565
<plugin>
66-
<groupId>org.apache.maven.plugins</groupId>
6766
<artifactId>maven-surefire-plugin</artifactId>
6867
<version>2.22.2</version>
6968
<configuration>
@@ -88,7 +87,7 @@
8887
<plugin>
8988
<groupId>org.apache.maven.plugins</groupId>
9089
<artifactId>maven-failsafe-plugin</artifactId>
91-
<version>2.22.1</version>
90+
<version>2.22.2</version>
9291
</plugin>
9392
<!-- end -->
9493
<plugin>
@@ -111,6 +110,24 @@
111110
</execution>
112111
</executions>
113112
</plugin>
113+
<plugin>
114+
<groupId>org.apache.maven.plugins</groupId>
115+
<artifactId>maven-assembly-plugin</artifactId>
116+
<version>3.3.0</version>
117+
<executions>
118+
<execution>
119+
<phase>package</phase>
120+
<goals>
121+
<goal>single</goal>
122+
</goals>
123+
</execution>
124+
</executions>
125+
<configuration>
126+
<descriptorRefs>
127+
<descriptorRef>jar-with-dependencies</descriptorRef>
128+
</descriptorRefs>
129+
</configuration>
130+
</plugin>
114131
</plugins>
115132
</build>
116133

@@ -195,15 +212,21 @@
195212
<dependency>
196213
<groupId>co.elastic.logging</groupId>
197214
<artifactId>jul-ecs-formatter</artifactId>
198-
<version>1.1.0</version>
215+
<version>1.2.0</version>
216+
</dependency>
217+
<dependency>
218+
<groupId>org.slf4j</groupId>
219+
<artifactId>slf4j-jdk14</artifactId>
220+
<version>1.7.32</version>
199221
</dependency>
200222
<dependency>
201223
<groupId>org.antlr</groupId>
202224
<artifactId>antlr-complete</artifactId>
203225
<version>3.5.2</version>
226+
<!-- we need it in unit tests but the weblogic runtime provides it at run time -->
227+
<scope>test</scope>
204228
</dependency>
205229
</dependencies>
206-
207230
<reporting>
208231
<plugins>
209232
<plugin>

src/main/java/weblogic/logging/exporter/Startup.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.logging.Level;
99
import java.util.logging.Logger;
1010

11-
import co.elastic.logging.jul.EcsFormatter;
1211
import weblogic.logging.LoggingHelper;
1312
import weblogic.logging.ServerLoggingHandler;
1413
import weblogic.logging.exporter.config.Config;
@@ -18,7 +17,7 @@ public class Startup {
1817
private static final String DEFAULT_CONFIG_FILE = "config/WebLogicLoggingExporter.yaml";
1918

2019
public static void main(String[] argv) {
21-
System.out.println("======================= Weblogic Logging Exporter Startup class called");
20+
System.out.println("======================= WebLogic Logging Exporter Startup class called");
2221
try {
2322
Logger logger = LoggingHelper.getServerLogger();
2423

@@ -30,15 +29,15 @@ public static void main(String[] argv) {
3029
*/
3130

3231
String fileName =
33-
System.getProperty(
34-
"WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE",
35-
System.getenv("WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE"));
32+
System.getProperty(
33+
"WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE",
34+
System.getenv("WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE"));
3635
System.out.println(
37-
"JavaProperty/EnvVariable WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE:" + fileName);
36+
"JavaProperty/EnvVariable WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE:" + fileName);
3837
if (fileName == null || fileName.isEmpty()) {
3938
System.out.println(
40-
"Env variable WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE is not set. Defaulting to:"
41-
+ DEFAULT_CONFIG_FILE);
39+
"Env variable WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE is not set. Defaulting to:"
40+
+ DEFAULT_CONFIG_FILE);
4241
fileName = DEFAULT_CONFIG_FILE;
4342
}
4443
File file = new File(fileName);
@@ -57,14 +56,14 @@ public static void main(String[] argv) {
5756
// Register a file handler using the provided config
5857
FileHandler fh = new FileHandler(config.getOutputFile(), config.getMaxFileSize(), config.getGetMaxRollbackFiles(), config.getAppendToFile());
5958
fh.setLevel(Level.parse(config.getFileLoggingLogLevel()));
60-
fh.setFormatter(new EcsFormatter());
59+
fh.setFormatter(new WebLogicLogFormatter(config.getDomainUID()));
6160
logger.addHandler(fh);
62-
} else {
61+
} else {
6362
System.out.println("WebLogic Elasticsearch Logging Exporter is disabled");
6463
}
65-
// also catch errors so that Weblogic does not crash when a required library was not placed in the classpath correctly.
64+
// also catch errors so that WebLogic does not crash when a required library was not placed in the classpath correctly.
6665
} catch (Error | Exception e) {
67-
System.out.println("======================= Something went wrong, the Weblogic Logging Exporter is not activated");
66+
System.out.println("======================= Something went wrong, the WebLogic Logging Exporter is not activated");
6867
e.printStackTrace();
6968
}
7069
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package weblogic.logging.exporter;
2+
3+
import co.elastic.logging.jul.EcsFormatter;
4+
import org.slf4j.MDC;
5+
import weblogic.logging.WLLogRecord;
6+
7+
import java.util.logging.LogRecord;
8+
9+
public class WebLogicLogFormatter extends EcsFormatter {
10+
public static final String FIELDS_MESSAGE_ID = "fields.messageID";
11+
public static final String FIELDS_SERVER_NAME = "fields.serverName";
12+
public static final String FIELDS_USER_ID = "fields.userId";
13+
public static final String FIELDS_SUB_SYSTEM = "fields.subSystem";
14+
public static final String FIELDS_MACHINE_NAME = "fields.machineName";
15+
public static final String FIELDS_TRANSACTION_ID = "fields.transactionId";
16+
public static final String FIELDS_DIAGNOSTIC_CONTEXT_ID = "fields.diagnosticContextId";
17+
public static final String FIELDS_SEQUENCE_NUMBER = "fields.sequenceNumber";
18+
public static final String FIELDS_DOMAIN_UID = "fields.domainUID";
19+
20+
private final String domainUID;
21+
22+
public WebLogicLogFormatter(String domainUID) {
23+
this.domainUID = domainUID;
24+
}
25+
26+
@Override
27+
public String format(final LogRecord record) {
28+
WLLogRecord wlLogRecord = (WLLogRecord) record;
29+
30+
MDC.put(FIELDS_MESSAGE_ID, wlLogRecord.getId());
31+
MDC.put(FIELDS_SERVER_NAME, wlLogRecord.getServerName());
32+
MDC.put(FIELDS_USER_ID, wlLogRecord.getUserId());
33+
MDC.put(FIELDS_SUB_SYSTEM, wlLogRecord.getSubsystem());
34+
MDC.put(FIELDS_MACHINE_NAME, wlLogRecord.getMachineName());
35+
MDC.put(FIELDS_TRANSACTION_ID, wlLogRecord.getTransactionId());
36+
MDC.put(FIELDS_DIAGNOSTIC_CONTEXT_ID, wlLogRecord.getDiagnosticContextId());
37+
MDC.put(FIELDS_SEQUENCE_NUMBER, String.valueOf(wlLogRecord.getSequenceNumber()));
38+
MDC.put(FIELDS_DOMAIN_UID, domainUID);
39+
40+
String result = super.format(wlLogRecord);
41+
42+
// Can't clear the whole MDC HashMap as there might be other records in there.
43+
MDC.remove(FIELDS_MESSAGE_ID);
44+
MDC.remove(FIELDS_SERVER_NAME);
45+
MDC.remove(FIELDS_USER_ID);
46+
MDC.remove(FIELDS_SUB_SYSTEM);
47+
MDC.remove(FIELDS_MACHINE_NAME);
48+
MDC.remove(FIELDS_TRANSACTION_ID);
49+
MDC.remove(FIELDS_DIAGNOSTIC_CONTEXT_ID);
50+
MDC.remove(FIELDS_SEQUENCE_NUMBER);
51+
MDC.remove(FIELDS_DOMAIN_UID);
52+
53+
return result;
54+
}
55+
}

0 commit comments

Comments
 (0)