
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( "Hello World"); assertTrue( logger.wasLoggedAsInfo( "Hello World")); Thanks, C. Helck ********************************************************************** 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. **********************************************************************

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@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( "Hello World");
assertTrue( logger.wasLoggedAsInfo( "Hello World"));
Thanks, C. Helck
**********************************************************************
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.
**********************************************************************
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user

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@us.icap.com <mailto:Chris.Helck@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

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@qos.ch [mailto:logback-user-bounces@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@us.icap.com <mailto:Chris.Helck@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@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. **********************************************************************

On 28/04/2010 9:10 PM, Chris Helck wrote:
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?
You should also look in logback-classic not just logback-core.
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?
That's the default logback configuration kicking in. You can either create a LoggingContext per unit test, which is what logback's own unit tests do, or reset the default logger context, the one returned by LoggerFactory. Here is the code to reset the default logger context. LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); lc.reset(); -- Ceki

Is there a way to do the equivilent of LoggingContext.reset() from a config file? The module I'm working in has hundreds of tests, I'd hate to have to add reset() to each file. Thanks, Chris -----Original Message----- From: logback-user-bounces@qos.ch [mailto:logback-user-bounces@qos.ch] On Behalf Of Ceki Gülcü Sent: Wednesday, April 28, 2010 3:31 PM To: logback users list Subject: Re: [logback-user] Is there a Junit appender? On 28/04/2010 9:10 PM, Chris Helck wrote:
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?
You should also look in logback-classic not just logback-core.
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?
That's the default logback configuration kicking in. You can either create a LoggingContext per unit test, which is what logback's own unit tests do, or reset the default logger context, the one returned by LoggerFactory. Here is the code to reset the default logger context. LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); lc.reset(); -- Ceki _______________________________________________ Logback-user mailing list Logback-user@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. **********************************************************************

Never mind. The config file <configuration><root/></configuration> Does the trick. -Chris -----Original Message----- From: logback-user-bounces@qos.ch [mailto:logback-user-bounces@qos.ch] On Behalf Of Chris Helck Sent: Wednesday, April 28, 2010 4:28 PM To: logback users list Subject: Re: [logback-user] Is there a Junit appender? Is there a way to do the equivilent of LoggingContext.reset() from a config file? The module I'm working in has hundreds of tests, I'd hate to have to add reset() to each file. Thanks, Chris -----Original Message----- From: logback-user-bounces@qos.ch [mailto:logback-user-bounces@qos.ch] On Behalf Of Ceki Gülcü Sent: Wednesday, April 28, 2010 3:31 PM To: logback users list Subject: Re: [logback-user] Is there a Junit appender? On 28/04/2010 9:10 PM, Chris Helck wrote:
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?
You should also look in logback-classic not just logback-core.
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?
That's the default logback configuration kicking in. You can either create a LoggingContext per unit test, which is what logback's own unit tests do, or reset the default logger context, the one returned by LoggerFactory. Here is the code to reset the default logger context. LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); lc.reset(); -- Ceki _______________________________________________ Logback-user mailing list Logback-user@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. ********************************************************************** _______________________________________________ Logback-user mailing list Logback-user@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. **********************************************************************

I haven't done it myself, but you could try using powermock to mock LoggerFactory to provide another mock that specifies what you want to log. Nik On Apr 28, 2010 2:16 PM, "Chris Helck" <Chris.Helck@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( "Hello World"); assertTrue( logger.wasLoggedAsInfo( "Hello World")); Thanks, C. Helck ********************************************************************** 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. ********************************************************************** _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
participants (4)
-
Ceki Gülcü
-
Chris Helck
-
Nikolas Everett
-
Robert Elliot