-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogger.kt
139 lines (121 loc) · 4.83 KB
/
Logger.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.ably.chat
import android.util.Log
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
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 = "ably-chat:${finalContext.tag}"
val completeContext = finalContext.staticContext + finalContext.dynamicContext.mapValues { it.value() }
val contextString = ", context: $completeContext"
val currentTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.US).format(Date())
val formattedMessage = "$currentTime (${level.name.uppercase()}) ${finalContext.tag}:${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,
)
}
}