Create log appender programmatically (Karaf 4.2.5)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Create log appender programmatically (Karaf 4.2.5)

jochenw
Hi,

in my application, I want to programmatically add a log appender. The
org.ops4j.pax.logging.cfg file should be left untouched (meaning that I
don't want to use ConfigAdmin to change the configuration).

When trying with the log4j ConfigurationBuilder to configure the appender
and the logger, and then the Configurator to activate it, following error
appears:

/LogManager returned an instance of
org.ops4j.pax.logging.log4jv2.Log4jv2LoggerContextFactory which does not
implement org.apache.logging.log4j.core.impl.Log4jContextFactory. Unable to
initialize Log4j/.

My pom contains the pax-logging-api and pax-logging-log4j2 bundles as
dependencyies, both with version 1.10.1.

Is there a way to get this right, or another way to create a log appender
via code? Please note that I don't want to create a custom appender, just
configure a rolling file appender, but from code instead of from config
file.

Best Regards

Jochen



--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html
Reply | Threaded
Open this post in threaded view
|

Re: Create log appender programmatically (Karaf 4.2.5)

jbonofre
Hi Jochen,

If you want to be "low level", you have to create a fragment on
pax-logging-log4j2 to add your appender.
It's similar to what I explain in this blog:
http://blog.nanthrax.net/?p=333 (just use pax-logging-log4j2 as host). I
just did this in my company yesterday (for a layout pattern and an
appender).

You have another possible approach is to create a Pax Logging
PaxAppender. Then you don't need a fragment, just registering a
PaxAppender service.
It's what we are using in Decanter for the log collector:

https://github.com/apache/karaf-decanter/blob/master/collector/log/src/main/java/org/apache/karaf/decanter/collector/log/LogAppender.java

You should not use the ConfigurationBuilder directly anyway, better to
create your own appender and delegate the configuration to pax-logging.

Regards
JB

On 07/06/2019 09:10, jochenw wrote:

> Hi,
>
> in my application, I want to programmatically add a log appender. The
> org.ops4j.pax.logging.cfg file should be left untouched (meaning that I
> don't want to use ConfigAdmin to change the configuration).
>
> When trying with the log4j ConfigurationBuilder to configure the appender
> and the logger, and then the Configurator to activate it, following error
> appears:
>
> /LogManager returned an instance of
> org.ops4j.pax.logging.log4jv2.Log4jv2LoggerContextFactory which does not
> implement org.apache.logging.log4j.core.impl.Log4jContextFactory. Unable to
> initialize Log4j/.
>
> My pom contains the pax-logging-api and pax-logging-log4j2 bundles as
> dependencyies, both with version 1.10.1.
>
> Is there a way to get this right, or another way to create a log appender
> via code? Please note that I don't want to create a custom appender, just
> configure a rolling file appender, but from code instead of from config
> file.
>
> Best Regards
>
> Jochen
>
>
>
> --
> Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html
>

--
Jean-Baptiste Onofré
[hidden email]
http://blog.nanthrax.net
Talend - http://www.talend.com
Reply | Threaded
Open this post in threaded view
|

Re: Create log appender programmatically (Karaf 4.2.5)

jochenw
Thanks for the quick reply, JB!

I've already seen your blog entry, and also know about the PaxAppender.
However, as far as I understood, for both I still need a configuration in
org.ops4j.pax.logging.cfg - e.g. the type RollingRandomAccessFile, the size
per file and the number of files in the policies, the file name, pattern
etc., or the name of the logger using that appender.

For the PaxAppender solution, it is of course possible to have a self-made
writer, using some RollingFileWriter library.

I'd be perfectly happy with delegating the configuration to pax-logging, but
currently I cannot see how to do that from code, without changing the
content of the org.ops4j.pax.logging.cfg or the respective PID. Maybe there
just is no simple solution like a "ConfigurationBuilder" replacement in
pax-logging.

Regards
Jochen




--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html
Reply | Threaded
Open this post in threaded view
|

Re: Create log appender programmatically (Karaf 4.2.5)

jbonofre
Hi,

You can always "programmatically" update the org.ops4j.pax.logging
configuration via ConfigAdmin service.

Just add the corresponding configuration as you would do "by hand".

It's what log:set is doing for instance. It's also what ssh is doing
changing level of a logger.

Regards
JB

On 07/06/2019 15:12, jochenw wrote:

> Thanks for the quick reply, JB!
>
> I've already seen your blog entry, and also know about the PaxAppender.
> However, as far as I understood, for both I still need a configuration in
> org.ops4j.pax.logging.cfg - e.g. the type RollingRandomAccessFile, the size
> per file and the number of files in the policies, the file name, pattern
> etc., or the name of the logger using that appender.
>
> For the PaxAppender solution, it is of course possible to have a self-made
> writer, using some RollingFileWriter library.
>
> I'd be perfectly happy with delegating the configuration to pax-logging, but
> currently I cannot see how to do that from code, without changing the
> content of the org.ops4j.pax.logging.cfg or the respective PID. Maybe there
> just is no simple solution like a "ConfigurationBuilder" replacement in
> pax-logging.
>
> Regards
> Jochen
>
>
>
>
> --
> Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html
>

--
Jean-Baptiste Onofré
[hidden email]
http://blog.nanthrax.net
Talend - http://www.talend.com
Reply | Threaded
Open this post in threaded view
|

Re: Create log appender programmatically (Karaf 4.2.5)

jochenw
Hi,

yes, via ConfigAdmin it is of course possible to adapt the configuration.
However, that will also write the changes into org.ops4j.pax.logging.cfg -
with some side effects. E.g. the variables in that file like ${karaf.log}
are replaced by their values, and sometimes pathnames are scrambled
(happened sometimes with Windows path names, where / and \\ were mixed).

Thanks for now! I'll have to think about it a bit.

Regards
Jochen



--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html