-
-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Capture Logs from OSLog as Breadcrumbs #1461
Comments
Is this implemented? Want to have my error OSLog messages hit sentry but if not possible will just switch em over |
Hello @djmango, this is not implemented yet. The easiest way to make it happen right now is to create a global log function where you also create a breadcrumb. Something like: func log(_ message : String) {
SentrySDK.addBreadcrumb(Breadcrumb(level: .debug, category: message))
NSLog("%@", message)
} |
Got it. Heres a little utility struct I wrote, this is what I'm using as a drop in replacement for Logger to also log fatals and errors to sentry. It logs them as just Info level, but works well enough for me. //
// SentryLogger.swift
// Invisibility
//
// Created by Sulaiman Ghori on 3/24/24.
// Copyright © 2024 Invisibility Inc. All rights reserved.
//
import Foundation
import OSLog
import Sentry
struct SentryLogger {
private let logger: Logger
private let subsystem: String
private let category: String
init(subsystem: String, category: String) {
logger = Logger(subsystem: subsystem, category: category)
self.subsystem = subsystem
self.category = category
}
private func log(_ message: String, level: SentryLevel, file: String, function: String, line: Int) {
let breadcrumb = Breadcrumb(level: level, category: category)
let formattedMessage = "\(file):\(line) \(function) - \(message)"
breadcrumb.message = formattedMessage
SentrySDK.addBreadcrumb(breadcrumb)
switch level {
case .debug:
logger.debug("\(message)")
case .info:
logger.info("\(message)")
case .warning:
logger.warning("\(message)")
case .error:
logger.error("\(message)")
SentrySDK.capture(message: message)
case .fatal:
logger.fault("\(message)")
SentrySDK.capture(message: message)
default:
logger.info("\(message)")
}
}
func debug(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(message, level: .debug, file: file, function: function, line: line)
}
func info(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(message, level: .info, file: file, function: function, line: line)
}
func warning(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(message, level: .warning, file: file, function: function, line: line)
}
func error(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(message, level: .error, file: file, function: function, line: line)
}
func fault(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(message, level: .fatal, file: file, function: function, line: line)
}
} |
@djmango thanks for sharing! |
Similar to #1247, but for os_log.
Useful resources:
The text was updated successfully, but these errors were encountered: