@@ -13,6 +13,7 @@ import io.github.aakira.napier.Napier
1313 * In theory, the message sent to Crashlytics could therefore be 2x this value
1414 */
1515private const val MAX_CHARS_IN_LOG = 1200
16+ private const val DEFAULT_TAG = " AppLogger"
1617
1718/* * A Crashlytics logger. The name Antilog might be an unfortunate choice by the Napier library;
1819 * this is not a stub
@@ -26,18 +27,10 @@ class CrashlyticsLogger : Antilog() {
2627 ) {
2728 if (message == null && throwable == null ) return
2829
29- if (BuildConfig .DEBUG || priority > LogLevel .DEBUG ) {
30- // also send to logcat
31- val logLevel = priority.toAndroidLogLevel()
32- val logMessage = buildString {
33- if (message != null ) append(message)
34- if (throwable != null ) {
35- if (message != null ) append(" \n " )
36- append(Log .getStackTraceString(throwable))
37- }
38- }
30+ val safeTag = tag ? : DEFAULT_TAG
3931
40- Log .println (logLevel, tag ? : " AppLogger" , logMessage)
32+ if (BuildConfig .DEBUG || priority > LogLevel .DEBUG ) {
33+ logToLogcat(priority, safeTag, message, throwable)
4134 }
4235
4336 val limitedMessage = message?.take(MAX_CHARS_IN_LOG ) ? : " (no message)" // to avoid OutOfMemoryError's
@@ -50,7 +43,29 @@ class CrashlyticsLogger : Antilog() {
5043 Firebase .crashlytics.log(limitedMessage + errorMessage)
5144 } else {
5245 Firebase .crashlytics.log(" recordException with message: $limitedMessage " )
53- Firebase .crashlytics.recordException(throwable ? : buildCrashlyticsSyntheticException(limitedMessage))
46+ Firebase .crashlytics.recordException(
47+ throwable ? : buildCrashlyticsSyntheticException(
48+ limitedMessage,
49+ safeTag
50+ )
51+ )
52+ }
53+ }
54+
55+ private fun logToLogcat (
56+ priority : LogLevel ,
57+ tag : String ,
58+ message : String? ,
59+ throwable : Throwable ?
60+ ) {
61+ val msg = message ? : " "
62+ when (priority) {
63+ LogLevel .VERBOSE -> Log .v(tag, msg, throwable)
64+ LogLevel .DEBUG -> Log .d(tag, msg, throwable)
65+ LogLevel .INFO -> Log .i(tag, msg, throwable)
66+ LogLevel .WARNING -> Log .w(tag, msg, throwable)
67+ LogLevel .ERROR -> Log .e(tag, msg, throwable)
68+ LogLevel .ASSERT -> Log .wtf(tag, msg, throwable)
5469 }
5570 }
5671
@@ -60,37 +75,23 @@ class CrashlyticsLogger : Antilog() {
6075 * This is a workaround for the fact that Crashlytics groups errors by stacktrace
6176 * [https://stackoverflow.com/a/59779764](https://stackoverflow.com/a/59779764)
6277 */
63- private fun buildCrashlyticsSyntheticException (message : String ): Exception {
78+ private fun buildCrashlyticsSyntheticException (message : String , tag : String ): Exception {
6479 val stackTrace = Thread .currentThread().stackTrace
6580 val numToRemove = 9
6681 val lastToRemove = stackTrace.getOrNull(numToRemove - 1 )
6782 if (lastToRemove == null ) {
68- Log .e(
69- null ,
70- " Got unexpected stacktrace while logging a message: ${stackTrace.contentToString()} "
71- )
83+ Log .e(tag, " Got unexpected stacktrace while logging a message: ${stackTrace.contentToString()} " )
7284 return SyntheticException (message, stackTrace)
7385 }
7486 if (lastToRemove.className != Napier ::class .java.name || lastToRemove.methodName != " e\$ default" ) {
7587 Log .e(
76- null ,
88+ tag ,
7789 " Got unexpected stacktrace: class: ${lastToRemove.className} , method: ${lastToRemove.methodName} "
7890 )
7991 }
8092 val abbreviatedStackTrace = stackTrace.takeLast(stackTrace.size - numToRemove).toTypedArray()
8193 return SyntheticException (message, abbreviatedStackTrace)
8294 }
83-
84- private fun LogLevel.toAndroidLogLevel (): Int {
85- return when (this ) {
86- LogLevel .VERBOSE -> Log .VERBOSE
87- LogLevel .DEBUG -> Log .DEBUG
88- LogLevel .INFO -> Log .INFO
89- LogLevel .WARNING -> Log .WARN
90- LogLevel .ERROR -> Log .ERROR
91- LogLevel .ASSERT -> Log .ASSERT
92- }
93- }
9495}
9596
9697class SyntheticException (
0 commit comments