Skip to content

Commit 8e40427

Browse files
author
Joseph Manalastas Iturralde
committed
Update readme
1 parent 98bec5e commit 8e40427

File tree

4 files changed

+197
-2
lines changed

4 files changed

+197
-2
lines changed

docs/README.md

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,181 @@ Puree is a log collector which provides the following features:
99
- Batching: Send multiple logs in a single request.
1010
- Retrying: Retry to send logs after backoff time if sending logs fails.
1111

12-
[Documentation](https://ghe.ckpd.co/pages/joseph-iturralde/puree-kotlin/api/)
12+
![](./assets/overview.png)
13+
14+
Puree helps you unify your logging infrastructure.
15+
16+
[API Documentation](https://cookpad.github.io/puree-kotlin/api/)
17+
18+
# Installation
19+
20+
Gradle:
21+
22+
```gradle
23+
repositories {
24+
mavenCentral()
25+
}
26+
27+
dependencies {
28+
implementation("com.cookpad.puree-kotlin:puree-kotlin:$version")
29+
}
30+
31+
```
32+
33+
# Usage
34+
35+
## Initialization
36+
37+
Logs are posted through a `PureeLogger` instance which should be configured with the filters, outputs, store to be used and the log types to be supported, in `Application#onCreate()`.
38+
39+
```kotlin
40+
class MyApplication : Application() {
41+
lateinit var logger: PureeLogger
42+
43+
override fun onCreate() {
44+
logger = PureeLogger.Builder(
45+
lifecycle = ProcessLifeycleOwner.get().lifecycle,
46+
logSerializer = MyLogSerializer(),
47+
logStore = DbPureeLogStore(this, "my_puree.db")
48+
)
49+
.filter(
50+
MyFilter(),
51+
MyLog::class.java
52+
)
53+
.output(
54+
MyOutput(),
55+
MyLog::class.java
56+
)
57+
.build()
58+
}
59+
}
60+
```
61+
62+
## Log objects and serialization
63+
64+
Log objects must implement the `PureeLog` interface.
65+
66+
```kotlin
67+
data class MyLog(val eventName: String) : PureeLog
68+
```
69+
70+
Internally, Puree operates on log objects as JSON objects. Puree requires clients to implement a `PureeLogSerializer` that serializes the logs to JSON.
71+
72+
Sample serializer using kotlinx.serialization
73+
74+
```kotlin
75+
class MyLogSerializer : PureeLogSerializer {
76+
override fun serialize(log: PureeLog): JSONObject {
77+
val json = Json.encodeToString(log)
78+
return JSONObject(json)
79+
}
80+
}
81+
```
82+
83+
## Filters
84+
85+
`PureeFilter` can be registered to add common fields to specific logs.
86+
87+
```kotlin
88+
class AddTimeFilter : PureeFilter {
89+
override fun applyFilter(log: JSONObject): JSONObject {
90+
return log.apply {
91+
put("event_time", System.currentTimeMillis())
92+
}
93+
}
94+
}
95+
```
96+
97+
`PureeFilter` can also be used to skip logs by returning `null`
98+
99+
```kotlin
100+
class SamplingFilter(private val samplingRate: Float) : PureeFilter {
101+
override fun applyFilter(log: JSONObject): JSONObject? {
102+
return log.takeUnless { samplingRate < Random.nextFloat() }
103+
}
104+
}
105+
```
106+
107+
## Outputs
108+
109+
There are two types of outputs: non-buffered and buffered.
110+
111+
`PureeOutput`: Non-buffered output. Writes logs immediately.
112+
`PureeBufferedOutput`: Buffered output. Enqueues logs to a local store and then flush them in the background.
113+
114+
For non-buffered outputs, implement a `PureeOutput`
115+
116+
```kotlin
117+
class LogcatOutput : PureeOutput {
118+
override fun emit(log: JSONObject) {
119+
Log.d("Puree", log.toString())
120+
}
121+
}
122+
```
123+
124+
For buffered ouptuts, extend `PureeBufferedOutput`. Buffered outputs can be configured by overriding its settings.
125+
126+
```kotlin
127+
class ServerLogBufferedOutput(private val logServerApi: LogServerApi) : PureeBufferedOutput("server_log_buffered") {
128+
override val flushInterval: Duration = Duration.ofMinutes(5) // Flush frequency
129+
override val logsPerFlush: Int = 1000 // Maximum number of logs in a batch.
130+
override val maxRetryCount: Int = 5 // Number of times a flush can be retried on failure
131+
override val exponentialBackoffBase: Duration = Duration.ofSeconds(2) // Base wait duration when retrying a failed flush
132+
override val purgeableAge: Duration? = Duration.ofDays(30) // How long the buffered logs are kept before purging
133+
override val maxFlushSizeInBytes: Long = Long.MAX_VALUE // The maximum size in bytes of the whole payload
134+
135+
override fun emit(logs: List<JSONObject>, onSuccess: () -> Unit, onFailed: (Throwable) -> Unit) {
136+
logServerApi.send(logs) { isSuccessful ->
137+
if (isSuccessful) {
138+
onSuccess()
139+
} else {
140+
onFailed(IOException("Error"))
141+
}
142+
}
143+
}
144+
}
145+
```
146+
147+
## Posting logs
148+
149+
Send logs using the `PureeLogger` instance:
150+
151+
```kotlin
152+
val pureeLogger = PureeLogger.Builder()
153+
// Configure the logger
154+
.build()
155+
156+
pureeLogger.postLog(MyLog())
157+
```
158+
159+
## Release engineering
160+
161+
* Update publication version in `gradle/publishing.gradle`
162+
* Create a Release in Github
163+
* Add release notes
164+
* Publish release
165+
166+
## License
167+
168+
```
169+
Copyright (c) 2021 Cookpad Inc.
170+
171+
Permission is hereby granted, free of charge, to any person obtaining a copy
172+
of this software and associated documentation files (the "Software"), to deal
173+
in the Software without restriction, including without limitation the rights
174+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
175+
copies of the Software, and to permit persons to whom the Software is
176+
furnished to do so, subject to the following conditions:
177+
178+
The above copyright notice and this permission notice shall be included in all
179+
copies or substantial portions of the Software.
180+
181+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
183+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
184+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
185+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
186+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
187+
OR OTHER DEALINGS IN THE SOFTWARE.
188+
189+
```

docs/assets/overview.png

146 KB
Loading

gradle/publishing.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ext.publication.version = [
88
'patch': '0',
99
]
1010
ext.publication.versionName = "${publication.version.major}.${publication.version.minor}.${publication.version.patch}"
11-
ext.publication.groupId = 'com.cookpad'
11+
ext.publication.groupId = 'com.cookpad.puree-kotlin'
1212
ext.publication.artifactId = 'puree-kotlin'
1313
ext.publication.description = 'Puree-kotlin is a log collector for Android'
1414
ext.publication.url = 'https://github.com/cookpad/puree-kotlin'

mkdocs.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
site_name: Puree-Kotlin
2+
site_author: Cookpad
3+
site_description: Logging library for Android
4+
site_url: https://github.com/epishie/puree-kotlin
5+
6+
repo_name: dokka
7+
repo_url: https://github.com/cookpad/puree-kotlin
8+
9+
copyright: 'Copyright &copy; 2021 Cookpad Inc.'
10+
11+
nav:
12+
- Overview: README.md
13+
- API Documentation: api/index.html
14+
15+
theme:
16+
name: 'material'
17+
palette:
18+
primary: orange

0 commit comments

Comments
 (0)