touchlab / Kermit

Kermit by Touchlab is a Kotlin Multiplatform centralized logging utility.
https://kermit.touchlab.co
Apache License 2.0
736 stars 41 forks source link

Investigate logging Error and NSError for Apple targets #384

Open kpgalligan opened 1 year ago

kpgalligan commented 1 year ago

https://developer.apple.com/documentation/foundation/nserror https://developer.apple.com/documentation/swift/error

Context: https://kotlinlang.slack.com/archives/CTJB58X7X/p1698398439685649

KevinSchildhorn commented 1 week ago

Hi there. Is there any simple/recommended way to log an Swift Error or NSError using kermit?

KevinSchildhorn commented 1 week ago

Currently if you print an error, it's somewhat self sufficient.

enum IntParsingError: Error {
         case overflow
}
// Swift
let error = NSError(domain: "MyDomain", code: 123, userInfo: ["Detail" : 789])
print(error)
print(error.localizedDescription)
ErrorTest().logNSError(error: )
ErrorTest().logNSError(error: IntParsingError.overflow)
// Kotlin
class ErrorTest {
    fun logNSError(error: NSError) {
        Logger.i { error.toString() }
    }
}
Error Domain=MyDomain Code=123 "(null)" UserInfo={Detail=789}
The operation couldn’t be completed. (MyDomain error 123.)
🟢 Error Domain=MyDomain Code=123 "(null)" UserInfo={Detail=789}
🟢 KermitSampleIOS.IntParsingError.overflow

There isn't a strict way to log an error, or a logger that takes in an error specifically.

Logger and OSLog don't seem to take in Errors or anything similar.