1
我正在使用Log4j2和PatterLayout。是否有可能編寫自己的擴展AbstractStringLayout的佈局,以便我可以自定義輸出消息?Log4j2自定義佈局
我已閱讀文檔,但我只看到佈局的變化 - 沒有自定義的。
https://logging.apache.org/log4j/2.x/manual/layouts.html
我正在使用Log4j2和PatterLayout。是否有可能編寫自己的擴展AbstractStringLayout的佈局,以便我可以自定義輸出消息?Log4j2自定義佈局
我已閱讀文檔,但我只看到佈局的變化 - 沒有自定義的。
https://logging.apache.org/log4j/2.x/manual/layouts.html
是有可能
可以使用自定義AbstractStringLayout
或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)
{
}
}
確保您的配置文件中包含了所有的自定義插件所在的包,
包= 「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>
檢查HTTP:/ /andrew-flower.com/blog/Create_Custom_Log4j_Plugins –