I have written a Log4j2 custom Converter plugin in OSGi environment and
have used log4j2 with ops4j-pax-logging. The custom field is tenantId and I
have referred to it in the log4j2.xml as %tenantId. When I run the program
the logs gets printed as {thread-name}enantId.

Plugin class

package com.test.logging.converters;

@Plugin(name = "TenantIdConverter", category = "Converter")
@ConverterKeys({"tenantId"})
public class TenantIdConverter extends LogEventPatternConverter {
    public TenantIdConverter(String name, String style) {
        super(name, style);
    }

    public static TenantIdConverter newInstance(String[] options) {
        return new TenantIdConverter("tenantId", "tenantId");
    }

    @Override
    public void format(LogEvent event, StringBuilder toAppendTo) {
        toAppendTo.append(getTenantID());
    }

    public String getTenantID() {
        String tenantId = "1234";
        if (tenantId == null) {
            tenantId = "[]";
        }
        return tenantId;
    }
}

pom.xml

    <dependency>
        <groupId>org.ops4j.pax.logging</groupId>
        <artifactId>pax-logging-api</artifactId>
        <version>1.10.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.ops4j.pax.logging</groupId>
        <artifactId>pax-logging-log4j2</artifactId>
        <version>1.10.1</version>
    </dependency>

------------------------------

        <plugin>
            <groupId>org.ops4j</groupId>
            <artifactId>maven-pax-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <executions>
                <execution>
                    <id>log4j-plugin-processor</id>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <phase>process-classes</phase>
                    <configuration>
                        <proc>only</proc>
                        <annotationProcessors>

<annotationProcessor>org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor</annotationProcessor>
                        </annotationProcessors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" name="log-test"
packages="com.test.logging.converters">
<Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="%-5p %d [%tenantId] %c: %m%n"/>
    </Console>

<RollingFile name="RollingFile" fileName="logs/log4j2app.log"

filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
        <PatternLayout>
            <Pattern>%-5p %d [%tenantId] %c: %m%n</Pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="250 MB"/>
        </Policies>
    </RollingFile>
</Appenders>
<Loggers>
    <Root level="INFO">
        <AppenderRef ref="STDOUT"/>
    </Root>
</Loggers>

Reply via email to