Skip to content

Instantly share code, notes, and snippets.

@bill350
Last active March 31, 2022 20:46
Show Gist options
  • Save bill350/80da7ef16e52d05692d96709b8ee96c8 to your computer and use it in GitHub Desktop.
Save bill350/80da7ef16e52d05692d96709b8ee96c8 to your computer and use it in GitHub Desktop.
import Foundation
import SwiftyBeaver
import os
final class OSLogDestination: BaseDestination {
fileprivate var level: SwiftyBeaver.Level
init(level: SwiftyBeaver.Level) {
self.level = level
}
override func send(_ level: SwiftyBeaver.Level,
msg: String,
thread: String,
file: String,
function: String,
line: Int,
context: Any?) -> String? {
// Be sure to log only allowed levels
if level.rawValue >= self.level.rawValue {
let log = self.createOSLog(context: context)
let fileName = SwiftyBeaver.fileNameOfFile(file)
os_log("%@.%@:%i - \n%@",
log: log,
type: self.osLogLevelRelated(to: level),
fileName, function, line, msg)
}
return super.send(level,
msg: msg,
thread: thread,
file: file,
function: function,
line: line)
}
}
private extension OSLogDestination {
func createOSLog(context: Any?) -> OSLog {
var currentContext = "Default"
if let loggerContext = context as? String {
currentContext = loggerContext
}
let subsystem = Bundle.main.bundleIdentifier ?? "com.logger.default"
let customLog = OSLog(subsystem: subsystem, category: currentContext)
return customLog
}
func osLogLevelRelated(to swiftyBeaverLogLevel: SwiftyBeaver.Level) -> OSLogType {
var logType: OSLogType
switch swiftyBeaverLogLevel {
case .debug:
logType = .debug
case .verbose:
logType = .default
case .info:
logType = .info
case .warning:
//We use "error" here because of 🔶 indicator in the Console
logType = .error
case .error:
//We use "fault" here because of 🔴 indicator in the Console
logType = .fault
}
return logType
}
}
@Signify
Copy link

Signify commented Oct 29, 2019

Why does the line
let fileName = SwiftyBeaver.fileNameOfFile(file) give me the error:
Type 'SwiftyBeaver' has no member 'fileNameOfFile' ?

Regards,
Amanda

@Signify
Copy link

Signify commented Oct 29, 2019

In the BaseDestination class file, it shows the method:

/// returns the filename of a path
func fileNameOfFile(_ file: String) -> String {
let fileParts = file.components(separatedBy: "/")
if let lastPart = fileParts.last {
return lastPart
}
return ""
}
But when I changed the line from

let fileName = SwiftyBeaver.fileNameOfFile(file) to
let fileName = BaseDestination.fileNameOfFile(file)

I get the error: 'fileNameOfFile' is inaccessible due to 'internal' protection level.
But the class says open not internal.

open class BaseDestination: Hashable, Equatable

Have any suggestions?

Thanks,

Amanda

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment