Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pluto-plugins/plugins/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Install Pluto as a Tree for Timber:
Timber.plant(PlutoTimberTree());
```

Console output is enabled by default. To keep these logs in Pluto without duplicating them in Logcat, disable console output when planting the tree:
```kotlin
Timber.plant(PlutoTimberTree(printToConsole = false))
```

But if you are connected to Logcat, PlutoLogs behave similar to Log class, with an improvement to tag the method and file name also. In Logcat, PlutoLogs will look like the following.
```
D/onClick(MainActivity.kt:40) | debug_log: button clicked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.pluto.plugins.logger

import timber.log.Timber

class PlutoTimberTree : Timber.Tree() {
class PlutoTimberTree @JvmOverloads constructor(
@Suppress("UNUSED_PARAMETER") printToConsole: Boolean = true
) : Timber.Tree() {

override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {}
}
Expand Down
2 changes: 2 additions & 0 deletions pluto-plugins/plugins/logger/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,6 @@ dependencies {

implementation(libs.room)
ksp(libs.room.compiler)

testImplementation(libs.junit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,29 @@ import com.squareup.moshi.Types
import java.math.BigDecimal
import timber.log.Timber

class PlutoTimberTree : Timber.Tree() {
class PlutoTimberTree @JvmOverloads constructor(
private val printToConsole: Boolean = true
) : Timber.Tree() {

override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (priority == LOG_EVENT_PRIORITY) {
val eventData = eventExtractor(message)
LogsProcessor.processEvent(tag ?: "pluto_timber", eventData.first, eventData.second, Thread.currentThread().getStackTraceElement())
LogsProcessor.processEvent(
tag ?: "pluto_timber",
eventData.first,
eventData.second,
Thread.currentThread().getStackTraceElement(),
printToConsole = printToConsole
)
} else {
LogsProcessor.process(priority, tag ?: "pluto_timber", messageExtractor(message), t, Thread.currentThread().getStackTraceElement())
LogsProcessor.process(
priority,
tag ?: "pluto_timber",
messageExtractor(message),
t,
Thread.currentThread().getStackTraceElement(),
printToConsole = printToConsole
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,35 @@ internal class LogsProcessor private constructor() {

companion object {

fun process(priority: Int, tag: String, message: String, tr: Throwable?, stackTraceElement: StackTraceElement) {
fun process(
priority: Int,
tag: String,
message: String,
tr: Throwable?,
stackTraceElement: StackTraceElement,
printToConsole: Boolean = true
) {
val stackTrace = stackTraceElement.stackTrace()
LogDBHandler.persistLog(priority2Level(priority), tag, message, tr, stackTrace)
consolePrint(priority2Level(priority), tag, message, tr, stackTrace)
printToConsoleIfEnabled(printToConsole) {
consolePrint(priority2Level(priority), tag, message, tr, stackTrace)
}
}

fun processEvent(tag: String, event: String, attr: HashMap<String, Any?>?, stackTraceElement: StackTraceElement) {
fun processEvent(
tag: String,
event: String,
attr: HashMap<String, Any?>?,
stackTraceElement: StackTraceElement,
printToConsole: Boolean = true
) {
val moshi = Moshi.Builder().build()
val moshiAdapter: JsonAdapter<Map<String, Any?>?> = moshi.adapter(Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java))
val stackTrace = stackTraceElement.stackTrace()
LogDBHandler.persistEvent(Level.Event, tag, event, attr, stackTrace)
consolePrint(Level.Event, tag, "$event => ${moshiAdapter.toJson(attr)}", null, stackTrace)
printToConsoleIfEnabled(printToConsole) {
consolePrint(Level.Event, tag, "$event => ${moshiAdapter.toJson(attr)}", null, stackTrace)
}
}

@SuppressWarnings("ComplexCondition")
Expand Down Expand Up @@ -57,6 +74,10 @@ internal class LogsProcessor private constructor() {
}
}

internal inline fun printToConsoleIfEnabled(enabled: Boolean, print: () -> Unit) {
if (enabled) print()
}

private fun consolePrint(level: Level, tag: String, message: String, tr: Throwable?, trace: StackTrace) {
val newMessage = StringBuilder().append(message).append("\n").append("[logged from: ${trace.formattedStack()}]").toString()
when (level) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.pluto.plugins.logger.internal

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class LogsProcessorTest {
@Test
fun doesNotPrintWhenConsoleOutputIsDisabled() {
var printed = false

LogsProcessor.printToConsoleIfEnabled(false) {
printed = true
}

assertFalse(printed)
}

@Test
fun printsWhenConsoleOutputIsEnabled() {
var printed = false

LogsProcessor.printToConsoleIfEnabled(true) {
printed = true
}

assertTrue(printed)
}
}
Loading