CategoriesTips and Tricks

Log4j2 custom layouts

Log4j22 provides multiple ways of creating a custom layout for many requirements. This article will explain simple ways to implement custom layouts using log4j2 plugins

In Log4j 2 a plugin is declared by adding a @Plugin annotation to the class declaration. During initialization, the Configuration will invoke the PluginManager to load the built-in Log4j plugins as well as any custom plugins.

Apache logging services

We can use both AbstractStringLayout or LogEventPatternConverter according to our requirements. Both options will provide a way to access log4j LogEvent that has all the relevant details about the log event to be customized.

Abstract String Layout

Custom plugin with name “NewCustLayout” can be created as show in below,

@Plugin(name = "NewCustLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
public class NewCustLayout extends AbstractStringLayout
{
protected NewCustLayout( Charset charset )
{
super( charset );
}
@Override public String toSerializable( LogEvent event )
{
return null;
}
}

LogEvent Pattern Converter

Custom plugin with name “NewLayoutConverter” and “custLayConv” as the converter key can be created as below

@Plugin(name = "NewLayoutConverter", category = "Converter")
@ConverterKeys({"custLayConv"})
public class NewLayoutConverter extends LogEventPatternConverter
{
/**
* Constructs an instance of LoggingEventPatternConverter.
*
* @param name name of converter.
* @param style CSS style for output.
*/
protected NewLayoutConverter( String name, String style )
{
super( name, style );
}
@Override public void format( LogEvent event, StringBuilder toAppendTo )
{
}
}

Log configurations

NewCustLayout” can be used instead of pattern layout in any appender and “%custLayConv” keyword can be used in any layout as shown in below,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration packages="logging.log4j.custom.plugins" name="SOME NAME">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{MM-dd-yyyy HH:mm:ss,SSS} [%t] %custLayConv %msg%n"/>
</Console>
<RollingRandomAccessFile name="example" fileName="${sys:tbx.log.path}example.log" filePattern="${sys:tbx.log.path}example.log.%i" append="true" immediateFlush="true" bufferSize="262144">
<NewCustLayout pattern="%d{MM-dd-yyyy HH:mm:ss,SSS} - %msg%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="100MB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="max" min="1" max="100" compressionLevel="3"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<logger name="logger" level="INFO" additivity="false">
<AppenderRef ref="example" level="INFO"/>
</logger>
<Root level="INFO">
<AppenderRef ref="CONSOLE" level="INFO"/>
</Root>
</Loggers>
</Configuration>
view raw log4j2.xml hosted with ❤ by GitHub

Make sure that your configuration file contain the java package that all the custom plugins are located ,

packages="logging.log4j.custom.plugins"

This article is base on one of my answers in stack overflow.

Please find the link here

One comment on “Log4j2 custom layouts”

Leave a Reply

Your email address will not be published. Required fields are marked *