Skip to content

Commit

Permalink
JacksonJsonLayout added, Log4j 2 and FasterXML Jackson dependencies u…
Browse files Browse the repository at this point in the history
…pgraded

* layout implements ItemSourceLayout
* layout allows to output customisations via FasterXML Jackson mixins
* LogEventJacksonJsonMixIn (modified log4j-core:LogEventJsonMixIn) was added to prevent #9 (visit javadoc for full list of changes)
* this layout is now used by default
  • Loading branch information
rfoltyns committed Dec 2, 2018
1 parent 00d41bb commit 04c4a4b
Show file tree
Hide file tree
Showing 18 changed files with 1,049 additions and 17 deletions.
11 changes: 6 additions & 5 deletions log4j2-elasticsearch-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>provided</scope>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<scope>provided</scope>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-afterburner</artifactId>
<scope>provided</scope>
</dependency>

Expand Down Expand Up @@ -75,6 +75,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.apache.logging.log4j.core.jackson;

/*-
* #%L
* log4j2-elasticsearch
* %%
* Copyright (C) 2018 Rafal Foltynski
* %%
* 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.
* #L%
*/

import com.fasterxml.jackson.databind.module.SimpleModule;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.ExtendedStackTraceElement;
import org.apache.logging.log4j.core.impl.ThrowableProxy;

public class ExtendedLog4j2JsonModule extends SimpleModule {

@Override
public void setupModule(SetupContext context) {
super.setupModule(context);

context.setMixInAnnotations(StackTraceElement.class, StackTraceElementMixIn.class);
context.setMixInAnnotations(Marker.class, MarkerMixIn.class);
context.setMixInAnnotations(Level.class, LevelMixIn.class);
context.setMixInAnnotations(ExtendedStackTraceElement.class, ExtendedStackTraceElementMixIn.class);
context.setMixInAnnotations(ThrowableProxy.class, ThrowableProxyMixIn.class);

// https://github.com/rfoltyns/log4j2-elasticsearch/issues/9, timeMillis can't be ignored
context.setMixInAnnotations(LogEvent.class, LogEventJacksonJsonMixIn.class);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.apache.logging.log4j.core.jackson;

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.
*
* MODIFICATIONS:
* rfoltyns:
* - timeInMillis not ignored anymore (patch for log4j-core:2.11+)
* - nanoTime, parameterCount, formattedMessage loggerFqcn, source, threadId, threadPriority, endOfBatch, instant ignored
* - XML-related annotations removed
* - setters removed
* - JsonDeserialize annotations removed
* - JsonFilter removed
*
*/

import java.util.Map;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.ThreadContext.ContextStack;
import org.apache.logging.log4j.core.time.Instant;
import org.apache.logging.log4j.util.ReadOnlyStringMap;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.ThrowableProxy;
import org.apache.logging.log4j.message.Message;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonPropertyOrder({ "timeMillis", "loggerName", "level", "marker", "message", "thrown", "threadName"})
public abstract class LogEventJacksonJsonMixIn implements LogEvent {

private static final long serialVersionUID = 1L;

@JsonIgnore
@Override
public abstract Map<String, String> getContextMap();

@JsonIgnore
@Override
public abstract ReadOnlyStringMap getContextData();

@JsonIgnore
@Override
public abstract ContextStack getContextStack();

@JsonProperty
@Override
public abstract Level getLevel();

@JsonIgnore
@Override
public abstract String getLoggerFqcn();

@JsonProperty
@Override
public abstract String getLoggerName();

@JsonProperty(JsonConstants.ELT_MARKER)
@Override
public abstract Marker getMarker();

@JsonProperty(JsonConstants.ELT_MESSAGE)
@JsonSerialize(using = MessageSerializer.class)
@Override
public abstract Message getMessage();

@JsonIgnore
@Override
public abstract StackTraceElement getSource();

@JsonIgnore
@Override
public abstract long getThreadId();

@JsonProperty("thread")
@Override
public abstract String getThreadName();

@JsonIgnore
@Override
public abstract int getThreadPriority();

@JsonIgnore
@Override
public abstract Throwable getThrown();

@JsonProperty(JsonConstants.ELT_THROWN)
@Override
public abstract ThrowableProxy getThrownProxy();

@JsonProperty
@Override
public abstract long getTimeMillis();

@JsonIgnore
@Override
public abstract boolean isEndOfBatch();

@JsonIgnore
@Override
public abstract boolean isIncludeLocation();

@JsonIgnore
@Override
public abstract long getNanoTime();

@JsonIgnore
@Override
public abstract Instant getInstant();

}

Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public ElasticsearchAppender build() {
}

if (layout == null) {
layout = JsonLayout.newBuilder().setCompact(true).build();
layout = JacksonJsonLayout.newBuilder().build();
}

return new ElasticsearchAppender(name, filter, layout, ignoreExceptions, batchDelivery, messageOnly, indexNameFormatter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

public interface ItemSourceFactory {

String ELEMENT_TYPE = "itemSourceFactory";

/**
* Indicates whether {@link ItemSource} lifecycle has to be taken care of
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.appenders.log4j2.elasticsearch;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;

/**
* Wraps {@link AfterburnerModule} configuration to avoid {@link ClassNotFoundException}
* when {@code {@link org.appenders.log4j2.elasticsearch.JacksonJsonLayout.Builder#withAfterburner(boolean)}} is set to false
* and com.fasterxml.jackson.module:jackson-module-afterburner dependency was not provided
*/
final class JacksonAfterburnerModuleConfigurer {

void configure(ObjectMapper objectMapper) {
objectMapper.registerModule(new AfterburnerModule());
}

}
Loading

0 comments on commit 04c4a4b

Please sign in to comment.