1

Is it possible to configure log4j to call a static function in pattern layout? also how can we configure log4j2 to print logs in json format? I have tried this

<JSONLayout complete="true" compact="false"/>

My complete log4j2.xml file is this :-

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="log-path">logs</Property>
    </Properties>
    <Appenders>
        <Console name="console-log" target="SYSTEM_OUT">
            <JSONLayout complete="true" compact="false"/>
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
        </Console>
        <RollingFile name="trace-log" fileName="${log-path}/ltlogs-trace.log"
                     filePattern="${log-path}/ltlogs-%d{yyyy-MM-dd}.log">
            <JSONLayout complete="true" compact="false"/>
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
        <RollingFile name="error-log" fileName="${log-path}/lt-logs.log"
                     filePattern="${log-path}/lt-logs-%d{yyyy-MM-dd}.log">
            <JSONLayout complete="true" compact="false"/>
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="com.limetray.helper" level="debug" additivity="false">
            <appender-ref ref="trace-log" level="debug"/>
            <appender-ref ref="error-log" level="error"/>
            <appender-ref ref="console-log" level="debug"/>
        </Logger>
        <Root level="info" additivity="false">
            <AppenderRef ref="console-log"/>
        </Root>
    </Loggers>
</Configuration>
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Rakesh Yadav
  • 369
  • 3
  • 5
  • 19
  • remove the , seems it is overriding your – kuhajeyan Sep 06 '16 at 12:32
  • log4j also provides appending functionality through AppenderSkeleton, check out this post http://stackoverflow.com/questions/6072389/how-to-create-a-own-appender-in-log4j – harsh Sep 06 '16 at 13:22

2 Answers2

2

You can create a Log4j 2 PatternLayout converter plugin. A converter is a small class that is passed a LogEvent and puts some text in a StringBuilder. For example, the converter for %m takes the message from the LogEvent and appends it to the StringBuilder.

A custom converter could have any pattern, let's say you use %STATIC, and in the implementation you call the static method.

Then if you configure <PatternLayout pattern ="%d %level %m%ex%n%STATIC" /> your converter will be called for each event.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
0

depends on the purpose why you want to call a function, if it is to add some extra information then you can either create a custom layout yourself or use a third party layout library in your project, ex.

https://github.com/logstash/log4j-jsonevent-layout

It will print the log in json format and also provides ability to add custom fields to json.

It is generally used along with logstash and and kibana but can also be used separately.

harsh
  • 117
  • 1
  • 10