-
Notifications
You must be signed in to change notification settings - Fork 82
ECS formatter for JUL (java.util.logging) #69
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# ECS formatter for JUL | ||
|
||
Formatter for JUL (java.util.logging) which produce ECS-compatible records. May be useful for applications which use JUL as primary logging framework (e.g. Apache Tomcat). | ||
|
||
## Step 1: add dependency | ||
|
||
Latest version: [](https://search.maven.org/search?q=g:co.elastic.logging%20AND%20a:jul-ecs-formatter) | ||
|
||
Add a dependency to your application | ||
```xml | ||
<dependency> | ||
<groupId>co.elastic.logging</groupId> | ||
<artifactId>jul-ecs-formatter</artifactId> | ||
<version>${ecs-logging-java.version}</version> | ||
</dependency> | ||
``` | ||
If you are not using a dependency management tool, like maven, you have to add both, `jul-ecs-formatter` and `ecs-logging-core` jars manually to the classpath. For example to the `$CATALINA_HOME/lib` directory. | ||
|
||
## Step 2: use the `EcsFormatter` | ||
|
||
Specify `co.elastic.logging.jul.EcsFormatter` as `formatter` for the required log handler. | ||
|
||
## Example | ||
For example `$CATALINA_HOME/conf/logging.properties` | ||
|
||
```properties | ||
java.util.logging.ConsoleHandler.level = FINE | ||
java.util.logging.ConsoleHandler.formatter = co.elastic.logging.jul.EcsFormatter | ||
co.elastic.logging.jul.EcsFormatter.serviceName=my-app | ||
``` | ||
|
||
## Layout Parameters | ||
|
||
|Parameter name |Type |Default|Description| | ||
|-----------------|-------|-------|-----------| | ||
|serviceName |String | |Sets the `service.name` field so you can filter your logs by a particular service | | ||
|eventDataset |String |`${serviceName}.log`|Sets the `event.dataset` field used by the machine learning job of the Logs app to look for anomalies in the log rate. | | ||
|stackTraceAsArray|boolean|`false`|Serializes the [`error.stack_trace`](https://www.elastic.co/guide/en/ecs/current/ecs-error.html) as a JSON array where each element is in a new line to improve readability. Note that this requires a slightly more complex [Filebeat configuration](../README.md#when-stacktraceasarray-is-enabled).| | ||
|includeOrigin |boolean|`false`|If `true`, adds the [`log.origin.file.name`](https://www.elastic.co/guide/en/ecs/current/ecs-log.html), [`log.origin.file.line`](https://www.elastic.co/guide/en/ecs/current/ecs-log.html) and [`log.origin.function`](https://www.elastic.co/guide/en/ecs/current/ecs-log.html) fields. Note that JUL does not stores line number and `log.origin.file.line` will have '1' value. | | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>ecs-logging-java-parent</artifactId> | ||
<groupId>co.elastic.logging</groupId> | ||
<version>0.3.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<properties> | ||
<parent.base.dir>${project.basedir}/..</parent.base.dir> | ||
</properties> | ||
|
||
<artifactId>jul-ecs-formatter</artifactId> | ||
<name>${project.groupId}:${project.artifactId}</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>ecs-logging-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>ecs-logging-core</artifactId> | ||
<version>${project.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
116 changes: 116 additions & 0 deletions
116
jul-ecs-formatter/src/main/java/co/elastic/logging/jul/EcsFormatter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/*- | ||
* #%L | ||
* Java ECS logging | ||
* %% | ||
* Copyright (C) 2019 - 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.logging.jul; | ||
|
||
import java.util.logging.Formatter; | ||
import java.util.logging.LogManager; | ||
import java.util.logging.LogRecord; | ||
|
||
import co.elastic.logging.EcsJsonSerializer; | ||
|
||
public class EcsFormatter extends Formatter { | ||
|
||
private static final String UNKNOWN_FILE = "<Unknown>"; | ||
|
||
private boolean stackTraceAsArray; | ||
private String serviceName; | ||
private boolean includeOrigin; | ||
private String eventDataset; | ||
|
||
/** | ||
* Default constructor. Will read configuration from LogManager properties. | ||
*/ | ||
public EcsFormatter() { | ||
serviceName = getProperty("co.elastic.logging.jul.EcsFormatter.serviceName", null); | ||
includeOrigin = Boolean.getBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.includeOrigin", "false")); | ||
stackTraceAsArray = Boolean | ||
.getBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.stackTraceAsArray", "false")); | ||
eventDataset = getProperty("co.elastic.logging.jul.EcsFormatter.eventDataset", null); | ||
eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName); | ||
} | ||
|
||
@Override | ||
public String format(final LogRecord record) { | ||
final StringBuilder builder = new StringBuilder(); | ||
EcsJsonSerializer.serializeObjectStart(builder, record.getMillis()); | ||
EcsJsonSerializer.serializeLogLevel(builder, record.getLevel().getName()); | ||
EcsJsonSerializer.serializeFormattedMessage(builder, record.getMessage()); | ||
EcsJsonSerializer.serializeServiceName(builder, serviceName); | ||
EcsJsonSerializer.serializeEventDataset(builder, eventDataset); | ||
EcsJsonSerializer.serializeThreadId(builder, record.getThreadID()); | ||
EcsJsonSerializer.serializeLoggerName(builder, record.getLoggerName()); | ||
if (includeOrigin && record.getSourceClassName() != null && record.getSourceMethodName() != null) { | ||
EcsJsonSerializer.serializeOrigin(builder, buildFileName(record.getSourceClassName()), record.getSourceMethodName(), -1); | ||
} | ||
final Throwable throwableInformation = record.getThrown(); | ||
if (throwableInformation != null) { | ||
EcsJsonSerializer.serializeException(builder, throwableInformation, stackTraceAsArray); | ||
} | ||
EcsJsonSerializer.serializeObjectEnd(builder); | ||
return builder.toString(); | ||
} | ||
|
||
protected void setIncludeOrigin(final boolean includeOrigin) { | ||
this.includeOrigin = includeOrigin; | ||
} | ||
|
||
protected void setServiceName(final String serviceName) { | ||
this.serviceName = serviceName; | ||
} | ||
|
||
protected void setStackTraceAsArray(final boolean stackTraceAsArray) { | ||
this.stackTraceAsArray = stackTraceAsArray; | ||
} | ||
|
||
public void setEventDataset(String eventDataset) { | ||
this.eventDataset = eventDataset; | ||
} | ||
|
||
private String getProperty(final String name, final String defaultValue) { | ||
String value = LogManager.getLogManager().getProperty(name); | ||
if (value == null) { | ||
value = defaultValue; | ||
} else { | ||
value = value.trim(); | ||
} | ||
return value; | ||
} | ||
|
||
private String buildFileName(String className) { | ||
String result = UNKNOWN_FILE; | ||
if (className != null) { | ||
int fileNameEnd = className.indexOf('$'); | ||
if (fileNameEnd < 0) { | ||
fileNameEnd = className.length(); | ||
} | ||
int classNameStart = className.lastIndexOf('.'); | ||
if (classNameStart < fileNameEnd) { | ||
result = className.substring(classNameStart + 1, fileNameEnd) + ".java"; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ecs-logging-java | ||
Copyright 2019 - 2020 Elasticsearch B.V. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.