I’ve never done this with Log4j2, so I don’t know how convoluted the appender 
is.

However, I wouldn’t make the appender actually write the data - I’d just 
validate the payload is what I wanted.  

I’d try and create the appender in the unit test, install the appender during 
the initialization of the test, and then call a method on the appender to 
validate the format inside a JUnit assert.  Hopefully that makes sense - I 
don’t have time to work up an example at the moment.

HTH

> On Nov 9, 2016, at 10:28 AM, idioma <corda.ila...@gmail.com> wrote:
> 
> Hi, thanks for your reply. It is exactly what I am trying to do as my
> processor is calling a void method that if a certain condition is met prints
> out LOG.info("some text {}", arg1, arg2). The issue is that I can't really
> Unit test the format of the message because it is a void method. I have had
> a look around in terms of custom appenders and found out that with Log4j2
> (which I am currently using) requires a slightly more convoluted solution.
> Here an example:
> 
> import java.io.Serializable;
> import java.util.concurrent.locks.*;
> import org.apache.logging.log4j.core.*;
> import org.apache.logging.log4j.core.appender.AbstractAppender;
> import org.apache.logging.log4j.core.appender.AppenderLoggingException;
> import org.apache.logging.log4j.core.config.plugins.*;
> import org.apache.logging.log4j.core.layout.PatternLayout;
> 
> // note: class name need not match the @Plugin name.
> @Plugin(name="MyCustomAppender", category="Core", elementType="appender",
> printObject=true)
> public final class MyCustomAppenderImpl extends AbstractAppender {
> 
>    private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
>    private final Lock readLock = rwLock.readLock();
> 
>    protected MyCustomAppenderImpl(String name, Filter filter,
>            Layout<? extends Serializable> layout, final boolean
> ignoreExceptions) {
>        super(name, filter, layout, ignoreExceptions);
>    }
> 
>    // The append method is where the appender does the work.
>    // Given a log event, you are free to do with it what you want.
>    // This example demonstrates:
>    // 1. Concurrency: this method may be called by multiple threads
> concurrently
>    // 2. How to use layouts
>    // 3. Error handling
>    @Override
>    public void append(LogEvent event) {
>        readLock.lock();
>        try {
>            final byte[] bytes = getLayout().toByteArray(event);
>            System.out.write(bytes);
>        } catch (Exception ex) {
>            if (!ignoreExceptions()) {
>                throw new AppenderLoggingException(ex);
>            }
>        } finally {
>            readLock.unlock();
>        }
>    }
> 
>    // Your custom appender needs to declare a factory method
>    // annotated with `@PluginFactory`. Log4j will parse the configuration
>    // and call this factory method to construct an appender instance with
>    // the configured attributes.
>    @PluginFactory
>    public static MyCustomAppenderImpl createAppender(
>            @PluginAttribute("name") String name,
>            @PluginElement("Layout") Layout<? extends Serializable> layout,
>            @PluginElement("Filter") final Filter filter,
>            @PluginAttribute("otherAttribute") String otherAttribute) {
>        if (name == null) {
>            LOGGER.error("No name provided for MyCustomAppenderImpl");
>            return null;
>        }
>        if (layout == null) {
>            layout = PatternLayout.createDefaultLayout();
>        }
>        return new MyCustomAppenderImpl(name, filter, layout, true);
>    }
> }
> 
> I wonder whether I am going too far in order to test the log output. I have
> tried to have the log4j2 configurations for the file output working with the
> intent to read the content of the log file, but with no luck. I am not sure
> I can see how to use the above implementation in my test, any idea?
> 
> Thank you,
> 
> I. 
> 
> 
> 
> --
> View this message in context: 
> http://camel.465427.n5.nabble.com/Help-in-testing-a-custom-processor-tp5789934p5789963.html
> Sent from the Camel Development mailing list archive at Nabble.com.

Reply via email to