[logback-user] Is there a Junit appender?
Chris Helck
Chris.Helck at us.icap.com
Wed Apr 28 21:10:59 CEST 2010
Thanks, this seems to be what I'm looking for, but I'm having a few problems.
1. I've checked the source code and I only see ListAppender being used in AppenderTrackerTest and StringListAppender. You said it's is used extensively, am I missing something?
2. Below is my code. It seems to work, but now the tests are echo'ing all log messages to stdout. Prior to this change the messages were going to SLF4j's NOP binding. I suspect this is because the Logback classic jar is on the classpath and is getting picked up. Long story short, I don't want log messages echo'd to stdout when running tests. Any ideas?
------------
public class DealingIntegrationTest extends AbstractIntegrationTest {
private ListAppender<ILoggingEvent> m_listAppender;
@Before
public void setupLogging() {
Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
m_listAppender = new ListAppender<ILoggingEvent>();
m_listAppender.start();
root.addAppender(m_listAppender);
}
@After
public void teardownLogging() {
Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.detachAppender(m_listAppender);
System.out.println("List " + m_listAppender.list);
}
// tests follow ....
-Chris
-----Original Message-----
From: logback-user-bounces at qos.ch [mailto:logback-user-bounces at qos.ch] On Behalf Of Ceki Gülcü
Sent: Wednesday, April 28, 2010 2:37 PM
To: logback users list
Subject: Re: [logback-user] Is there a Junit appender?
As Robert mentioned, there is an appender called ListAppender located in the ch.qos.logback.core.read package.
Assuming the ListAppender was previously attached to the root logger under the name "LIST", the test pattern would be:
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
...
@Test
public void test() {
Logger root =
(Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
ListAppender la = root.getAppender("LIST");
root.debug("hello");
assertEquals(1, listAppender.list.size());
assertEquals("hello", listAppender.list.get(0).getMessage());
}
You can also attach the ListAppender to the root logger in code. For example,
ListAppender<ILoggingEvent> listAppender =
new ListAppender<ILoggingEvent>(); listAppender.start(); root.addAppender(listAppender); Logger logger = lc.getLogger(LoggerTest.class); assertEquals(0, listAppender.list.size()); logger.debug("hello"); assertEquals(1, listAppender.list.size());
Logback uses ListAppender pretty extensively for its own unit testing.
Check the logback source code for more examples.
HTH,
On 28/04/2010 8:22 PM, Robert Elliot wrote:
> Logback has a list appender that just stores all logging events in a
> list that you can query. That's what I use for unit testing logging.
>
>
>
> On 28 Apr 2010, at 19:16, "Chris Helck" <Chris.Helck at us.icap.com
> <mailto:Chris.Helck at us.icap.com>> wrote:
>
>> I want to unit test some objects and to check that the objects are
>> logging certain messages under certain cirmcumstances. Is this
>> possible? Is it easy?
>>
>> I am using slf4j. When I run the unit tests I use the NOP logger, in
>> production we use logback.
>>
>> I want something like:
>>
>> Logger logger = LoggerFactory.getLogger(MyStuff.class);
>> logger.info <http://logger.info>( "Hello World");
>>
>> assertTrue( logger.wasLoggedAsInfo( "Hello World"));
>>
>> Thanks,
>> C. Helck
_______________________________________________
Logback-user mailing list
Logback-user at qos.ch
http://qos.ch/mailman/listinfo/logback-user
**********************************************************************
This communication and all information (including, but not limited to,
market prices/levels and data) contained therein (the "Information") is
for informational purposes only, is confidential, may be legally
privileged and is the intellectual property of ICAP plc and its affiliates
("ICAP") or third parties. No confidentiality or privilege is waived or
lost by any mistransmission. The Information is not, and should not
be construed as, an offer, bid or solicitation in relation to any
financial instrument or as an official confirmation of any transaction.
The Information is not warranted, including, but not limited, as to
completeness, timeliness or accuracy and is subject to change
without notice. ICAP assumes no liability for use or misuse of the
Information. All representations and warranties are expressly
disclaimed. The Information does not necessarily reflect the views of
ICAP. Access to the Information by anyone else other than the
recipient is unauthorized and any disclosure, copying, distribution or
any action taken or omitted to be taken in reliance on it is prohibited. If
you receive this message in error, please immediately delete it and all
copies of it from your system, destroy any hard copies of it and
notify the sender.
**********************************************************************
More information about the Logback-user
mailing list