|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Java ECS logging |
| 4 | + * %% |
| 5 | + * Copyright (C) 2019 - 2020 Elastic and contributors |
| 6 | + * %% |
| 7 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 8 | + * license agreements. See the NOTICE file distributed with |
| 9 | + * this work for additional information regarding copyright |
| 10 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 11 | + * the Apache License, Version 2.0 (the "License"); you may |
| 12 | + * not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, |
| 18 | + * software distributed under the License is distributed on an |
| 19 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 20 | + * KIND, either express or implied. See the License for the |
| 21 | + * specific language governing permissions and limitations |
| 22 | + * under the License. |
| 23 | + * #L% |
| 24 | + */ |
| 25 | +package co.elastic.logging.jboss.logmanager; |
| 26 | + |
| 27 | +import co.elastic.logging.EcsJsonSerializer; |
| 28 | +import org.jboss.logmanager.ExtFormatter; |
| 29 | +import org.jboss.logmanager.ExtLogRecord; |
| 30 | +import org.jboss.logmanager.LogManager; |
| 31 | + |
| 32 | +public class EcsFormatter extends ExtFormatter { |
| 33 | + |
| 34 | + private String serviceName; |
| 35 | + private String eventDataset; |
| 36 | + private boolean includeOrigin; |
| 37 | + private boolean stackTraceAsArray; |
| 38 | + |
| 39 | + public EcsFormatter() { |
| 40 | + serviceName = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.serviceName", null); |
| 41 | + eventDataset = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.eventDataset", null); |
| 42 | + eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName); |
| 43 | + includeOrigin = Boolean.getBoolean(getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.includeOrigin", "false")); |
| 44 | + stackTraceAsArray = Boolean.getBoolean(getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.stackTraceAsArray", "false")); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public String format(ExtLogRecord record) { |
| 49 | + StringBuilder builder = new StringBuilder(); |
| 50 | + EcsJsonSerializer.serializeObjectStart(builder, record.getMillis()); |
| 51 | + EcsJsonSerializer.serializeLogLevel(builder, record.getLevel().getName()); |
| 52 | + EcsJsonSerializer.serializeFormattedMessage(builder, record.getFormattedMessage()); |
| 53 | + EcsJsonSerializer.serializeServiceName(builder, serviceName); |
| 54 | + EcsJsonSerializer.serializeEventDataset(builder, eventDataset); |
| 55 | + EcsJsonSerializer.serializeThreadName(builder, record.getThreadName()); |
| 56 | + EcsJsonSerializer.serializeLoggerName(builder, record.getLoggerName()); |
| 57 | + EcsJsonSerializer.serializeMDC(builder, record.getMdcCopy()); |
| 58 | + String ndc = record.getNdc(); |
| 59 | + if (ndc != null && !ndc.isEmpty()) { |
| 60 | + EcsJsonSerializer.serializeTagStart(builder); |
| 61 | + for (String tag : ndc.split("\\.")) { |
| 62 | + EcsJsonSerializer.serializeSingleTag(builder, tag); |
| 63 | + } |
| 64 | + EcsJsonSerializer.serializeTagEnd(builder); |
| 65 | + } |
| 66 | + if (includeOrigin && record.getSourceFileName() != null && record.getSourceMethodName() != null) { |
| 67 | + EcsJsonSerializer.serializeOrigin(builder, record.getSourceFileName(), record.getSourceMethodName(), record.getSourceLineNumber()); |
| 68 | + } |
| 69 | + Throwable throwable = record.getThrown(); |
| 70 | + if (throwable != null) { |
| 71 | + EcsJsonSerializer.serializeException(builder, throwable, stackTraceAsArray); |
| 72 | + } |
| 73 | + EcsJsonSerializer.serializeObjectEnd(builder); |
| 74 | + return builder.toString(); |
| 75 | + } |
| 76 | + |
| 77 | + public void setIncludeOrigin(final boolean includeOrigin) { |
| 78 | + this.includeOrigin = includeOrigin; |
| 79 | + } |
| 80 | + |
| 81 | + public void setServiceName(final String serviceName) { |
| 82 | + this.serviceName = serviceName; |
| 83 | + eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName); |
| 84 | + } |
| 85 | + |
| 86 | + public void setStackTraceAsArray(final boolean stackTraceAsArray) { |
| 87 | + this.stackTraceAsArray = stackTraceAsArray; |
| 88 | + } |
| 89 | + |
| 90 | + public void setEventDataset(String eventDataset) { |
| 91 | + this.eventDataset = eventDataset; |
| 92 | + } |
| 93 | + |
| 94 | + private String getProperty(final String name, final String defaultValue) { |
| 95 | + String value = LogManager.getLogManager().getProperty(name); |
| 96 | + if (value == null) { |
| 97 | + value = defaultValue; |
| 98 | + } else { |
| 99 | + value = value.trim(); |
| 100 | + } |
| 101 | + return value; |
| 102 | + } |
| 103 | +} |
0 commit comments