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

Commit f626774

Browse files
Nick WemekampNick Wemekamp
Nick Wemekamp
authored and
Nick Wemekamp
committed
Added support for logging in JSON format to a file.
Signed-off-by: Nick Wemekamp <[email protected]>
1 parent aba248a commit f626774

File tree

6 files changed

+133
-36
lines changed

6 files changed

+133
-36
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,15 @@ This section outlines the steps that are required to add the Weblogic Logging Ex
125125
you can find this JAR file in your local maven repository at `~/.m2/repository/org/yaml/snakeyaml/1.27/snakeyaml-1.27.jar`.
126126
Otherwise, you can download it from [Maven Central](https://search.maven.org/artifact/org.yaml/snakeyaml/1.27/bundle).
127127
128-
Place this file in a suitable location, e.g. your domain directory.
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)
129133
130-
Update the server classpath to include these two files. This can be done by adding a statement to the end of your
134+
Place the file(s) in a suitable location, e.g. your domain directory.
135+
136+
Update the server classpath to include these file(s). This can be done by adding a statement to the end of your
131137
`setDomainEnv.sh` script in your domain's `bin` directory as follows (this example assumes your domain
132138
directory is `/u01/base_domain`):
133139
@@ -163,7 +169,10 @@ This section outlines the steps that are required to add the Weblogic Logging Ex
163169
If you prefer to place the configuration file in a different location, you can set the environment variable
164170
`WEBLOGIC_LOGGING_EXPORTER_CONFIG_FILE` to point to the location of the file.
165171
166-
1. Restart the servers to activate the changes. After restarting the servers, they will load the WebLogic
172+
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.
174+
175+
6. Restart the servers to activate the changes. After restarting the servers, they will load the WebLogic
167176
Logging Exporter and start sending their logs to the specified Elasticsearch instance. You can then
168177
access them in Kibana as shown in the example below. You will need to create an index first and then go to
169178
the visualization page.

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
</plugin>
6464
<!-- for junit 5 -->
6565
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
6667
<artifactId>maven-surefire-plugin</artifactId>
6768
<version>2.22.2</version>
6869
<configuration>
@@ -85,6 +86,7 @@
8586
</configuration>
8687
</plugin>
8788
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
8890
<artifactId>maven-failsafe-plugin</artifactId>
8991
<version>2.22.1</version>
9092
</plugin>
@@ -190,6 +192,11 @@
190192
<artifactId>snakeyaml</artifactId>
191193
<version>1.27</version>
192194
</dependency>
195+
<dependency>
196+
<groupId>co.elastic.logging</groupId>
197+
<artifactId>jul-ecs-formatter</artifactId>
198+
<version>1.1.0</version>
199+
</dependency>
193200
<dependency>
194201
<groupId>org.antlr</groupId>
195202
<artifactId>antlr-complete</artifactId>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# turn off the elasticsearch output
2+
weblogicLoggingExporterEnabled: false
3+
4+
# configure json logging output
5+
writeToFileEnabled: true
6+
# %g is necessary for rollback of log files.
7+
outputFile: '/var/log/oracle/json-logging%g.log'
8+
# Max open files
9+
maxRollbackFiles: 3
10+
# Max file size in bytes, this is 50 MB
11+
maxFileSize: 52428800
12+
# Should the log file be appended to when the new logger is created
13+
appendToFile: true
14+
# Optional configuration for specifying which levels get logged to the json log file.
15+
# fileLoggingLogLevel: INFO

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
package weblogic.logging.exporter;
55

66
import java.io.File;
7+
import java.util.logging.FileHandler;
8+
import java.util.logging.Level;
79
import java.util.logging.Logger;
10+
11+
import co.elastic.logging.jul.EcsFormatter;
812
import weblogic.logging.LoggingHelper;
13+
import weblogic.logging.ServerLoggingHandler;
914
import weblogic.logging.exporter.config.Config;
1015

