Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 custom layout

Tags:

java

log4j2

I am using Log4j2 with a PatternLayout. Is it possible to write my own Layout that extends AbstractStringLayout, so that I can customize my output message?

I have read the docs but I only see variations of layouts - no custom one.

https://logging.apache.org/log4j/2.x/manual/layouts.html

like image 993
TheCoder Avatar asked May 16 '17 15:05

TheCoder


People also ask

What is pattern layout in log4j2?

Layout class and overrides the format() method to structure the logging information according to a supplied pattern. PatternLayout is also a simple Layout object that provides the following-Bean Property which can be set using the configuration file: Sr.No.

Does Log4j properties work with log4j2?

Log4j 2 doesn't support the Log4j v1 ". properties" format anymore (yet, since v2. 4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".

Which is better Log4j or log4j2?

Log4j, Logback, and Log4j2 are good logging frameworks that are broadly used. So which one should you use? I recommend using Log4j2 because it's the fastest and most advanced of the three frameworks. Logback is still a good option, if performance is not your highest priority.


2 Answers

Yes it is possible

You can use custom AbstractStringLayout or LogEventPatternConverter

EX : AbstractStringLayout

@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;
    }
}

EX : LogEventPatternConverter

@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 )
    {

    }
}

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

packages="logging.log4j.custom.plugins"

<?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>    
like image 140
Tharinda Wicky Avatar answered Oct 27 '22 15:10

Tharinda Wicky


package com.mytest;
@Plugin(name = "MyHtmlLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)

public class MyHtmlLayout  extends AbstractStringLayout{
    protected MyHtmlLayout( Charset charset ){
        super( charset );
    }

    @Override
    public String toSerializable( LogEvent event ) {
        StringBuilder throwable = new StringBuilder();
        if (event.getThrown() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            pw.println();
            event.getThrown().printStackTrace(pw);
            pw.close();
            throwable.append(sw.toString());
        }
        StringBuilder retValue=new StringBuilder();
        retValue.append("<tr><td>").append(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:S").format(event.getTimeMillis())).append(" ")
        .append(event.getLevel().toString()).append(" ").append(event.getLoggerName()).append(" ").append(event.getMessage().getFormattedMessage()).append(" ").append(throwable).append("</tr></td>\n");

        return retValue.toString();
    }

    @PluginFactory
    public static MyHtmlLayout createLayout(@PluginAttribute(value = "charset", defaultString = "UTF-8") Charset charset) {
        return new MyHtmlLayout(charset);
    }
    @Override
    public byte[] getHeader() {
        return ("<html>\n  <body>\n" + "<Table>\n").getBytes();
    }
    @Override
    public byte[] getFooter() {
        return ("</table>\n</body>\n</html>").getBytes();
    }
}

Configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" packages="com.mytest" name="NewCustLayout">
    <Appenders>
        <Console name="LogToConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        
        <RollingFile name="LogToRollingFile" fileName="logs/app.html"
                     filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <!-- <PatternLayout>
                <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
            </PatternLayout>-->
            <MyHtmlLayout/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="100"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <!-- avoid duplicated logs with additivity=false -->
        <Logger name="com.psx.logtest" level="debug" additivity="false">
            <AppenderRef ref="LogToRollingFile"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="LogToConsole"/>
        </Root>
    </Loggers>
</Configuration>
like image 41
Raja Janapati Avatar answered Oct 27 '22 17:10

Raja Janapati