From 6b3b24a073a43ce54a7b8add22a965970b996cf9 Mon Sep 17 00:00:00 2001 From: James Perkins Date: Fri, 29 Sep 2017 11:56:03 -0700 Subject: [PATCH] [LOGMGR-155] Remove the logstash formatter. This is simple enough to achieve with just the JsonFormatter. --- .../formatters/LogstashFormatter.java | 114 ---------- .../formatters/LogstashFormatterTests.java | 206 ------------------ 2 files changed, 320 deletions(-) delete mode 100644 src/main/java/org/jboss/logmanager/formatters/LogstashFormatter.java delete mode 100644 src/test/java/org/jboss/logmanager/formatters/LogstashFormatterTests.java diff --git a/src/main/java/org/jboss/logmanager/formatters/LogstashFormatter.java b/src/main/java/org/jboss/logmanager/formatters/LogstashFormatter.java deleted file mode 100644 index a8d509d6..00000000 --- a/src/main/java/org/jboss/logmanager/formatters/LogstashFormatter.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * - * Copyright 2017 Red Hat, Inc., and individual contributors - * as indicated by the @author tags. - * - * Licensed 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. - */ - -package org.jboss.logmanager.formatters; - -import java.util.Collections; -import java.util.EnumMap; -import java.util.Locale; -import java.util.Map; - -import org.jboss.logmanager.ExtLogRecord; -import org.jboss.logmanager.PropertyValues; - -/** - * A {@link JsonFormatter JSON formatter} which adds the {@code @version} to the generated JSON and overrides the - * {@code timestamp} key to {@code @timestamp}. - *

- * The default {@link #getVersion() version} is {@code 1}. - *

- * - * @author James R. Perkins - */ -@SuppressWarnings({"WeakerAccess", "unused"}) -public class LogstashFormatter extends JsonFormatter { - - private volatile int version = 1; - - - /** - * Creates a new logstash formatter. - */ - public LogstashFormatter() { - this(Collections.singletonMap(Key.TIMESTAMP, "@timestamp")); - } - - /** - * Creates a new logstash formatter. - * - * @param keyOverrides a string representation of a map to override keys - * - * @see PropertyValues#stringToEnumMap(Class, String) - */ - public LogstashFormatter(final String keyOverrides) { - super(keyOverrides(keyOverrides)); - } - - /** - * Creates a new logstash formatter. - * - * @param keyOverrides a map of overrides for the default keys - */ - public LogstashFormatter(final Map keyOverrides) { - super(keyOverrides(keyOverrides)); - } - - @Override - protected void before(final Generator generator, final ExtLogRecord record) throws Exception { - generator.add("@version", version); - } - - /** - * Returns the version being used for the {@code @version} property. - * - * @return the version being used - */ - public int getVersion() { - return version; - } - - /** - * Sets the version to use for the {@code @version} property. - * - * @param version the version to use - */ - public void setVersion(final int version) { - this.version = version; - } - - private static Map keyOverrides(final Map keyOverrides) { - if (keyOverrides.containsKey(Key.TIMESTAMP)) { - return keyOverrides; - } - final EnumMap result = new EnumMap<>(Key.class); - result.putAll(keyOverrides); - result.put(Key.TIMESTAMP, "@timestamp"); - return result; - } - - private static String keyOverrides(final String keyOverrides) { - if (keyOverrides == null || keyOverrides.trim().isEmpty()) { - return Key.TIMESTAMP.name() + "=@timestamp"; - } - if (keyOverrides.toUpperCase(Locale.ROOT).contains(Key.TIMESTAMP.name())) { - return keyOverrides; - } - return keyOverrides + "," + Key.TIMESTAMP.name() + "=@timestamp"; - } -} diff --git a/src/test/java/org/jboss/logmanager/formatters/LogstashFormatterTests.java b/src/test/java/org/jboss/logmanager/formatters/LogstashFormatterTests.java deleted file mode 100644 index 72bd35d7..00000000 --- a/src/test/java/org/jboss/logmanager/formatters/LogstashFormatterTests.java +++ /dev/null @@ -1,206 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * - * Copyright 2017 Red Hat, Inc., and individual contributors - * as indicated by the @author tags. - * - * Licensed 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. - */ - -package org.jboss.logmanager.formatters; - -import java.io.StringReader; -import java.time.Instant; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Consumer; -import javax.json.Json; -import javax.json.JsonObject; -import javax.json.JsonReader; -import javax.json.JsonValue; -import javax.json.JsonValue.ValueType; - -import org.jboss.logmanager.ExtFormatter; -import org.jboss.logmanager.ExtLogRecord; -import org.jboss.logmanager.Level; -import org.jboss.logmanager.formatters.StructuredFormatter.Key; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -/** - * @author James R. Perkins - */ -public class LogstashFormatterTests extends AbstractStructuredFormatterTest { - private static final Map KEY_OVERRIDES = new HashMap<>(); - private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter - .ISO_OFFSET_DATE_TIME - .withZone(ZoneId.systemDefault()); - - @Before - public void before() { - KEY_OVERRIDES.clear(); - } - - @Test - public void testLogstashFormat() throws Exception { - KEY_OVERRIDES.put(Key.TIMESTAMP, "@timestamp"); - final LogstashFormatter formatter = new LogstashFormatter(); - formatter.setPrintDetails(true); - ExtLogRecord record = createLogRecord("Test formatted %s", "message"); - compareLogstash(record, formatter, 1); - - record = createLogRecord("Test Message"); - formatter.setVersion(2); - compareLogstash(record, formatter, 2); - - record = createLogRecord(Level.ERROR, "Test formatted %s", "message"); - record.setLoggerName("org.jboss.logmanager.ext.test"); - record.setMillis(System.currentTimeMillis()); - record.setThrown(new RuntimeException("Test Exception")); - record.putMdc("testMdcKey", "testMdcValue"); - record.setNdc("testNdc"); - compareLogstash(record, formatter, 2); - } - - @Override - Class getFormatterType() { - return LogstashFormatter.class; - } - - @Override - void compare(final Map> expectedValues, final String formattedMessage) { - try (JsonReader reader = Json.createReader(new StringReader(formattedMessage))) { - final JsonObject json = reader.readObject(); - final Iterator>> iterator = expectedValues.entrySet().iterator(); - while (iterator.hasNext()) { - final Map.Entry> entry = iterator.next(); - final String key = entry.getKey(); - final JsonValue jsonValue = json.get(key); - if (jsonValue.getValueType() == ValueType.STRING) { - entry.getValue().accept(json.getString(key)); - } else if (jsonValue.getValueType() == ValueType.NUMBER) { - entry.getValue().accept(json.getJsonNumber(key).toString()); - } else if (jsonValue.getValueType() == ValueType.NULL) { - entry.getValue().accept(null); - } else { - Assert.fail(String.format("Type %s is not implemented: %s", jsonValue.getValueType(), jsonValue)); - } - iterator.remove(); - } - Assert.assertTrue("Expected map to be empty " + expectedValues, expectedValues.isEmpty()); - } - } - - @Override - Map> createExpectedValues() { - final Map> expectedValues = super.createExpectedValues(); - expectedValues.put("@version", value -> { - final int version = Integer.parseInt(value); - Assert.assertEquals(1, version); - }); - return expectedValues; - } - - private static int getInt(final JsonObject json, final Key key) { - final String name = getKey(key); - if (json.containsKey(name) && !json.isNull(name)) { - return json.getInt(name); - } - return 0; - } - - private static long getLong(final JsonObject json, final Key key) { - final String name = getKey(key); - if (json.containsKey(name) && !json.isNull(name)) { - return json.getJsonNumber(name).longValue(); - } - return 0L; - } - - private static String getString(final JsonObject json, final Key key) { - final String name = getKey(key); - if (json.containsKey(name) && !json.isNull(name)) { - return json.getString(name); - } - return null; - } - - private static Map getMap(final JsonObject json, final Key key) { - final String name = getKey(key); - if (json.containsKey(name) && !json.isNull(name)) { - final Map result = new LinkedHashMap<>(); - final JsonObject mdcObject = json.getJsonObject(name); - for (String k : mdcObject.keySet()) { - final JsonValue value = mdcObject.get(k); - if (value.getValueType() == ValueType.STRING) { - result.put(k, value.toString().replace("\"", "")); - } else { - result.put(k, value.toString()); - } - } - return result; - } - return Collections.emptyMap(); - } - - private static String getKey(final Key key) { - if (KEY_OVERRIDES.containsKey(key)) { - return KEY_OVERRIDES.get(key); - } - return key.getKey(); - } - - private static void compareLogstash(final ExtLogRecord record, final ExtFormatter formatter, final int version) { - compareLogstash(record, formatter.format(record), version); - } - - private static void compareLogstash(final ExtLogRecord record, final String jsonString, final int version) { - try (JsonReader reader = Json.createReader(new StringReader(jsonString))) { - final JsonObject json = reader.readObject(); - compare(record, json); - final String name = "@version"; - int foundVersion = 0; - if (json.containsKey(name) && !json.isNull(name)) { - foundVersion = json.getInt(name); - } - Assert.assertEquals(version, foundVersion); - } - } - - private static void compare(final ExtLogRecord record, final JsonObject json) { - Assert.assertEquals(record.getLevel(), Level.parse(getString(json, Key.LEVEL))); - Assert.assertEquals(record.getLoggerClassName(), getString(json, Key.LOGGER_CLASS_NAME)); - Assert.assertEquals(record.getLoggerName(), getString(json, Key.LOGGER_NAME)); - compareMaps(record.getMdcCopy(), getMap(json, Key.MDC)); - Assert.assertEquals(record.getFormattedMessage(), getString(json, Key.MESSAGE)); - Assert.assertEquals(DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(record.getMillis())), - getString(json, Key.TIMESTAMP)); - Assert.assertEquals(record.getNdc(), getString(json, Key.NDC)); - // Assert.assertEquals(record.getResourceBundle()); - // Assert.assertEquals(record.getResourceBundleName()); - // Assert.assertEquals(record.getResourceKey()); - Assert.assertEquals(record.getSequenceNumber(), getLong(json, Key.SEQUENCE)); - Assert.assertEquals(record.getSourceClassName(), getString(json, Key.SOURCE_CLASS_NAME)); - Assert.assertEquals(record.getSourceFileName(), getString(json, Key.SOURCE_FILE_NAME)); - Assert.assertEquals(record.getSourceLineNumber(), getInt(json, Key.SOURCE_LINE_NUMBER)); - Assert.assertEquals(record.getSourceMethodName(), getString(json, Key.SOURCE_METHOD_NAME)); - Assert.assertEquals(record.getThreadID(), getInt(json, Key.THREAD_ID)); - Assert.assertEquals(record.getThreadName(), getString(json, Key.THREAD_NAME)); - } -}