Skip to content

Commit a959497

Browse files
ericloe-cashsvc-squareup-copybara
authored andcommitted
Update Logger.log to support nullable Throwable
GitOrigin-RevId: 933929911db0fd1c45fa77a8abfccc1129e6cc2e
1 parent c18856f commit a959497

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

misk/src/main/kotlin/misk/web/interceptors/hooks/RequestResponseLoggingHook.kt

+17-23
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,28 @@ internal class RequestResponseLoggingHook private constructor(
106106
val randomDouble = random.current().nextDouble()
107107
val includeBody = randomDouble < sampling
108108

109-
val additionalTags: MutableSet<Tag> = mutableSetOf(
109+
var additionalTags: Set<Tag> = setOf(
110110
"response_code" to statusCode,
111111
"response_time_millis" to elapsed.toMillis(),
112-
)
112+
)
113+
114+
val error = error?.unwrap()
115+
var level = Level.INFO
116+
117+
if (error != null) {
118+
// Log with the exception and smart tags applied on error.
119+
level = mapperResolver.mapperFor(error)?.loggingLevel(error) ?: Level.ERROR
120+
additionalTags = additionalTags + SmartTagsThreadLocalHandler.peekThreadLocalSmartTags()
121+
}
113122

114123
requestResponseLoggedCapture.onLogged()
115124

116-
val error = error?.unwrap()
117-
fun buildDescriptionImpl(): Any? {
118-
return buildDescription(
125+
logger.log(
126+
level = level,
127+
th = error,
128+
tags = additionalTags.toTypedArray(),
129+
) {
130+
buildDescription(
119131
caller = caller,
120132
httpCall = httpCall,
121133
elapsedToString = elapsedToString,
@@ -124,24 +136,6 @@ internal class RequestResponseLoggingHook private constructor(
124136
includeBody = includeBody,
125137
)
126138
}
127-
128-
if (error != null) {
129-
// Log with the exception and smart tags applied on error.
130-
val level = mapperResolver.mapperFor(error)?.loggingLevel(error) ?: Level.ERROR
131-
132-
logger.log(
133-
level = level,
134-
th = error,
135-
tags = (SmartTagsThreadLocalHandler.peekThreadLocalSmartTags() + additionalTags).toTypedArray(),
136-
message = ::buildDescriptionImpl,
137-
)
138-
} else {
139-
logger.log(
140-
level = Level.INFO,
141-
tags = additionalTags.toTypedArray(),
142-
message = ::buildDescriptionImpl,
143-
)
144-
}
145139
}
146140

147141
private fun buildDescription(

wisp/wisp-logging/src/main/kotlin/wisp/logging/Logging.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fun KLogger.log(level: Level, vararg tags: Tag, message: () -> Any?) {
8787
}
8888

8989
// This logger takes care of adding the mdc tags and cleaning them up when done
90-
fun KLogger.log(level: Level, th: Throwable, vararg tags: Tag, message: () -> Any?) {
90+
fun KLogger.log(level: Level, th: Throwable?, vararg tags: Tag, message: () -> Any?) {
9191
withTags(*tags) {
9292
when (level) {
9393
Level.ERROR -> error(th, message)

0 commit comments

Comments
 (0)