Munish Gupta
Munish Gupta

Reputation: 41

how to replace pax-logging-service with pax-logging-log4j2 with latest KARAF

we upgraded OSGI KARAF to 4.1.x version and with that default pax-logging-api implementation has been changed to Log4J2 from log4j1.

earlier with log4j1, we used to have a custom appender which extends fileappender and takes the configuration from CFG file. we do compile this appender as fragment and provide the fragment-host bundle as pax-logging-service for getting loaded.

Now with log4J2, creating custom appender very different it seems. I went through this question How to Create a Custom Appender in log4j2?

which is very helpful but I am not completely unclear on how it will get loaded now?

how will it take the configuration parameters like it was earlier taking from CFG file?

Can we still use as fragment and mention fragment-host bundle here also to get it working?

Regards Munish

Upvotes: 2

Views: 1006

Answers (1)

icook
icook

Reputation: 71

I don't known if Munish Gupta has solved the problem. Just write the solution for the one who searched for the question. It takes me so long to solved it.

  • Firstly , write our custom appender in our own bundle. Appender in the log4j2 is a plugin, so we must add annotation @Plugin and @PluginFactory. Just look this question:

    How to Create a Custom Appender in log4j2?

  • Secondly, generate Plugin.dat file. According the log4j2 manual, the Plugin.dat would be generated automatically.

    But when I write my own appender, it does not generate Plugin.dat. Then we need add this configuration in our pom.xml

    <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>

    Official manual: log4j2 plugin

  • Thirdly, we need modify maven-bundle-plugin(BND), the generated Plugin.dat would not appear in the final jar.(The BND FAQ has said it.). We need use tag. Here is the config.

    <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>3.3.0</version> <extensions>true</extensions> <configuration> <instructions> <Include-Resource>META-INF=${project.build.directory}/classes/META-INF</Include-Resource> </instructions> </configuration> </plugin>

    BND FAQ

  • Fourthly , register our custom appender bundle as a fragment, and the fragment host is pax-logging-log4j2.

    <configuration> <instructions> <Bundle-Name>${project.artifactId}</Bundle-Name> <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName> <Bundle-Version>${project.version}</Bundle-Version> <Export-Package>com.good.gcs.core.logging</Export-Package> <Import-Package>!*</Import-Package> <Fragment-Host>org.ops4j.pax.logging.pax-logging-log4j2</Fragment-Host> <_failok>true</_failok> </instructions> </configuration>

    Ref:Log4j Register the bundle as fragment

Upvotes: 1

Related Questions