Signal-iOS/SignalServiceKit/Debugging/LogFormatter.swift
2025-12-30 11:34:05 -08:00

61 lines
2.2 KiB
Swift
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import CocoaLumberjack
import Foundation
final class LogFormatter: NSObject, DDLogFormatter {
func format(message logMessage: DDLogMessage) -> String? {
return Self.formatLogMessage(logMessage, modifiedMessage: nil)
}
static func formatLogMessage(_ logMessage: DDLogMessage, modifiedMessage: String?) -> String {
let timestamp = dateFormatter.string(from: logMessage.timestamp)
let level = Self.formattedLevel(for: logMessage.flag)
let location = Self.formattedLocation(logMessage: logMessage)
let message = modifiedMessage ?? logMessage.message
return "\(timestamp) \(level) \(location)\(message)"
}
private static let dateFormatter: DateFormatter = {
// Copied from DDLogFileFormatterDefault.
let formatter = DateFormatter()
formatter.formatterBehavior = .behavior10_4
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy/MM/dd HH:mm:ss:SSS"
return formatter
}()
private static func formattedLevel(for flag: DDLogFlag) -> String {
if flag.contains(.error) { return "ERR❤" } // Error
if flag.contains(.warning) { return "WRN🧡" } // Warning
if flag.contains(.info) { return "INF💛" } // Info
if flag.contains(.debug) { return "DBG💚" } // Debug
if flag.contains(.verbose) { return "VRB💙" } // Verbose
return "UNK💜" // Unknown
}
private static func formattedLocation(logMessage: DDLogMessage) -> String {
var file = logMessage.file
file = file.replacingOccurrences(of: ".swift", with: "")
let line = logMessage.line
// Drop the arguments from the function name.
var function = logMessage.function ?? ""
if let parensIndex = function.firstIndex(of: "(") {
function = String(function[..<parensIndex])
}
if file.isEmpty, line == 0, function.isEmpty {
return ""
}
return "[\(file):\(line)\(function.isEmpty ? "" : " ")\(function)]: "
}
}