slf4j+logback problem

  • Thread starter dr. Hans van der Meer
  • Start date
D

dr. Hans van der Meer

When I configured logging as in the logback manual in logback.xml:

<appender name="STDOUT"
class="mypackage.MyAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%-4relative [%thread] %-5level %class - %msg%n</pattern>
</layout>
</appender>

then an object of class mypackage.MyAppender is constructed and can be used for
receiving LoggingEvents.
However, I cannot format the logevents directly with this Appender through:
appender.getLayout().doLayout(logevent)
because the getLayout returns null?
How is this possible? I would like to format the logevents in accordance with
the pattern as given in logback.xml.
Using logback (latest) version 0.9.5.

Hans van der Meer
 
D

dr. Hans van der Meer

"dr. Hans van der Meer said:
When I configured logging as in the logback manual in logback.xml:

<appender name="STDOUT"
class="mypackage.MyAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%-4relative [%thread] %-5level %class - %msg%n</pattern>
</layout>
</appender>

then an object of class mypackage.MyAppender is constructed and can be used
for
receiving LoggingEvents.
However, I cannot format the logevents directly with this Appender through:
appender.getLayout().doLayout(logevent)
because the getLayout returns null?
How is this possible? I would like to format the logevents in accordance with
the pattern as given in logback.xml.
Using logback (latest) version 0.9.5.

Hans van der Meer

Well, digging in the sources finally gave the answer.
I turns out (surprise!) that the suggested class to extend, AppenderBase, has a
nonfunctional getLayout() and setLayout(). The first always returns null, the
second one does nothing.
It is a pity the Javadoc gives no hint to this. On the contrary, it says "This
class is used to manage base functionnalities of all appenders." suggesting it
does what it says about these methods.

Solving the problem requires that your own subclass appender does something like:

private Layout layout;
public Layout getLayout(){return layout;}
public setLayout(Layout layout){this.layout = layout;}

and then all will be well.

I am posting this in the hope others who might encounter the problem can find
how to solve it.

Hans van der Meer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top