Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions Sources/Scribe/LogConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public struct LogConfiguration: Sendable {
/// Only used by the built-in formatter.
public var includeShortCode: Bool

/// Whether to include the file and line number in the format: [file]:[line number].
///
/// Only used by the built-in formatter.
public var includeFileAndLineNumber: Bool

/// Maximum number of cached auto-generated `Logger` instances (e.g., categories from `#fileID`). `nil` means
/// unbounded. Default is 100.
public var autoLoggerCacheLimit: Int?
Expand Down Expand Up @@ -67,14 +72,16 @@ public struct LogConfiguration: Sendable {
includeTimestamp: Bool = true,
includeEmoji: Bool = true,
includeShortCode: Bool = false,
includeFileAndLineNumber: Bool = true,
autoLoggerCacheLimit: Int? = 100,
dateFormat: String = "yyyy-MM-dd HH:mm:ss.SSSZ"
) {
self.enabledCategories = enabledCategories
self.formatter = nil
formatter = nil
self.includeTimestamp = includeTimestamp
self.includeEmoji = includeEmoji
self.includeShortCode = includeShortCode
self.includeFileAndLineNumber = includeFileAndLineNumber
self.autoLoggerCacheLimit = autoLoggerCacheLimit
self.dateFormat = dateFormat
}
Expand All @@ -88,21 +95,19 @@ public struct LogConfiguration: Sendable {
/// - formatter: Custom formatter closure used to build the final log line.
/// - autoLoggerCacheLimit: Maximum cached auto-generated `Logger` instances. Pass `nil` for no limit. Default is
/// 100.
/// - dateFormat: Timestamp format string supplied for convenience to formatter users. Default is
/// `"yyyy-MM-dd HH:mm:ss.SSSZ"`.
public init(
enabledCategories: Set<LogCategory>? = nil,
formatter: @escaping @Sendable (FormatterContext) -> String,
autoLoggerCacheLimit: Int? = 100,
dateFormat: String = "yyyy-MM-dd HH:mm:ss.SSSZ"
autoLoggerCacheLimit: Int? = 100
) {
self.enabledCategories = enabledCategories
self.formatter = formatter
self.includeTimestamp = true
self.includeEmoji = true
self.includeShortCode = false
includeTimestamp = true
includeEmoji = true
includeShortCode = false
includeFileAndLineNumber = true
self.autoLoggerCacheLimit = autoLoggerCacheLimit
self.dateFormat = dateFormat
dateFormat = "yyyy-MM-dd HH:mm:ss.SSSZ"
}

/// Default configuration with timestamps enabled and all categories allowed.
Expand Down
12 changes: 10 additions & 2 deletions Sources/Scribe/LogManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,24 @@ public final class LogManager: @unchecked Sendable {
if let tsComponent {
parts.append(tsComponent)
}

if cfg.includeEmoji {
parts.append(level.emoji)
}

if cfg.includeShortCode {
parts.append("[\(level.shortCode)]")
}

parts.append("[\(category.name)]")
parts.append(message)
let fileName = (file as NSString).lastPathComponent
formatted = "\(parts.joined(separator: " ")) — \(fileName):\(line)"

if cfg.includeFileAndLineNumber {
let fileName = (file as NSString).lastPathComponent
parts.append("— \(fileName):\(line)")
}

formatted = parts.joined(separator: " ")
}

switch level.osLogType {
Expand Down