Skip to content

Parameterized logging jul #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ void testSimpleLog() throws Exception {
assertThat(getLastLogLine().get("message").textValue()).isEqualTo("test");
}

@Test
void testSimpleParameterizedLog() throws Exception {
ParameterizedLogSupport parameterizedLogSupport = getParameterizedLogSettings();

// don't test parameterized logging if the log framework implementation does not support it.
if (parameterizedLogSupport == ParameterizedLogSupport.NOT_SUPPORTED) {
return;
}

if (parameterizedLogSupport == ParameterizedLogSupport.NUMBER_AND_BRACKETS) {
debug("{0} is not {1}", 1, 2);
} else if (parameterizedLogSupport == ParameterizedLogSupport.BRACKETS_ONLY) {
debug("{} is not {}", 1, 2);
}

assertThat(getLastLogLine().get("message").textValue()).isEqualTo("1 is not 2");
}

@Test
void testThreadContext() throws Exception {
if (putMdc("foo", "bar")) {
Expand Down Expand Up @@ -124,8 +142,14 @@ public boolean putNdc(String message) {
return false;
}

public ParameterizedLogSupport getParameterizedLogSettings() {
return ParameterizedLogSupport.BRACKETS_ONLY;
}

public abstract void debug(String message);

public abstract void debug(String message, Object... logParams);

public abstract void error(String message, Throwable t);

public abstract JsonNode getLastLogLine() throws IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*-
* #%L
* Java ECS logging
* %%
* Copyright (C) 2019 - 2020 Elastic and contributors
* %%
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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.
* #L%
*/
package co.elastic.logging;

/**
* Logging frameworks differ in their implementation of log formatting.
* For example JUL and Jboss only support formatting when the message is structured as follows: "Log: {0}"
* whereas more recent log frameworks only support it with the following message: "Log: {}"
* Log4j does not support parameterized logging.
*/
public enum ParameterizedLogSupport {
BRACKETS_ONLY, // {}, {}
NUMBER_AND_BRACKETS, // {0}, {1}
NOT_SUPPORTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void printStackTrace(PrintWriter pw) {
}
});

assertThat(formatter.format(record)).isEqualTo("{" +
assertThat(formatter.format(record).replace("\\r\\n", "\\n")).isEqualTo("{" +
"\"@timestamp\":\"1970-01-01T00:00:00.005Z\", " +
"\"log.level\": \"INFO\", " +
"\"message\":\"Example Message\", " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package co.elastic.logging.jboss.logmanager;

import co.elastic.logging.AbstractEcsLoggingTest;
import co.elastic.logging.ParameterizedLogSupport;
import com.fasterxml.jackson.databind.JsonNode;
import org.jboss.logmanager.Level;
import org.jboss.logmanager.Logger;
Expand Down Expand Up @@ -64,6 +65,16 @@ public void debug(String message) {
logger.log(Level.DEBUG, message);
}

@Override
public ParameterizedLogSupport getParameterizedLogSettings() {
return ParameterizedLogSupport.NUMBER_AND_BRACKETS;
}

@Override
public void debug(String message, Object... logParams) {
logger.log(Level.DEBUG, message, logParams);
}

@Override
public void error(String message, Throwable t) {
logger.log(Level.ERROR, message, t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String format(final LogRecord record) {
final StringBuilder builder = new StringBuilder();
EcsJsonSerializer.serializeObjectStart(builder, record.getMillis());
EcsJsonSerializer.serializeLogLevel(builder, record.getLevel().getName());
EcsJsonSerializer.serializeFormattedMessage(builder, record.getMessage());
EcsJsonSerializer.serializeFormattedMessage(builder, super.formatMessage(record));
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadId(builder, record.getThreadID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import co.elastic.logging.ParameterizedLogSupport;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -85,6 +86,16 @@ public void debug(String message) {
logger.log(Level.FINE, message);
}

@Override
public ParameterizedLogSupport getParameterizedLogSettings() {
return ParameterizedLogSupport.NUMBER_AND_BRACKETS;
}

@Override
public void debug(String message, Object... logParams) {
logger.log(Level.FINE, message, logParams);
}

@Override
public void error(String message, Throwable t) {
logger.log(Level.SEVERE, message, t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
package co.elastic.logging.log4j;

import co.elastic.logging.AbstractEcsLoggingTest;
import co.elastic.logging.ParameterizedLogSupport;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.apache.log4j.NDC;
import org.apache.log4j.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -82,6 +80,16 @@ public void debug(String message) {
logger.debug(message);
}

@Override
public ParameterizedLogSupport getParameterizedLogSettings() {
return ParameterizedLogSupport.NOT_SUPPORTED;
}

@Override
public void debug(String message, Object... logParams) {
throw new UnsupportedOperationException();
}

@Override
public void error(String message, Throwable t) {
logger.error(message, t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public void debug(String message) {
root.debug(message);
}

@Override
public void debug(String message, Object... logParams) {
root.debug(message, logParams);
}

@Override
public void error(String message, Throwable t) {
root.error(message, t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public void debug(String message) {
logger.debug(message);
}

@Override
public void debug(String message, Object... logParams) {
logger.debug(message, logParams);
}

@Test
void testAdditionalFields() throws Exception {
debug("test");
Expand Down