Blog

RxNetty 使ってるときに access log を取得する方法

RxNetty を利用しているときに、access log を取得しておくと便利です。

RxNetty 自体には access log を取得するフックがないので以下のようなフィルタを用意してみました。 DateTimeFormatter の速度を考慮して、1秒以内の場合にはキャッシュしているところがおしゃれです。

利用方法としては、以下のように acccess log 取得用の ロガーに流すだけです。簡単ですね。

        Logger accessLogLogger = LoggerFactory.getLogger("access_log");
        RequestHandler<ByteBuf, ByteBuf> handler = new RxNettyAccessLogFilter((request, response) -> {
            response.writeString("Hello");
            return response.close();
        }, request -> "-", accessLogLogger::info);
       RxNetty.createHttpServer(8080, handler).startAndWait();