1116
public class Startup {
@@ -15,7 +20,6 @@ public class Startup {
1520
public static void main(String[] argv) {
1621
System.out.println("======================= Weblogic Logging Exporter Startup class called");
1722
try {
18-
// Logger logger = LoggingHelper.getDomainLogger();
1923
Logger logger = LoggingHelper.getServerLogger();
2024

2125
/*
@@ -41,12 +45,26 @@ public static void main(String[] argv) {
4145
System.out.println("Reading configuration from file name: " + file.getAbsolutePath());
4246
Config config = Config.loadConfig(file);
4347
System.out.println(config);
48+
49+
// Elastic log handler is enabled or the file log handler
4450
if (config.getEnabled()) {
4551
logger.addHandler(new LogExportHandler(config));
46-
} else {
47-
System.out.println("WebLogic Logging Exporter is disabled");
52+
} else if (config.isFileLoggingEnabled()) {
53+
// Because of this bridge log messages in the applications themselves are being forwarded to the server logger.
54+
// so that logging in ear/war artifacts are also visible to the server logger and appear in the JSON log file.
55+
Logger.getLogger("").addHandler(new ServerLoggingHandler());
56+
57+
// Register a file handler using the provided config
58+
FileHandler fh = new FileHandler(config.getOutputFile(), config.getMaxFileSize(), config.getGetMaxRollbackFiles(), config.getAppendToFile());
59+
fh.setLevel(Level.parse(config.getFileLoggingLogLevel()));
60+
fh.setFormatter(new EcsFormatter());
61+
logger.addHandler(fh);
62+
} else {
63+
System.out.println("WebLogic Elasticsearch Logging Exporter is disabled");
4864
}
49-
} catch (Exception e) {
65+
// also catch errors so that Weblogic does not crash when a required library was not placed in the classpath correctly.
66+
} catch (Error | Exception e) {
67+
System.out.println("======================= Something went wrong, the Weblogic Logging Exporter is not activated");
5068
e.printStackTrace();
5169
}
5270
}

src/main/java/weblogic/logging/exporter/config/Config.java

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public class Config {
3131
private static final String INDEX_NAME = "weblogicLoggingIndexName";
3232
private static final String DOMAIN_UID = "domainUID";
3333

34+
private static final String WRITE_TO_FILE_ENABLED = "writeToFileEnabled";
35+
private static final String OUTPUT_FILE = "outputFile";
36+
private static final String MAX_ROLLBACK_FILES = "maxRollbackFiles";
37+
private static final String MAX_FILE_SIZE = "maxFileSize";
38+
private static final String APPEND_TO_FILE = "appendToFile";
39+
private static final String FILE_LOGGING_LOG_LEVEL = "fileLoggingLogLevel";
40+
3441
private String host = DEFAULT_HOST;
3542
private int port = DEFAULT_PORT;
3643
private String indexName = DEFAULT_INDEX_NAME;
@@ -40,6 +47,14 @@ public class Config {
4047
private final List<FilterConfig> filterConfigs = new ArrayList<>();
4148
private String domainUID = DEFAULT_DOMAIN_UID;
4249

50+
private boolean fileLoggingEnabled;
51+
private String outputFile;
52+
private Integer getMaxRollbackFiles;
53+
private Integer maxFileSize;
54+
private boolean appendToFile;
55+
56+
private String fileLoggingLogLevel = "INFO";
57+
4358
private Config() {}
4459

4560
private Config(Map<String, Object> yaml) {
@@ -76,6 +91,26 @@ private Config(Map<String, Object> yaml) {
7691
System.out.println("Index name is converted to all lower case : " + indexName);
7792
}
7893
if (yaml.containsKey(FILTERS)) appendFilters(yaml.get(FILTERS));
94+
95+
// File output
96+
if (yaml.containsKey(WRITE_TO_FILE_ENABLED)) {
97+
fileLoggingEnabled = MapUtils.getBooleanValue(yaml, WRITE_TO_FILE_ENABLED);
98+
}
99+
if (yaml.containsKey(OUTPUT_FILE)) {
100+
outputFile = MapUtils.getStringValue(yaml, OUTPUT_FILE);
101+
}
102+
if (yaml.containsKey(MAX_ROLLBACK_FILES)) {
103+
getMaxRollbackFiles = MapUtils.getIntegerValue(yaml, MAX_ROLLBACK_FILES);
104+
}
105+
if (yaml.containsKey(MAX_FILE_SIZE)) {
106+
maxFileSize = MapUtils.getIntegerValue(yaml, MAX_FILE_SIZE);
107+
}
108+
if (yaml.containsKey(APPEND_TO_FILE)) {
109+
appendToFile = MapUtils.getBooleanValue(yaml, APPEND_TO_FILE);
110+
}
111+
if (yaml.containsKey(FILE_LOGGING_LOG_LEVEL)) {
112+
fileLoggingLogLevel = MapUtils.getStringValue(yaml, FILE_LOGGING_LOG_LEVEL);
113+
}
79114
}
80115

81116
public static Config loadConfig(File file) {
@@ -143,29 +178,22 @@ private static Config loadConfig(Map<String, Object> yamlConfig) {
143178

144179
@Override
145180
public String toString() {
146-
return "Config{"
147-
+ "weblogicLoggingIndexName='"
148-
+ indexName
149-
+ '\''
150-
+ ", publishHost='"
151-
+ host
152-
+ '\''
153-
+ ", publishPort="
154-
+ port
155-
+ ", weblogicLoggingExporterSeverity='"
156-
+ severity
157-
+ '\''
158-
+ ", weblogicLoggingExporterBulkSize='"
159-
+ bulkSize
160-
+ '\''
161-
+ ", enabled="
162-
+ enabled
163-
+ ", weblogicLoggingExporterFilters="
164-
+ filterConfigs
165-
+ ", domainUID='"
166-
+ domainUID
167-
+ '\''
168-
+ '}';
181+
return "Config{" +
182+
"host='" + host + '\'' +
183+
", port=" + port +
184+
", indexName='" + indexName + '\'' +
185+
", bulkSize=" + bulkSize +
186+
", enabled=" + enabled +
187+
", severity='" + severity + '\'' +
188+
", filterConfigs=" + filterConfigs +
189+
", domainUID='" + domainUID + '\'' +
190+
", fileLoggingEnabled=" + fileLoggingEnabled +
191+
", outputFile='" + outputFile + '\'' +
192+
", getMaxRollbackFiles=" + getMaxRollbackFiles +
193+
", maxFileSize=" + maxFileSize +
194+
", appendToFile=" + appendToFile +
195+
", fileLoggingLogLevel='" + fileLoggingLogLevel + '\'' +
196+
'}';
169197
}
170198

171199
public String getHost() {
@@ -199,4 +227,28 @@ public int getBulkSize() {
199227
public String getDomainUID() {
200228
return domainUID;
201229
}
230+
231+
public boolean isFileLoggingEnabled() {
232+
return fileLoggingEnabled;
233+
}
234+
235+
public String getOutputFile() {
236+
return outputFile;
237+
}
238+
239+
public Integer getGetMaxRollbackFiles() {
240+
return getMaxRollbackFiles;
241+
}
242+
243+
public Integer getMaxFileSize() {
244+
return maxFileSize;
245+
}
246+
247+
public boolean getAppendToFile() {
248+
return appendToFile;
249+
}
250+
251+
public String getFileLoggingLogLevel() {
252+
return fileLoggingLogLevel;
253+
}
202254
}

src/test/java/weblogic/logging/exporter/config/ConfigTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ public class ConfigTest {
2222
private final PrintStream originalOut = System.out;
2323
private final PrintStream originalErr = System.err;
2424

25-
private static final String EXPECTED_STRING =
26-
"Config{weblogicLoggingIndexName='index1', publishHost='host1', "
27-
+ "publishPort=1234, weblogicLoggingExporterSeverity='Warning', "
28-
+ "weblogicLoggingExporterBulkSize='2', enabled=false, "
29-
+ "weblogicLoggingExporterFilters=[FilterConfig{expression='MSGID != 'BEA-000449'', "
30-
+ "servers=[]}], domainUID='domain1'}";
25+
private static final String EXPECTED_STRING = "Config{host='host1', port=1234, indexName='index1', bulkSize=2, enabled=false, severity='Warning', filterConfigs=[FilterConfig{expression='MSGID != 'BEA-000449'', servers=[]}], domainUID='domain1', fileLoggingEnabled=false, outputFile='null', getMaxRollbackFiles=null, maxFileSize=null, appendToFile=false, fileLoggingLogLevel='INFO'}";
3126

3227
@BeforeEach
3328
public void setUpStreams() {
@@ -115,6 +110,7 @@ public void shouldConvertIndexNameToLowerCase() {
115110
@Test
116111
public void checkToStringWorksAsExpected() {
117112
Config config = Config.loadConfig(new File("src/test/resources/config1.yaml"));
113+
System.out.println(config.toString());
118114
assertEquals(EXPECTED_STRING, config.toString());
119115
}
120116
}

0 commit comments

Comments
 (0)