https://github.com/sys1yagi/loco
loco (Log Coroutine) is a logging library using coroutine for Android.
https://github.com/sys1yagi/loco
android coroutine kotlin logging
Last synced: about 1 year ago
JSON representation
loco (Log Coroutine) is a logging library using coroutine for Android.
- Host: GitHub
- URL: https://github.com/sys1yagi/loco
- Owner: sys1yagi
- License: mit
- Created: 2019-05-17T04:21:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-01T11:15:58.000Z (about 7 years ago)
- Last Synced: 2025-03-23T02:12:44.773Z (over 1 year ago)
- Topics: android, coroutine, kotlin, logging
- Language: Kotlin
- Size: 207 KB
- Stars: 23
- Watchers: 1
- Forks: 1
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# loco
loco (Log Coroutine) is a logging library using coroutine for Android.
[](https://circleci.com/gh/sys1yagi/loco)   
# Install
Add your necessary module.
```groovy
dependencies {
// core
implementation 'com.sys1yagi.loco:loco-core:1.1.0'
// optional, use Gson to serialize the log, and you can use filters to process the logs.
implementation 'com.sys1yagi.loco:loco-smasher-filterable-gson:1.0.0'
// optional, persist the log using sqlite on android.
implementation 'com.sys1yagi.loco:loco-store-android-sqlite:1.0.0'
}
```
# How to use
Loco has several modules such as log collection and sending, schduler and more.
Each module is provided by interface and can be implemented as you want.
You can implement all modules like below:
```kotlin
fun startLoco() {
Loco.start(
LocoConfig(
store = InMemoryStore(), // persistent layer for buffering logs.
smasher = GsonSmasher(Gson()), // serializer for logs.
senders = mapOf(
// log senders and mapping
StdOutSender() to listOf(
ClickLog::class
)
),
scheduler = IntervalSendingScheduler(5000L) // sending interval scheduler
)
)
// send logs anytime, anywhere
Loco.send(
ClickLog(1, "jack")
)
}
data class ClickLog(
val id: Int,
val name: String
) : LocoLog
class GsonSmasher(val gson: Gson) : Smasher {
override fun smash(log: LocoLog): String {
return gson.toJson(log)
}
}
class IntervalSendingScheduler(val interval: Long) : SendingScheduler {
override suspend fun schedule(
latestResults: List>,
config: LocoConfig,
offer: () -> Unit
) {
delay(interval)
offer()
}
}
class StdOutSender : Sender {
override suspend fun send(logs: List): SendingResult {
logs.forEach {
println(it.toString())
}
return SendingResult.SUCCESS
}
}
class InMemoryStore : Store {
val storage = mutableListOf()
override suspend fun store(log: SmashedLog) {
storage.add(log)
}
override suspend fun load(size: Int): List {
return storage.take(size)
}
override suspend fun delete(logs: List) {
storage.removeAll(logs)
}
}
```
# How to use for Android
There are some useful modules for Android.
```groovy
dependencies {
// core
implementation 'com.sys1yagi.loco:loco-core:1.1.0'
// use Gson to serialize the log, and you can use filters to process the logs.
implementation 'com.sys1yagi.loco:loco-smasher-filterable-gson:1.0.0'
// persist the log using sqlite on android.
implementation 'com.sys1yagi.loco:loco-store-android-sqlite:1.0.0'
}
```
All you have to do is prepare sender and scheduler, and add mappings.
```kotlin
class SampleApplication : Application() {
override fun onCreate() {
Loco.start(
LocoConfig(
store = LocoAndroidSqliteStore(), // loco-store-android-sqlite
smasher = FilterableGsonSmasher(Gson()), // loco-smasher-filterable-gson
senders = // ...
scheduler = // ...
)
)
}
}
```
See more [sample](https://github.com/sys1yagi/loco/tree/master/sample)
## Multi Sender
You can configure multiple Senders.
```kotlin
Loco.start(
LocoConfig(
store = // ...,
smasher = // ...,
senders = mapOf(
NetworkSender() to listOf(
ClickLog::class,
ScreenLog::class
),
LogcatSender() to listOf(
ScreenLog::class
)
),
scheduler = // ...,
)
)
```
# Classes
## LocoLog
It is marker interface. Logs passed to Loco must implement LocoLog.
```kotlin
data class ClickLog(
val id: Int,
val name: String
) : LocoLog
```
```kotlin
Loco.send(
ClickLog(2, "jill") // OK
)
```
## Smasher
it serializes Log.
```kotlin
interface Smasher {
fun smash(log: LocoLog): String
}
```
The serialized log is set to SmashedLog and passed to the store.
## SmashedLog
SmashedLog has a serialized log and type names.
## Store
Store is responsible for persisting, reading and deleting SmashedLog.
```kotlin
interface Store {
suspend fun store(log: SmashedLog)
suspend fun load(size: Int): List
suspend fun delete(logs: List)
}
```
```kotlin
class InMemoryStore : Store {
val storage = mutableListOf()
override suspend fun store(log: SmashedLog) {
storage.add(log)
}
override suspend fun load(size: Int): List {
return storage.take(size)
}
override suspend fun delete(logs: List) {
storage.removeAll(logs)
}
}
```
## Sender
Sending `List` anywhare.
```kotlin
interface Sender {
suspend fun send(logs: List): SendingResult
}
```
You should return `SendingResult`.
```kotlin
enum class SendingResult {
SUCCESS, // consume log record
FAILED, // consume log record
RETRY // not consume log record
}
```
## SendingScheduler
The SendingScheduler determines the sending interval.
It receives the latest sending result and config.
You can decide on the next execution plan based on them.
```kotlin
interface SendingScheduler {
suspend fun schedule(
latestResults: List>,
config: LocoConfig,
offer: () -> Unit
)
}
```
The following is an example of a scheduler that runs at regular intervals.
```kotlin
class IntervalSendingScheduler(val interval: Long) : SendingScheduler {
override suspend fun schedule(
latestResults: List>,
config: LocoConfig,
offer: () -> Unit
) {
delay(interval)
offer()
}
}
```
## LocoConfig.Extra
Loco has extra settings for convenient.
```kotlin
data class Extra(
val defaultSender: Sender? = null,
val sendingBulkSize: Int = 10,
val internist: Internist? = null
)
```
### defaultSender
If you want to send all the logs in a single Sender, you can use defaultSender.
```kotlin
Loco.start(
LocoConfig(
store = //... ,
smasher = //... ,
senders = mapOf(),
scheduler = //... ,
extra = LocoConfig.Extra(
defaultSender = LocatSender()
)
)
)
```
### sendingBulkSize
You can set the number of logs when sending. The default is 10. This value is passed to Store#load(size: Int).
__NOTE: This value is deprecated in 1.2.0.__
### Internist
What's happening in Loco?
You can make an internist intrude.
Internist can receive inner event of Loco.
```kotlin
Loco.start(
LocoConfig(
store = //... ,
smasher = //... ,
senders = //... ,
scheduler = //... ,
LocoConfig.Extra(
internist = object : Internist {
override fun onSend(locoLog: LocoLog, config: LocoConfig) {
println("onSend")
}
override fun onStore(log: SmashedLog, config: LocoConfig) {
println("onStore")
}
override fun onStartSending() {
println("onStartSending")
}
override fun onSending(sender: Sender, logs: List, config: LocoConfig) {
println("onSending: $sender, ${logs.size}")
}
override fun onEndSending(sendingResults: List>, config: LocoConfig) {
println("onStartSending")
}
}
)
)
)
```
# License
```
MIT License
Copyright (c) 2019 Toshihiro Yagi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```