-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63185dc
commit 9640fb7
Showing
13 changed files
with
682 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.axway.alf</groupId> | ||
<artifactId>alf</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
|
||
<artifactId>alf-log4j2</artifactId> | ||
<name>${project.artifactId}</name> | ||
<description>ALF implementation over LOG4J2</description> | ||
|
||
<properties> | ||
<bnd.Fragment-Host>io.axway.alf.api</bnd.Fragment-Host> | ||
<bnd.Export-Package>io.axway.alf.log4j2.layout</bnd.Export-Package> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.axway.alf</groupId> | ||
<artifactId>alf-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
118 changes: 118 additions & 0 deletions
118
alf-log4j2/src/main/java/io/axway/alf/log4j2/Log4j2Logger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package io.axway.alf.log4j2; | ||
|
||
import java.util.function.*; | ||
import io.axway.alf.Arguments; | ||
import io.axway.alf.log.Logger; | ||
|
||
final class Log4j2Logger implements Logger { | ||
private final org.apache.logging.log4j.Logger m_logger; | ||
|
||
Log4j2Logger(org.apache.logging.log4j.Logger logger) { | ||
m_logger = logger; | ||
} | ||
|
||
@Override | ||
public void trace(String message) { | ||
m_logger.trace(message); | ||
} | ||
|
||
@Override | ||
public void trace(String message, Consumer<Arguments> arguments) { | ||
m_logger.trace(() -> new Log4j2Message(message, arguments)); | ||
} | ||
|
||
@Override | ||
public void trace(String message, Throwable throwable) { | ||
m_logger.trace(message, throwable); | ||
} | ||
|
||
@Override | ||
public void trace(String message, Consumer<Arguments> arguments, Throwable throwable) { | ||
m_logger.trace(() -> new Log4j2Message(message, arguments), throwable); | ||
} | ||
|
||
@Override | ||
public void debug(String message) { | ||
m_logger.debug(message); | ||
} | ||
|
||
@Override | ||
public void debug(String message, Consumer<Arguments> arguments) { | ||
m_logger.debug(() -> new Log4j2Message(message, arguments)); | ||
} | ||
|
||
@Override | ||
public void debug(String message, Throwable throwable) { | ||
m_logger.debug(message, throwable); | ||
} | ||
|
||
@Override | ||
public void debug(String message, Consumer<Arguments> arguments, Throwable throwable) { | ||
m_logger.debug(() -> new Log4j2Message(message, arguments), throwable); | ||
} | ||
|
||
@Override | ||
public void info(String message) { | ||
m_logger.info(message); | ||
} | ||
|
||
@Override | ||
public void info(String message, Consumer<Arguments> arguments) { | ||
m_logger.info(() -> new Log4j2Message(message, arguments)); | ||
} | ||
|
||
@Override | ||
public void info(String message, Throwable throwable) { | ||
m_logger.info(message, throwable); | ||
} | ||
|
||
@Override | ||
public void info(String message, Consumer<Arguments> arguments, Throwable throwable) { | ||
m_logger.info(() -> new Log4j2Message(message, arguments), throwable); | ||
} | ||
|
||
@Override | ||
public void warn(String message) { | ||
m_logger.warn(message); | ||
} | ||
|
||
@Override | ||
public void warn(String message, Consumer<Arguments> arguments) { | ||
m_logger.warn(() -> new Log4j2Message(message, arguments)); | ||
} | ||
|
||
@Override | ||
public void warn(String message, Throwable throwable) { | ||
m_logger.warn(message, throwable); | ||
} | ||
|
||
@Override | ||
public void warn(String message, Consumer<Arguments> arguments, Throwable throwable) { | ||
m_logger.warn(() -> new Log4j2Message(message, arguments), throwable); | ||
} | ||
|
||
@Override | ||
public void error(String message) { | ||
m_logger.error(message); | ||
} | ||
|
||
@Override | ||
public void error(String message, Consumer<Arguments> arguments) { | ||
m_logger.error(() -> new Log4j2Message(message, arguments)); | ||
} | ||
|
||
@Override | ||
public void error(String message, Throwable throwable) { | ||
m_logger.error(message, throwable); | ||
} | ||
|
||
@Override | ||
public void error(String message, Consumer<Arguments> arguments, Throwable throwable) { | ||
m_logger.error(() -> new Log4j2Message(message, arguments), throwable); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return m_logger.getName(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
alf-log4j2/src/main/java/io/axway/alf/log4j2/Log4j2LoggerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.axway.alf.log4j2; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import io.axway.alf.log.ILoggerFactory; | ||
import io.axway.alf.log.Logger; | ||
|
||
public class Log4j2LoggerFactory implements ILoggerFactory { | ||
@Override | ||
public Logger getLogger(String name) { | ||
return new Log4j2Logger(LogManager.getLogger(name)); | ||
} | ||
|
||
@Override | ||
public Logger getLogger(Class<?> clazz) { | ||
return new Log4j2Logger(LogManager.getLogger(clazz)); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
alf-log4j2/src/main/java/io/axway/alf/log4j2/Log4j2Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.axway.alf.log4j2; | ||
|
||
import java.util.function.*; | ||
import org.apache.logging.log4j.message.Message; | ||
import io.axway.alf.Arguments; | ||
|
||
import static io.axway.alf.formatter.JsonMessageFormatter.getFormatter; | ||
|
||
public final class Log4j2Message implements Message { | ||
private final String m_message; | ||
private final Consumer<Arguments> m_args; | ||
|
||
Log4j2Message(String message, Consumer<Arguments> args) { | ||
m_message = message; | ||
m_args = args; | ||
} | ||
|
||
public String getMessage() { | ||
return m_message; | ||
} | ||
|
||
public Consumer<Arguments> getArgs() { | ||
return m_args; | ||
} | ||
|
||
@Override | ||
public String getFormattedMessage() { | ||
return getFormatter().format(m_message, m_args); | ||
} | ||
|
||
@Override | ||
public String getFormat() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public Object[] getParameters() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Throwable getThrowable() { | ||
return null; | ||
} | ||
} |
162 changes: 162 additions & 0 deletions
162
alf-log4j2/src/main/java/io/axway/alf/log4j2/layout/AlfJsonLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package io.axway.alf.log4j2.layout; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.*; | ||
import javax.annotation.*; | ||
import org.apache.logging.log4j.core.Layout; | ||
import org.apache.logging.log4j.core.LogEvent; | ||
import org.apache.logging.log4j.core.config.Configuration; | ||
import org.apache.logging.log4j.core.config.Node; | ||
import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; | ||
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; | ||
import org.apache.logging.log4j.core.layout.AbstractStringLayout; | ||
import org.apache.logging.log4j.message.Message; | ||
import org.apache.logging.log4j.util.ReadOnlyStringMap; | ||
import io.axway.alf.formatter.JsonWriter; | ||
import io.axway.alf.log4j2.Log4j2Message; | ||
|
||
@SuppressWarnings({"WeakerAccess", "unused"}) | ||
@Plugin(name = "AlfJsonLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) | ||
public final class AlfJsonLayout extends AbstractStringLayout { | ||
public static class Builder<B extends Builder<B>> extends AbstractStringLayout.Builder<B> | ||
implements org.apache.logging.log4j.core.util.Builder<AlfJsonLayout> { | ||
@Nullable | ||
@PluginBuilderAttribute("dateFormat") | ||
private String m_dateFormat; | ||
|
||
@PluginBuilderAttribute("threadPrinting") | ||
private boolean m_threadPrinting = true; | ||
|
||
@PluginBuilderAttribute("levelPrinting") | ||
private boolean m_levelPrinting = true; | ||
|
||
@PluginBuilderAttribute("loggerPrinting") | ||
private boolean m_loggerPrinting = true; | ||
|
||
@PluginBuilderAttribute("contextPrinting") | ||
private boolean m_contextPrinting = true; | ||
|
||
@PluginBuilderAttribute("messageKey") | ||
private String m_messageKey = "message"; | ||
|
||
public Builder() { | ||
super(); | ||
setCharset(StandardCharsets.UTF_8); | ||
} | ||
|
||
@Override | ||
public AlfJsonLayout build() { | ||
DateFormat dateFormat = m_dateFormat == null ? null : new SimpleDateFormat(m_dateFormat); | ||
return new AlfJsonLayout(getConfiguration(), getCharset(), getHeaderSerializer(), getFooterSerializer(), dateFormat, m_threadPrinting, | ||
m_levelPrinting, m_loggerPrinting, m_contextPrinting, m_messageKey); | ||
} | ||
|
||
public Builder withDateFormat(String dateFormat) { | ||
m_dateFormat = dateFormat; | ||
return this; | ||
} | ||
|
||
public Builder withThreadPrinting(boolean threadPrinting) { | ||
m_threadPrinting = threadPrinting; | ||
return this; | ||
} | ||
|
||
public Builder withLevelPrinting(boolean levelPrinting) { | ||
m_levelPrinting = levelPrinting; | ||
return this; | ||
} | ||
|
||
public Builder withLoggerPrinting(boolean loggerPrinting) { | ||
m_loggerPrinting = loggerPrinting; | ||
return this; | ||
} | ||
|
||
public Builder withContextPrinting(boolean contextPrinting) { | ||
m_contextPrinting = contextPrinting; | ||
return this; | ||
} | ||
|
||
public Builder withMessageKey(String messageKey) { | ||
m_messageKey = messageKey; | ||
return this; | ||
} | ||
} | ||
|
||
@PluginBuilderFactory | ||
public static <B extends Builder<B>> B newBuilder() { | ||
return new Builder<B>().asBuilder(); | ||
} | ||
|
||
@Nullable | ||
private final DateFormat m_dateFormat; | ||
private final boolean m_threadPrinting; | ||
private final boolean m_levelPrinting; | ||
private final boolean m_loggerPrinting; | ||
private final boolean m_contextPrinting; | ||
private final String m_messageKey; | ||
|
||
public AlfJsonLayout(Configuration config, Charset aCharset, Serializer headerSerializer, Serializer footerSerializer, @Nullable DateFormat dateFormat, | ||
boolean threadPrinting, boolean levelPrinting, boolean loggerPrinting, boolean contextPrinting, String messageKey) { | ||
super(config, aCharset, headerSerializer, footerSerializer); | ||
m_dateFormat = dateFormat; | ||
m_threadPrinting = threadPrinting; | ||
m_levelPrinting = levelPrinting; | ||
m_loggerPrinting = loggerPrinting; | ||
m_contextPrinting = contextPrinting; | ||
m_messageKey = messageKey; | ||
} | ||
|
||
@Override | ||
public String toSerializable(LogEvent event) { | ||
StringBuilder sb = new StringBuilder(128); | ||
JsonWriter jsonWriter = new JsonWriter(sb); | ||
|
||
if (m_dateFormat != null) { | ||
jsonWriter.add("time", m_dateFormat.format(new Date(event.getTimeMillis()))); | ||
} else { | ||
jsonWriter.add("time", event.getTimeMillis()); | ||
} | ||
|
||
if (m_threadPrinting) { | ||
jsonWriter.add("thread", event.getThreadName()); | ||
} | ||
|
||
if (m_levelPrinting) { | ||
jsonWriter.add("level", event.getLevel()); | ||
} | ||
|
||
if (m_loggerPrinting) { | ||
jsonWriter.add("logger", event.getLoggerName()); | ||
} | ||
|
||
if (m_contextPrinting) { | ||
ReadOnlyStringMap context = event.getContextData(); | ||
if (!context.isEmpty()) { | ||
jsonWriter.add("context", args -> context.forEach(args::add)); | ||
} | ||
} | ||
|
||
Message message = event.getMessage(); | ||
if (message instanceof Log4j2Message) { | ||
Log4j2Message log4j2Message = (Log4j2Message) message; | ||
jsonWriter.add(m_messageKey, log4j2Message.getMessage()); | ||
jsonWriter.add("args", log4j2Message.getArgs()); | ||
} else { | ||
jsonWriter.add(m_messageKey, message.getFormattedMessage()); | ||
} | ||
|
||
Throwable exception = event.getThrown() != null ? event.getThrown() : message.getThrowable(); | ||
if (exception != null) { | ||
jsonWriter.add("exception", exception); | ||
} | ||
|
||
jsonWriter.end(); | ||
|
||
sb.append(System.lineSeparator()); | ||
return sb.toString(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
alf-log4j2/src/main/java/io/axway/alf/log4j2/layout/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@ParametersAreNonnullByDefault | ||
package io.axway.alf.log4j2.layout; | ||
|
||
import javax.annotation.*; |
4 changes: 4 additions & 0 deletions
4
alf-log4j2/src/main/java/io/axway/alf/log4j2/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@ParametersAreNonnullByDefault | ||
package io.axway.alf.log4j2; | ||
|
||
import javax.annotation.*; |
1 change: 1 addition & 0 deletions
1
alf-log4j2/src/main/resources/META-INF/services/io.axway.alf.log.ILoggerFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.axway.alf.log4j2.Log4j2LoggerFactory |
Oops, something went wrong.