Skip to content

Commit

Permalink
Merge pull request #3 from planetary-social/xcode-15
Browse files Browse the repository at this point in the history
Use OSLog package to take advantage of Xcode 15 log formatting
  • Loading branch information
mplorentz authored Sep 22, 2023
2 parents 77b1df4 + 2900bde commit d25fb7d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 76 deletions.
25 changes: 0 additions & 25 deletions Package.resolved

This file was deleted.

4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "Logger",
platforms: [
.iOS(.v13)
.iOS(.v14)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
Expand All @@ -18,7 +18,7 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
.package(name: "CocoaLumberjack",
url: "https://github.com/CocoaLumberjack/CocoaLumberjack.git",
from: "3.7.0"),
from: "3.8.1"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
56 changes: 30 additions & 26 deletions Sources/Delivery/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ public class Log: LogProtocol {
}

@discardableResult
public func optional(_ error: Error?, _ detail: String? = nil) -> Bool {
service.optional(error, detail)
public func optional(_ error: Error?, _ detail: String? = nil, sourceFile: String = #file) -> Bool {
service.optional(error, detail, sourceFile: sourceFile)
}

public func info(_ string: String) {
service.info(string)
public func info(_ string: String, sourceFile: String = #file) {
service.info(string, sourceFile: sourceFile)
}

public func debug(_ string: String) {
service.debug(string)
public func debug(_ string: String, sourceFile: String = #file) {
service.debug(string, sourceFile: sourceFile)
}

public func error(_ string: String) {
service.unexpected(string, nil)
public func error(_ string: String, sourceFile: String = #file) {
service.unexpected(string, nil, sourceFile: sourceFile)
}

public func unexpected(_ reason: Reason, _ detail: String?) {
service.unexpected(reason.rawValue, detail)
public func unexpected(_ reason: Reason, _ detail: String?, sourceFile: String = #file) {
service.unexpected(reason.rawValue, detail, sourceFile: sourceFile)
}

public func fatal(_ reason: Reason, _ detail: String?) {
service.fatal(reason.rawValue, detail)
public func fatal(_ reason: Reason, _ detail: String?, sourceFile: String = #file) {
service.fatal(reason.rawValue, detail, sourceFile: sourceFile)
}
}

Expand All @@ -66,47 +66,51 @@ public extension Log {
///
/// Convenience function that unwraps the error (if exists) and logs its description
@discardableResult
static func optional(_ error: Error?, _ detail: String? = nil) -> Bool {
shared.optional(error, detail)
static func optional(_ error: Error?, _ detail: String? = nil, sourceFile: String = #file) -> Bool {
shared.optional(error, detail, sourceFile: sourceFile)
}

/// Log a INFO message
static func info(_ string: String) {
shared.info(string)
static func info(_ string: String, sourceFile: String = #file) {
shared.info(string, sourceFile: sourceFile)
}

/// Log a DEBUG message
static func debug(_ string: String) {
shared.debug(string)
static func debug(_ string: String, sourceFile: String = #file) {
shared.debug(string, sourceFile: sourceFile)
}

/// Log a ERROR message
///
/// Convencience function that categorize common errors that the app can handle
static func unexpected(_ reason: Reason, _ detail: String?) {
shared.unexpected(reason, detail)
static func unexpected(_ reason: Reason, _ detail: String?, sourceFile: String = #file) {
shared.unexpected(reason, detail, sourceFile: sourceFile)
}

/// Log a FATAL message
///
/// Convencience function that categorize common errors that the app cannot handle
static func fatal(_ reason: Reason, _ detail: String?) {
shared.fatal(reason, detail)
static func fatal(_ reason: Reason, _ detail: String?, sourceFile: String = #file) {
shared.fatal(reason, detail, sourceFile: sourceFile)
}

/// Log a ERROR message
///
/// Convenience function that unwraps an error and a response from a network call
static func optional(_ error: Error?, from response: URLResponse?) {
static func optional(_ error: Error?, from response: URLResponse?, sourceFile: String = #file) {
guard let error = error else { return }
guard let response = response else { return }
let path = response.url?.path ?? "unknown path"
let detail = "\(path) \(error)"
shared.unexpected(.apiError, detail)
shared.unexpected(.apiError, detail, sourceFile: sourceFile)
}

/// Log a ERROR message
static func error(_ message: String) {
shared.error(message)
static func error(_ message: String, _ detail: String? = nil, sourceFile: String = #file) {
shared.error(message, sourceFile: sourceFile)
}

static func error(_ error: Error, _ detail: String? = nil, sourceFile: String = #file) {
shared.optional(error, detail, sourceFile: sourceFile)
}
}
12 changes: 6 additions & 6 deletions Sources/Delivery/Models/LogProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ public protocol LogProtocol {
///
/// Convenience function that unwraps the error (if exists) and logs its description
@discardableResult
func optional(_ error: Error?, _ detail: String?) -> Bool
func optional(_ error: Error?, _ detail: String?, sourceFile: String) -> Bool

/// Log a INFO message
func info(_ string: String)
func info(_ string: String, sourceFile: String)

/// Log a DEBUG message
func debug(_ string: String)
func debug(_ string: String, sourceFile: String)

/// Log a ERROR message
func error(_ string: String)
func error(_ string: String, sourceFile: String)

/// Log a ERROR message
///
/// Convencience function that categorize common errors that the app can handle
func unexpected(_ reason: Reason, _ detail: String?)
func unexpected(_ reason: Reason, _ detail: String?, sourceFile: String)

/// Log a FATAL message
///
/// Convencience function that categorize common errors that the app cannot handle
func fatal(_ reason: Reason, _ detail: String?)
func fatal(_ reason: Reason, _ detail: String?, sourceFile: String)
}
10 changes: 5 additions & 5 deletions Sources/Domain/Services/LoggerService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ protocol LoggerService {

var fileUrls: [URL] { get }

func debug(_ string: String)
func debug(_ string: String, sourceFile: String)

func info(_ string: String)
func info(_ string: String, sourceFile: String)

func optional(_ error: Error?, _ detail: String?) -> Bool
func optional(_ error: Error?, _ detail: String?, sourceFile: String) -> Bool

func unexpected(_ reason: String, _ detail: String?)
func unexpected(_ reason: String, _ detail: String?, sourceFile: String)

func fatal(_ reason: String, _ detail: String?)
func fatal(_ reason: String, _ detail: String?, sourceFile: String)
}
41 changes: 29 additions & 12 deletions Sources/Domain/Services/LoggerServiceAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
//

import Foundation
import os.log
import OSLog

/// The LoggerServiceAdapter class can be used to output logs to files in the device's filesystem
/// The LoggerServiceAdapter class can be used componenetoutput logs to files in the device's filesystem
/// and to the Console (in real-time) when debugging
///
/// It implements LoggerService so it is meant to be used by Log as a plug-in that actually outputs to logs somewhere.
class LoggerServiceAdapter: LoggerService {

var fileLoggerService: FileLoggerService
var loggers = [String: Logger]()

init(fileLoggerService: FileLoggerService) {
self.fileLoggerService = fileLoggerService
Expand All @@ -24,37 +25,53 @@ class LoggerServiceAdapter: LoggerService {
fileLoggerService.fileUrls
}

func debug(_ string: String) {
func debug(_ string: String, sourceFile: String) {
let message = "LOG:DEBUG: \(string)"
fileLoggerService.debug(message)
os_log("%@", type: OSLogType.debug, message)
let osLogger = logger(for: sourceFile)
osLogger.debug("\(string)")
}

func info(_ string: String) {
func info(_ string: String, sourceFile: String) {
let message = "LOG:INFO: \(string)"
os_log("%@", type: OSLogType.info, message)
fileLoggerService.info(message)
let osLogger = logger(for: sourceFile)
osLogger.info("\(string)")
}

func optional(_ error: Error?, _ detail: String?) -> Bool {
func optional(_ error: Error?, _ detail: String?, sourceFile: String) -> Bool {
guard let error = error else {
return false
}
let message = "LOG:ERROR:\(detail ?? "") \(error)"
os_log("%@", type: OSLogType.error, message)
fileLoggerService.error(message)
let osLogger = logger(for: sourceFile)
osLogger.error("\(detail ?? "") \(error)")
return true
}

func unexpected(_ reason: String, _ detail: String?) {
func unexpected(_ reason: String, _ detail: String?, sourceFile: String) {
let message = "LOG:UNEXPECTED:\(reason) \(detail ?? "")"
os_log("%@", type: OSLogType.error, message)
fileLoggerService.error(message)
let osLogger = logger(for: sourceFile)
osLogger.error("\(reason) \(detail ?? "")")
}

func fatal(_ reason: String, _ detail: String?) {
func fatal(_ reason: String, _ detail: String?, sourceFile: String) {
let message = "LOG:FATAL:\(reason) \(detail ?? "")"
os_log("%@", type: OSLogType.fault, message)
fileLoggerService.error(message)
let osLogger = logger(for: sourceFile)
osLogger.error("\(reason) \(detail ?? "")")
}

private func logger(for sourceFile: String) -> Logger {
let className = (sourceFile as NSString).lastPathComponent.replacingOccurrences(of: ".swift", with: "")
if let logger = loggers[className] {
return logger
} else {
let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: className)
loggers[className] = logger
return logger
}
}
}

0 comments on commit d25fb7d

Please sign in to comment.