Skip to content

Commit

Permalink
Merge pull request #141 from jamezp/LOGMGR-155
Browse files Browse the repository at this point in the history
[LOGMGR-155] Follow up with clean up and removal of the Logstash formatter
  • Loading branch information
jamezp committed Oct 2, 2017
2 parents aa9959c + 6b3b24a commit 5735714
Show file tree
Hide file tree
Showing 14 changed files with 1,114 additions and 428 deletions.
424 changes: 424 additions & 0 deletions src/main/java/org/jboss/logmanager/PropertyValues.java

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions src/main/java/org/jboss/logmanager/formatters/JsonFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonValue;
import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonGeneratorFactory;

import org.jboss.logmanager.PropertyValues;

/**
* A formatter that outputs the record into JSON format optionally printing details.
* <p>
Expand All @@ -56,7 +57,21 @@ public class JsonFormatter extends StructuredFormatter {
* Creates a new JSON formatter.
*/
public JsonFormatter() {
this(Collections.emptyMap());
config = new HashMap<>();
factory = Json.createGeneratorFactory(config);
}

/**
* Creates a new JSON formatter.
*
* @param keyOverrides a string representation of a map to override keys
*
* @see PropertyValues#stringToEnumMap(Class, String)
*/
public JsonFormatter(final String keyOverrides) {
super(keyOverrides);
config = new HashMap<>();
factory = Json.createGeneratorFactory(config);
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.IdentityHashMap;
import java.util.Map;

import org.jboss.logmanager.ExtFormatter;
import org.jboss.logmanager.ExtLogRecord;
import org.jboss.logmanager.PropertyValues;

/**
* An abstract class that uses a generator to help generate structured data from a {@link
Expand All @@ -40,7 +41,7 @@
* Note that including details can be expensive in terms of calculating the caller.
* </p>
* <p>
* By default the {@linkplain #setEndOfRecordDelimiter(String) record delimiter} is set to {@code \n}.
* By default the {@linkplain #setRecordDelimiter(String) record delimiter} is set to {@code \n}.
* </p>
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
Expand Down Expand Up @@ -121,6 +122,7 @@ public enum ExceptionOutputType {
}

private final Map<Key, String> keyOverrides;
private final String keyOverridesValue;
// Guarded by this
private String metaData;
// Guarded by this
Expand All @@ -137,14 +139,23 @@ public enum ExceptionOutputType {
private int refId;

protected StructuredFormatter() {
this(null);
this(null, null);
}

protected StructuredFormatter(final Map<Key, String> keyOverrides) {
this(keyOverrides, PropertyValues.mapToString(keyOverrides));
}

protected StructuredFormatter(final String keyOverrides) {
this(PropertyValues.stringToEnumMap(Key.class, keyOverrides), keyOverrides);
}

private StructuredFormatter(final Map<Key, String> keyOverrides, final String keyOverridesValue) {
this.keyOverridesValue = keyOverridesValue;
this.printDetails = false;
zoneId = ZoneId.systemDefault();
dateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(zoneId);
this.keyOverrides = (keyOverrides == null ? Collections.emptyMap() : new HashMap<>(keyOverrides));
this.keyOverrides = (keyOverrides == null ? Collections.emptyMap() : new EnumMap<>(keyOverrides));
exceptionOutputType = ExceptionOutputType.DETAILED;
}

Expand Down Expand Up @@ -240,26 +251,23 @@ public final synchronized String format(final ExtLogRecord record) {
generator.add(getKey(Key.STACK_TRACE), w.toString());
}
}

// Print any user meta-data
if (isNotNullOrEmpty(metaData)) {
for (String key : metaDataMap.keySet()) {
generator.add(key, metaDataMap.get(key));
}
}
if (details) {
generator.add(getKey(Key.SOURCE_CLASS_NAME), record.getSourceClassName())
.add(getKey(Key.SOURCE_FILE_NAME), record.getSourceFileName())
.add(getKey(Key.SOURCE_METHOD_NAME), record.getSourceMethodName())
.add(getKey(Key.SOURCE_LINE_NUMBER), record.getSourceLineNumber());
}

if (!isNotNullOrEmpty(metaData)) {
generator.addMetaData(metaDataMap);
}

after(generator, record);
generator.end();

// Append an EOL character if desired
if (getEndOfRecordDelimiter() != null) {
writer.append(getEndOfRecordDelimiter());
if (getRecordDelimiter() != null) {
writer.append(getRecordDelimiter());
}
return writer.toString();
} catch (Exception e) {
Expand All @@ -276,13 +284,22 @@ public boolean isCallerCalculationRequired() {
return isPrintDetails();
}

/**
* A string representation of the key overrides. The default is {@code null}.
*
* @return a string representation of the key overrides or {@code null} if no overrides were configured
*/
public String getKeyOverrides() {
return keyOverridesValue;
}

/**
* Returns the character used to indicate the record has is complete. This defaults to {@code \n} and may be
* {@code null} if no end of record character is desired.
*
* @return the end of record delimiter or {@code null} if no delimiter is desired
*/
public String getEndOfRecordDelimiter() {
public String getRecordDelimiter() {
return eorDelimiter;
}

Expand All @@ -292,7 +309,7 @@ public String getEndOfRecordDelimiter() {
*
* @param eorDelimiter the delimiter to be used or {@code null} to not use a delimiter
*/
public void setEndOfRecordDelimiter(final String eorDelimiter) {
public void setRecordDelimiter(final String eorDelimiter) {
this.eorDelimiter = eorDelimiter;
}

Expand All @@ -305,7 +322,7 @@ public void setEndOfRecordDelimiter(final String eorDelimiter) {
*
* @return the meta data string or {@code null} if one was not set
*
* @see ValueParser#stringToMap(String)
* @see PropertyValues#stringToMap(String)
*/
public String getMetaData() {
return metaData;
Expand All @@ -320,11 +337,11 @@ public String getMetaData() {
*
* @param metaData the meta data to set or {@code null} to not format any meta data
*
* @see ValueParser#stringToMap(String)
* @see PropertyValues#stringToMap(String)
*/
public synchronized void setMetaData(final String metaData) {
this.metaData = metaData;
metaDataMap = ValueParser.stringToMap(metaData);
metaDataMap = PropertyValues.stringToMap(metaData);
}

/**
Expand Down Expand Up @@ -599,6 +616,25 @@ default Generator add(final String key, final long value) throws Exception {
*/
Generator add(String key, String value) throws Exception;

/**
* Adds the meta data to the structured format.
* <p>
* By default this processes the map and uses {@link #add(String, String)} to add entries.
* </p>
*
* @param metaData the matp of the meta data, cannot be {@code null}
*
* @return the generator
*
* @throws Exception if an error occurs while adding the data
*/
default Generator addMetaData(final Map<String, String> metaData) throws Exception {
for (String key : metaData.keySet()) {
add(key, metaData.get(key));
}
return this;
}

/**
* Writes the start of an object.
* <p>
Expand Down

0 comments on commit 5735714

Please sign in to comment.