A tiny key-value Logger implementation for Android.
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle.kts
at the end of repositories:
allprojects {
repositories {
maven(url = "https://jitpack.io")
}
}
Step 2. Add the dependency
dependencies {
implementation("com.github.asherepenko:android-logger:x.y.z")
}
Logger
defines base context inkey-value
formatLogWriter
defines the way where to store logs- Can be easily used with Archivarius
Required fields list:
message
timestamp
(RFC 3339, with fractional seconds, nanoseconds when possible)log_level
(one ofdebug
,info
,warning
,error
)application_id
Depending on the context, log records may include the following fields:
tag
user_id
app_install_id
device_serial
device_id
job_id
(for background tasks)exception
(this field might contain error stacktrace)
Create or extend BaseLogger
instance with LogWriter
implementation:
// Create logger instance
val logger = BaseLogger(
BuildConfig.APPLICATION_ID,
object : LogWriter() {
override fun write(logLevel: LogLevel, logContext: LogContext) {
val tag = extendedContext[BaseLoggerParams.TAG]
val message = extendedContext[BaseLoggerParams.MESSAGE]
val contextString = extendedContext.entries.joinToString()
when (logLevel) {
LogLevel.DEBUG -> Log.d(tag, "$message \n Context: $contextString")
LogLevel.INFO -> Log.i(tag, "$message \n Context: $contextString")
LogLevel.WARNING -> Log.w(tag, "$message \n Context: $contextString")
LogLevel.ERROR -> Log.e(tag, "$message \n Context: $contextString")
}
}
fun forceUpload(): Completable = Completable.complete()
})
// Write logs with Logger
logger.info("This is a test info message")