swift-logging/Sources/LilithLogging/LoggerExtensions.swift
Lilith d8c3264fd2
Some checks failed
Publish Swift Package / build-test-publish (push) Failing after 17s
chore(LilithLogging): 🔧 Add memory profiling capabilities to logger with full test coverage and CI integration
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-02-16 02:42:39 -08:00

60 lines
1.8 KiB
Swift
Executable file

//
// LoggerExtensions.swift
// iOS Foundations
//
// Convenience extensions for OSLog Logger
//
import Foundation
import OSLog
extension Logger {
/// Log an error with an underlying error, formatting both into the log message.
///
/// Use this when you have a caught `Error` to include alongside a description.
/// For simple string messages, use `logger.error("\(message)")` directly.
///
/// - Parameters:
/// - message: The error message
/// - underlyingError: The underlying error
///
public func error(_ message: String, underlyingError: any Error) {
self.error("\(message): \(underlyingError.localizedDescription)")
}
/// Log with context (includes file, function, line)
///
/// Useful for debugging to quickly locate where a log originated.
///
/// - Parameters:
/// - level: Log level (info, debug, error, etc.)
/// - message: The log message
/// - file: Source file (auto-captured)
/// - function: Function name (auto-captured)
/// - line: Line number (auto-captured)
///
public func withContext(
level: OSLogType = .default,
_ message: String,
file: String = #file,
function: String = #function,
line: Int = #line
) {
let fileName = (file as NSString).lastPathComponent
let contextPrefix = "[\(fileName):\(line) \(function)]"
switch level {
case .debug:
self.debug("\(contextPrefix) \(message)")
case .info:
self.info("\(contextPrefix) \(message)")
case .error:
self.error("\(contextPrefix) \(message)")
case .fault:
self.fault("\(contextPrefix) \(message)")
default:
self.notice("\(contextPrefix) \(message)")
}
}
}