Skip to content
loco (Log Coroutine) is a logging library using coroutine for Android.
Kotlin
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
gradle
loco-core
loco-sender-gson-jsonarray
loco-smasher-filterable-gson
loco-store-android-sqlite
sample
.gitignore
LICENSE
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

loco

loco (Log Coroutine) is a logging library using coroutine for Android.

CircleCI kotlin coroutine license

archi

Icons made by Smashicons from www.flaticon.com is licensed by CC 3.0 BY

Install

Add your necessary module.

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:

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<Pair<Sender, SendingResult>>,
    config: LocoConfig,
    offer: () -> Unit
  ) {
    delay(interval)
    offer()
  }
}

class StdOutSender : Sender {
  override suspend fun send(logs: List<SmashedLog>): SendingResult {
    logs.forEach {
      println(it.toString())
    }
    return SendingResult.SUCCESS
  }
}

class InMemoryStore : Store {
  val storage = mutableListOf<SmashedLog>()
  override suspend fun store(log: SmashedLog) {
    storage.add(log)
  }

  override suspend fun load(size: Int): List<SmashedLog> {
    return storage.take(size)
  }

  override suspend fun delete(logs: List<SmashedLog>) {
    storage.removeAll(logs)
  }
}

How to use for Android

There are some useful modules for Android.

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.

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

Multi Sender

You can configure multiple Senders.

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.

data class ClickLog(
  val id: Int,
  val name: String
) : LocoLog
Loco.send(
  ClickLog(2, "jill") // OK
)

Smasher

it serializes Log.

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.

interface Store {
  suspend fun store(log: SmashedLog)
  suspend fun load(size: Int): List<SmashedLog>
  suspend fun delete(logs: List<SmashedLog>)
}
class InMemoryStore : Store {
  val storage = mutableListOf<SmashedLog>()
  override suspend fun store(log: SmashedLog) {
    storage.add(log)
  }

  override suspend fun load(size: Int): List<SmashedLog> {
    return storage.take(size)
  }

  override suspend fun delete(logs: List<SmashedLog>) {
    storage.removeAll(logs)
  }
}

Sender

Sending List<SmashedLog> anywhare.

interface Sender {
    suspend fun send(logs: List<SmashedLog>): SendingResult
}

You should return SendingResult.

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.

interface SendingScheduler {
  suspend fun schedule(
    latestResults: List<Pair<Sender, SendingResult>>,
    config: LocoConfig,
    offer: () -> Unit
  )
}

The following is an example of a scheduler that runs at regular intervals.

class IntervalSendingScheduler(val interval: Long) : SendingScheduler {
  override suspend fun schedule(
    latestResults: List<Pair<Sender, SendingResult>>,
    config: LocoConfig,
    offer: () -> Unit
  ) {
    delay(interval)
    offer()
  }
}

LocoConfig.Extra

Loco has extra settings for convenient.

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.

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.

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<SmashedLog>, config: LocoConfig) {
          println("onSending: $sender, ${logs.size}")
        }

        override fun onEndSending(sendingResults: List<Pair<Sender, SendingResult>>, 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.
You can’t perform that action at this time.