Create Web Service Appender for Logback

1.2k views Asked by At

I have a REST API which I want to communicate with using my Logback Appender. The REST API in itself is secured by OAuth as a result of which any request needs to be attached with the OAuth token. Is it possible to write a custom appender which can do this. I'm extremely new to Logback and have no idea how to write a custom appender.

For example, the URL to obtain token is http://example.com/obtain-token and the API to push logs is http://example.com/addLogs

I would really appreciate if someone can give me a dummy code on how to go about this

1

There are 1 answers

0
Mavlarn On

Logback has the implementation of server appender, you can do it like this:

    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    SocketAppender appender = new SocketAppender();
    appender.setName("MyServer");
    appender.setContext(context);
    appender.setRemoteHost("some host");
    appender.setPort(82323);

    appender.start();

    // Wrap the appender in an Async appender for performance
    AsyncAppender asyncAppender = new AsyncAppender();
    asyncAppender.setContext(context);
    asyncAppender.setName("ASYNC_SERVER");
    asyncAppender.setQueueSize(500);
    asyncAppender.addAppender(appender);
    asyncAppender.start();

    context.getLogger("ROOT").addAppender(asyncAppender);

Be aware that the Logger and some other classes are in ch.qos.logback.classic package. You can check the document about appenders in detail.