1414![ Coverage] ( .github/badges/jacoco.svg )
1515![ Branches] ( .github/badges/branches.svg )
1616
17- [ ![ Milestone] ( https://img.shields.io/github/milestones/progress-percent/mjfryc/mjaron-tinyloki-java/1?label=Next%20release:%20milestone-v0.4.0 )] ( https://github.com/mjfryc/mjaron-tinyloki-java/milestone/1 )
18-
19-
20- Tiny [ Grafana Loki] ( https://grafana.com/oss/loki/ ) client (log sender) written in pure Java 1.8 without any external dependencies.
17+ Tiny [ Grafana Loki] ( https://grafana.com/oss/loki/ ) client (log sender) written in pure Java 1.8 without any external
18+ dependencies.
2119
2220* Implements JSON variant of [ Loki API] ( https://grafana.com/docs/loki/latest/api/#post-lokiapiv1push )
2321* Works with ** Android** and ** Java SE**
@@ -28,26 +26,16 @@ Tiny [Grafana Loki](https://grafana.com/oss/loki/) client (log sender) written i
2826HTTP sender requires http URL, optionally basic authentication credentials may be set.
2927
3028Short example:
29+
3130``` java
3231import pl.mjaron.tinyloki.* ;
3332
3433public class Sample {
3534 public static void main (String [] args ) {
36-
37- LogController tinyLoki = TinyLoki
38- .withUrl(" http://localhost/loki/api/v1/push" )
39- .withBasicAuth(" user" , " pass" )
40- .start();
41-
42- ILogStream stream = tinyLoki. stream() // Before v0.3.2 use createStream()
43- .info()
44- .l(" host" , " MyComputerName" )
45- .l(" customLabel" , " custom_value" )
46- .build();
47-
48- stream. log(" Hello world." );
49-
50- tinyLoki. softStop(). hardStop();
35+ TinyLoki loki = TinyLoki . withUrl(" http://localhost:3100" ). withBasicAuth(" user" , " pass" ). start();
36+ ILogStream helloStream = loki. stream(). info(). l(" topic" , " hello" ). build();
37+ helloStream. log(" Hello world!" );
38+ loki. closeSync();
5139 }
5240}
5341```
@@ -60,34 +48,51 @@ import pl.mjaron.tinyloki.*;
6048public class Sample {
6149 public static void main (String [] args ) {
6250
63- // Initialize log controller instance with URL.
64- // Usually creating more than one LogController instance doesn't make sense.
65- // LogController owns separate thread which sends logs periodically.
66- LogController tinyLoki = TinyLoki
67- .withUrl(" http://localhost/loki/api/v1/push" )
68- .withBasicAuth(" user" , " pass" )
69- .start();
70-
71- // Create streams. It is thread-safe.
72- ILogStream stream = tinyLoki. createStream(
73- // Define stream labels...
74- // Initializing log level to verbose and adding some custom labels.
75- TinyLoki . verbose()
76- .l(" host" , " MyComputerName" ) // Custom static label.
77- .l(" customLabel" , " custom_value" ) // Custom static label.
78- // Label names should start with letter
79- // and contain letters, digits and '_' only.
80- // Bad characters will be replaced by '_'.
81- // If first character is bad, it will be replaced by 'A'.
82- );
83-
84- // ... new streams and other logs here (thread-safe, non-blocking).
85- stream. log(" Hello world." );
86-
87- // Optionally flush logs before application exit.
88- tinyLoki
89- .softStop() // Try to send logs last time. Blocking method.
90- .hardStop(); // If it doesn't work (soft timeout) - force stop sending thread.
51+ // Initialize the log controller instance with URL.
52+ // The endpoint loki/api/v1/push will be added by default if missing.
53+ // Usually creating more than one TinyLoki instance doesn't make sense.
54+ // TinyLoki (its default IExecutor implementation) owns separate thread which sends logs periodically.
55+ // It may be called inside try-with-resources block, but the default close() method doesn't synchronize the logs,
56+ // but just interrupts the background worker thread.
57+ try (TinyLoki loki = TinyLoki . withUrl(" http://localhost:3100/loki/api/v1/push" )
58+ // Print all diagnostic information coming from the TinyLoki library. For diagnostic purposes only.
59+ .withVerboseLogMonitor()
60+
61+ // Set the custom log processing interval time.
62+ // So the executor will try to send the next logs 10 seconds after the previous logs sending operation.
63+ .withThreadExecutor(10 * 1000 )
64+
65+ // Set custom time of HTTP connection establishing timeout.
66+ .withConnectTimeout(10 * 1000 )
67+
68+ // Encode the logs to limit the size of data sent.
69+ .withGzipLogEncoder()
70+
71+ // The BasicBuffering is set by default, but here the (not encoded) message size limit may be customized.
72+ .withBasicBuffering(3 * 1024 * 1024 , 10 )
73+
74+ // Initialize the library with above settings.
75+ // The ThreadExecutor will create a new thread and start waiting for the logs to be sent.
76+ .start()) {
77+
78+ // Some logs here...
79+ ILogStream whiteStream = loki. stream(). l(" color" , " white" ). build();
80+ whiteStream. log(" Hello white world." );
81+
82+ // Blocking method, tries to send the logs ASAP and wait for sending completion.
83+ // This method returns false when timeout occurs, but true when sending has completed with success or failure.
84+ boolean allHttpSendingOperationsFinished = loki. sync();
85+ System . out. println(" Are all logs processed: " + allHttpSendingOperationsFinished);
86+
87+ ILogStream redStream = loki. stream(). l(" color" , " red" ). build();
88+ redStream. log(" Hello white world." );
89+
90+ // Blocking method, tries to synchronize the logs than interrupt and join the execution thread.
91+ // Set the custom timeout time for this operation.
92+ boolean closedWithSuccess = loki. closeSync(5 * 1000 );
93+
94+ System . out. println(" Synced and closed with success: " + closedWithSuccess);
95+ }
9196 }
9297}
9398```
@@ -97,11 +102,11 @@ public class Sample {
97102### Maven Central
98103
99104``` gradle
100- implementation 'io.github.mjfryc:mjaron-tinyloki-java:0.3.11 '
105+ implementation 'io.github.mjfryc:mjaron-tinyloki-java:1.0.0 '
101106```
102107
103- _ [ Maven Central page] ( https://search.maven.org/artifact/io.github.mjfryc/mjaron-tinyloki-java/ ) ,_
104- _ [ Maven Central repository URL] ( https://repo1.maven.org/maven2/io/github/mjfryc/mjaron-tinyloki-java/ ) _
108+ _ [ Maven Central page] ( https://search.maven.org/artifact/io.github.mjfryc/mjaron-tinyloki-java/ ) ,_
109+ _ [ Maven Central repository URL] ( https://repo1.maven.org/maven2/io/github/mjfryc/mjaron-tinyloki-java/ ) _
105110
106111### GitHub Packages
107112
@@ -114,10 +119,10 @@ Click the [Packages section](https://github.com/mjfryc?tab=packages&repo_name=mj
1141193 . Add this jar to project dependencies in build.gradle, e.g:
115120
116121``` gradle
117- implementation files(project.rootDir.absolutePath + '/libs/mjaron-tinyloki-java-0.3.11 .jar')
122+ implementation files(project.rootDir.absolutePath + '/libs/mjaron-tinyloki-java-1.0.0 .jar')
118123```
119124
120- ## API design
125+ ## API design (outdated)
121126
122127``` mermaid
123128classDiagram
0 commit comments