简体   繁体   English

创建用于登录的Web Service Appender

[英]Create Web Service Appender for Logback

I have a REST API which I want to communicate with using my Logback Appender. 我有一个REST API,我想使用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. REST API本身受OAuth保护,因此,任何请求都必须使用OAuth令牌进行附加。 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. 我对Logback极为陌生,不知道如何编写自定义附加程序。

For example, the URL to obtain token is http://example.com/obtain-token and the API to push logs is http://example.com/addLogs 例如,用于获取令牌的URL是http://example.com/obtain-token ,用于推送日志的API是http://example.com/addLogs

I would really appreciate if someone can give me a dummy code on how to go about this 如果有人可以给我一个关于如何解决这个问题的虚拟代码,我将不胜感激

Logback has the implementation of server appender, you can do it like this: Logback具有服务器附加程序的实现,您可以这样执行:

    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. 请注意,Logger和其他一些类在ch.qos.logback.classic软件包中。 You can check the document about appenders in detail. 您可以详细检查有关追加程序的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM