loki4j / loki-logback-appender

Fast and lightweight implementation of Logback appender for Grafana Loki
https://loki4j.github.io/loki-logback-appender/
BSD 2-Clause "Simplified" License
300 stars 26 forks source link

Stand-alone Loki client #113

Open nehaev opened 3 years ago

nehaev commented 3 years ago

Separate a Loki client from logback specifics in order to provide a library that can be used independently of logging frameworks (e.g. logback, log4j).

SanjulaGanepola commented 6 days ago

@nehaev I am interested in using a stand-alone Loki client to send logs directly to Loki without any appender. It looks like you have all the items here checked off except for tests. By chance is this ready to be used and if yes, could you share a code snippet. Thanks!

nehaev commented 6 days ago

Hi @SanjulaGanepola, currently it's possible only if you're willing to dive deep into the code, and potentially deal with some incompatible changes in Loki4j's internal interfaces in future.

The high-level code snippet looks like this:

var pipelineConf = PipelineConfig.builder()
    ...
    .build();
var pipeline = new AsyncBufferPipeline(pipelineConf);
pipeline.start();
// Send messages in async fashion
pipeline.append(timeStampMs, nanos, stream, message);
...
// When done, stop the pipeline
pipeline.stop();

But there are a lot of low-level nuances, of course. Anyway, you can always find a working version that uses exactly these interfaces in https://github.com/loki4j/loki-logback-appender/blob/main/loki-logback-appender/src/main/java/com/github/loki4j/logback/Loki4jAppender.java