-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ECO-4998] feat: add logger abstraction
- Loading branch information
Showing
11 changed files
with
200 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.ably.chat | ||
|
||
import android.util.Log | ||
import java.time.LocalDateTime | ||
|
||
fun interface LogHandler { | ||
fun log(message: String, level: LogLevel, throwable: Throwable?, context: LogContext) | ||
} | ||
|
||
data class LogContext( | ||
val tag: String, | ||
val staticContext: Map<String, String> = mapOf(), | ||
val dynamicContext: Map<String, () -> String> = mapOf(), | ||
) | ||
|
||
internal interface Logger { | ||
val context: LogContext | ||
fun withContext( | ||
tag: String? = null, | ||
staticContext: Map<String, String> = mapOf(), | ||
dynamicContext: Map<String, () -> String> = mapOf(), | ||
): Logger | ||
|
||
fun log( | ||
message: String, | ||
level: LogLevel, | ||
throwable: Throwable? = null, | ||
newTag: String? = null, | ||
newStaticContext: Map<String, String> = mapOf(), | ||
) | ||
} | ||
|
||
internal fun Logger.trace( | ||
message: String, | ||
throwable: Throwable? = null, | ||
tag: String? = null, | ||
staticContext: Map<String, String> = mapOf(), | ||
) { | ||
log(message, LogLevel.Trace, throwable, tag, staticContext) | ||
} | ||
|
||
internal fun Logger.debug( | ||
message: String, | ||
throwable: Throwable? = null, | ||
tag: String? = null, | ||
staticContext: Map<String, String> = mapOf(), | ||
) { | ||
log(message, LogLevel.Debug, throwable, tag, staticContext) | ||
} | ||
|
||
internal fun Logger.info(message: String, throwable: Throwable? = null, tag: String? = null, staticContext: Map<String, String> = mapOf()) { | ||
log(message, LogLevel.Info, throwable, tag, staticContext) | ||
} | ||
|
||
internal fun Logger.warn(message: String, throwable: Throwable? = null, tag: String? = null, staticContext: Map<String, String> = mapOf()) { | ||
log(message, LogLevel.Warn, throwable, tag, staticContext) | ||
} | ||
|
||
internal fun Logger.error( | ||
message: String, | ||
throwable: Throwable? = null, | ||
tag: String? = null, | ||
staticContext: Map<String, String> = mapOf(), | ||
) { | ||
log(message, LogLevel.Error, throwable, tag, staticContext) | ||
} | ||
|
||
internal fun LogContext.mergeWith( | ||
tag: String? = null, | ||
staticContext: Map<String, String> = mapOf(), | ||
dynamicContext: Map<String, () -> String> = mapOf(), | ||
): LogContext { | ||
return LogContext( | ||
tag = tag ?: this.tag, | ||
staticContext = this.staticContext + staticContext, | ||
dynamicContext = this.dynamicContext + dynamicContext, | ||
) | ||
} | ||
|
||
internal class AndroidLogger( | ||
private val minimalVisibleLogLevel: LogLevel, | ||
override val context: LogContext, | ||
) : Logger { | ||
|
||
override fun withContext(tag: String?, staticContext: Map<String, String>, dynamicContext: Map<String, () -> String>): Logger { | ||
return AndroidLogger( | ||
minimalVisibleLogLevel = minimalVisibleLogLevel, | ||
context = context.mergeWith(tag, staticContext, dynamicContext), | ||
) | ||
} | ||
|
||
override fun log(message: String, level: LogLevel, throwable: Throwable?, newTag: String?, newStaticContext: Map<String, String>) { | ||
if (level.logLevelValue <= minimalVisibleLogLevel.logLevelValue) return | ||
val finalContext = context.mergeWith(newTag, newStaticContext) | ||
val tag = finalContext.tag | ||
val completeContext = finalContext.staticContext + finalContext.dynamicContext.mapValues { it.value() } | ||
|
||
val contextString = ", context: $completeContext" | ||
val formattedMessage = "[${LocalDateTime.now()}] $tag ${level.name} ably-chat: ${message}$contextString" | ||
when (level) { | ||
// We use Logcat's info level for Trace and Debug | ||
LogLevel.Trace -> Log.i(tag, formattedMessage, throwable) | ||
LogLevel.Debug -> Log.i(tag, formattedMessage, throwable) | ||
LogLevel.Info -> Log.i(tag, formattedMessage, throwable) | ||
LogLevel.Warn -> Log.w(tag, formattedMessage, throwable) | ||
LogLevel.Error -> Log.e(tag, formattedMessage, throwable) | ||
LogLevel.Silent -> {} | ||
} | ||
} | ||
} | ||
|
||
internal class CustomLogger( | ||
private val logHandler: LogHandler, | ||
private val minimalVisibleLogLevel: LogLevel, | ||
override val context: LogContext, | ||
) : Logger { | ||
|
||
override fun withContext(tag: String?, staticContext: Map<String, String>, dynamicContext: Map<String, () -> String>): Logger { | ||
return CustomLogger( | ||
logHandler = logHandler, | ||
minimalVisibleLogLevel = minimalVisibleLogLevel, | ||
context = context.mergeWith(tag, staticContext, dynamicContext), | ||
) | ||
} | ||
|
||
override fun log(message: String, level: LogLevel, throwable: Throwable?, newTag: String?, newStaticContext: Map<String, String>) { | ||
if (level.logLevelValue <= minimalVisibleLogLevel.logLevelValue) return | ||
val finalContext = context.mergeWith(newTag, newStaticContext) | ||
logHandler.log( | ||
message = message, | ||
level = level, | ||
throwable = throwable, | ||
context = finalContext, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters