Skip to content

Commit

Permalink
Upgrade to log4j 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik242 committed Nov 27, 2015
1 parent e3fa86a commit 998bbd4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description = 'Log4J2 Layout for outputting LogStash json_event format'

apply plugin: 'java'

version='4.0.0'
version='4.0.1'
group = 'net.logstash.log4j2'

buildscript {
Expand Down Expand Up @@ -62,10 +62,12 @@ publishing {

dependencies {

def log4j2Version = '2.+';
def log4j2Version = '2.4+';
compile "org.apache.logging.log4j:log4j-api:$log4j2Version"
compile "org.apache.logging.log4j:log4j-core:$log4j2Version"

compile "org.apache.commons:commons-csv:1.2"

// Testing stuff, hamcrest comes first please
testCompile 'org.hamcrest:hamcrest-all:1.+'
testCompile 'org.testng:testng:6.+'
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/apache/logging/log4j/core/LogStashLogEvent.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.apache.logging.log4j.core;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.fasterxml.jackson.databind.util.StdConverter;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.impl.ThrowableProxy;
import org.apache.logging.log4j.message.Message;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
* To serialize with mixin AND add two properties (@version and @timestamp) we
* need to convert from LogEvent to a POJO with these new accessor methods.
Expand Down Expand Up @@ -120,6 +120,11 @@ public void setIncludeLocation(boolean locationRequired) {
wrappedLogEvent.setIncludeLocation(locationRequired);
}

@Override
public long getNanoTime() {
return wrappedLogEvent.getNanoTime();
}

/**
* Converter used by JsonSerilize annotation on mixin.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
package org.apache.logging.log4j.core.layout;


import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.util.Constants;

import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.util.KeyValuePair;
import org.apache.logging.log4j.util.Strings;

import java.nio.charset.Charset;
import java.util.*;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Copy Pasta version of JsonLayout that uses a different JSON writer which adds
Expand Down Expand Up @@ -161,7 +162,7 @@ public static AbstractJacksonLayout createLayout(
* @return A JSON Layout.
*/
public static AbstractJacksonLayout createDefaultLayout() {
return new LogStashJSONLayout(false, false, false, false, false, Constants.UTF_8, new HashMap<String, String>());
return new LogStashJSONLayout(false, false, false, false, false, UTF_8, new HashMap<>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package org.apache.logging.log4j.core.layout;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.core.util.KeyValuePair;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.util.KeyValuePair;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.SimpleMessage;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;

Expand Down Expand Up @@ -63,7 +60,7 @@ public void afterTest() {
"\"contextMap\":[{\"key\":\"Foo\",\"value\":\"Bar\"},{\"key\":\"A\",\"value\":\"B\"}]}";

@Test
public void BasicSimpleTest() throws JsonParseException, JsonMappingException, IOException {
public void BasicSimpleTest() throws Exception {
Message simpleMessage = new SimpleMessage("Test Message");

Map<String,String> mdc = new HashMap<String,String>();
Expand Down Expand Up @@ -95,7 +92,7 @@ public void BasicSimpleTest() throws JsonParseException, JsonMappingException, I
);

String actualJSON = layout.toSerializable(event);
System.out.println(actualJSON);
System.out.println(actualJSON);
assertThat(actualJSON, sameJSONAs(expectedBasicSimpleTestJSON)
.allowingExtraUnexpectedFields()
.allowingAnyArrayOrdering());
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
<PatternLayout pattern="pattern::{}:: %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>

<!-- Example of what you might do to add fields, warning values should be known to be json escaped strings -->
<KeyValuePair key="application_name" value="super application"/>
<KeyValuePair key="arbitrary_name_replace_me" value="replaceable_value"/>
<!--<KeyValuePair key="application_name" value="super application"/>-->
<!--<KeyValuePair key="arbitrary_name_replace_me" value="replaceable_value"/>-->

<!-- Example of using system property substitution -->
<KeyValuePair key="application_user" value="${sys:user.name}"/>
<!--<KeyValuePair key="application_user" value="${sys:user.name}"/>-->
<!--<KeyValuePair key="hostame" value="${sys:user.name}"/> -->
<!--<KeyValuePair key="local_ip" value="${sys:local_ip}"/> -->

<!-- Example of using environment property substitution env:USERNAME on windows -->
<KeyValuePair key="environment_user" value="${env:USER}"/>
<!--<KeyValuePair key="environment_user" value="${env:USER}"/>-->
</LogStashJSONLayout>
</Console>
<!--<AuditSyslogAppender name="AuditSysLog" format="RFC5424" host="localhost" port="514"
Expand Down

0 comments on commit 998bbd4

Please sign in to comment.