Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

java - Log4j2 custom layout

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...