Skip to content

Commit

Permalink
Clear extra imports
Browse files Browse the repository at this point in the history
  • Loading branch information
javiermanzo committed Dec 18, 2024
1 parent 99dce9c commit ecf012c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 67 deletions.
6 changes: 4 additions & 2 deletions Example/LogBirdExample/LogBirdExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import LogBird
struct LogBirdExampleApp: App {

init() {
LogBird.setIdentifier("🏀")
let extraMessages: [LBExtraMessage] = [
LBExtraMessage(title: "Title Extra", message: "Message extra")
]

for level in LBLogLevel.allCases {
LogBird.log("Log Message", extraMessages: extraMessages, additionalInfo: ["test":"value"], level: level)
LogBird.log("Log Message with extra messages", extraMessages: extraMessages, additionalInfo: ["test":"value"], level: level)
}

let someError = NSError(domain: "com.myapp.error", code: 500, userInfo: [NSLocalizedDescriptionKey: "Unknown error"])
LogBird.log("Log Error", error: someError, level: .error)

LogBird.setIdentifier("🏀")
LogBird.log("Log With Console Identifier")
}

var body: some Scene {
Expand Down
64 changes: 32 additions & 32 deletions Sources/LogBird/LBManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,40 @@ import OSLog
import Combine

final class LBManager: @unchecked Sendable {

private let logger: Logger
private var identifier: String?
private let dispatchQueue: DispatchQueue = DispatchQueue(label: "com.logbird.accessQueue")

private let source: LBSource

private var logs: [LBLog] = []
private let logsSubject = CurrentValueSubject<[LBLog], Never>([])
var logsPublisher: AnyPublisher<[LBLog], Never> {
logsSubject.eraseToAnyPublisher()
}

static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS ZZZZ"
formatter.timeZone = TimeZone.current
formatter.locale = Locale.current
return formatter
}()

init(subsystem: String, category: String) {
let source = LBSource(subsystem: subsystem, category: category)
self.logger = Logger(subsystem: source.subsystem, category: source.category)
self.source = source
}

func setIdentifier(_ value: String?) {
dispatchQueue.async { [weak self] in
guard let self = self else { return }
self.identifier = value
}
}

func log(_ message: String? = nil,
extraMessages: [LBExtraMessage]? = nil,
additionalInfo: [String: Any]? = nil,
Expand All @@ -52,7 +52,7 @@ final class LBManager: @unchecked Sendable {
file: String = #fileID,
function: String = #function,
line: Int = #line) {

let log = buildLogData(message: message,
extraMessages: extraMessages,
additionalInfo: additionalInfo,
Expand All @@ -61,15 +61,15 @@ final class LBManager: @unchecked Sendable {
file: file,
function: function,
line: line)

let logMessage = generateLogMessage(log)

logger.log(level: level.osLogType, "\(logMessage)")

self.logs.insert(log, at: 0)
self.logsSubject.send(self.logs)
}

private func buildLogData(message: String? = nil,
extraMessages: [LBExtraMessage]? = nil,
additionalInfo: [String: Any]? = nil,
Expand All @@ -78,11 +78,11 @@ final class LBManager: @unchecked Sendable {
file: String,
function: String,
line: Int) -> LBLog {

let errorData: LBError? = errorToLBError(error) ?? nil

let additionalInfoString = additionalInfo?.compactMapValues { "\($0)" }

let log = LBLog(
level: level,
message: message,
Expand All @@ -93,53 +93,53 @@ final class LBManager: @unchecked Sendable {
location: LBLocation(file: file, function: function, line: line),
source: LBSource(subsystem: source.subsystem, category: source.category)
)

return log
}

private func errorToLBError(_ error: Error?) -> LBError? {
guard let error else { return nil }
let nsError = error as NSError
let userInfoString = nsError.userInfo.compactMapValues { "\($0)" }

return LBError(
domain: nsError.domain,
code: nsError.code,
localizedDescription: nsError.localizedDescription,
userInfo: userInfoString
)
}

private func generateLogMessage(_ log: LBLog) -> String {
var logMessage: String = ""
let spacing: String = " "

// Header
var header: String = "\(log.level.emoji) \(log.level.rawValue.uppercased()) LogBird: \n"
if let identifier {
header = "\(identifier) \(header)"
}

logMessage = "\(logMessage)\(header)"

// Created At
let createdAt: String = "Created at:\n\(spacing)\(LBManager.dateFormatter.string(from: Date(timeIntervalSince1970: log.createdAt)))\n"
logMessage = "\(logMessage)\(createdAt)"

// Message
if let message = log.message {
let info: String = "Message:\n\(spacing)\(message)\n"
logMessage = "\(logMessage)\(info)"
}

// Extra Messages
if let extraMessages = log.extraMessages {
for extraMessage in extraMessages {
let message: String = "\(extraMessage.title):\n\(spacing)\(extraMessage.message)\n"
logMessage = "\(logMessage)\(message)"
}
}

// Additional Info
if let additionalInfo = log.additionalInfo {
var info: String = "Additional Info:\n"
Expand All @@ -150,13 +150,13 @@ final class LBManager: @unchecked Sendable {
}
logMessage = "\(logMessage)\(info)"
}

// Error
if let error = log.error {
let domain: String = "Domain: \(error.domain)\n"
let code: String = "Code: \(error.code)\n"
var userInfoString: String = ""

if let userInfo = error.userInfo {
userInfoString = "User Info:\n"
for key in userInfo.keys {
Expand All @@ -165,28 +165,28 @@ final class LBManager: @unchecked Sendable {
}
}
}

var errorString: String = "Error:\n\(spacing)\(domain)\(spacing)\(code)"
if !userInfoString.isEmpty {
errorString = "\(errorString)\(spacing)\(userInfoString)"
}

logMessage = "\(logMessage)\(errorString)"
}

// Source
let subsystem: String = "Subsystem: \(log.source.subsystem)\n"
let category: String = "Category: \(log.source.category)\n"
let source: String = "Source: \n\(spacing)\(subsystem)\(spacing)\(category)"
logMessage = "\(logMessage)\(source)"

// Location
let file: String = "File: \(log.location.file)\n"
let function: String = "Function: \(log.location.function)\n"
let line: String = "Line: \(log.location.line)\n"
let location: String = "Location:\n\(spacing)\(file)\(spacing)\(function)\(spacing)\(line)"
logMessage = "\(logMessage)\(location)"

return logMessage
}
}
16 changes: 8 additions & 8 deletions Sources/LogBird/LogBird.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Foundation
import Combine

public class LogBird: @unchecked Sendable {

private let manager: LBManager

public init(subsystem: String, category: String) {
manager = LBManager(subsystem: subsystem, category: category)
}
Expand All @@ -20,31 +20,31 @@ public class LogBird: @unchecked Sendable {
// MARK: Public Static methods
extension LogBird {
static public let shared = LogBird(subsystem: Bundle.main.bundleIdentifier ?? "", category: "general")

static public var logsPublisher: AnyPublisher<[LBLog], Never> {
shared.logsPublisher
}

static public func setIdentifier(_ identifier: String?) {
shared.setIdentifier(identifier)
}

static public func log(_ message: String, extraMessages: [LBExtraMessage]? = nil, additionalInfo: [String: Any]? = nil, error: Error? = nil, level: LBLogLevel = .debug, file: String = #fileID, function: String = #function, line: Int = #line) {
shared.log(message, extraMessages: extraMessages, additionalInfo: additionalInfo, error: error, level: level, file: file, function: function, line: line)
}
}

// MARK: Public Methods
extension LogBird {

public var logsPublisher: AnyPublisher<[LBLog], Never> {
manager.logsPublisher
}

public func setIdentifier(_ value: String?) {
manager.setIdentifier(value)
}

public func log(_ message: String, extraMessages: [LBExtraMessage]? = nil, additionalInfo: [String: Any]? = nil, error: Error? = nil, level: LBLogLevel = .debug, file: String = #fileID, function: String = #function, line: Int = #line) {
manager.log(message, extraMessages: extraMessages, additionalInfo: additionalInfo, error: error, level: level, file: file, function: function, line: line)
}
Expand Down
Loading

0 comments on commit ecf012c

Please sign in to comment.