markuswinkler / XCGLoggerNSLoggerConnector

Adds NSLogger support (with images) to XCGLogger.
MIT License
20 stars 10 forks source link

Using userInfo customise the log message domain and log image #8

Open phimage opened 7 years ago

phimage commented 7 years ago

We could use it like that

// somewhere in the code to shorten log lines
let domainKey = XCGNSLoggerLogDestination.Constants.userInfoKeyDomain
let imageKey = XCGNSLoggerLogDestination.Constants.userInfoKeyImage
...
logger.info(...., userInfo: [domainKey: "mydomain"])
logger.info(...., userInfo: [imageKey: UIImage(...)])

but I use the optional XCGLogger extension UserInfoTaggingProtocol

pod 'XCGLogger/UserInfoHelpers'

by creating a domain structure

public struct Domain: UserInfoTaggingProtocol {
    public var dictionary: [String: String] {
        return [XCGNSLoggerLogDestination.Constants.userInfoKeyDomain: name]
    }
    public init(_ name: String) {
        self.name = name
    }
    public static func name(_ name: String) -> Domain {
        return Domain(name)
    }
}

and log with it

extension Domain {
    static let monitor = Domain("monitor")
    static let test = Domain("👷")
    static let network = Domain("network")
}
extension Dev {
    static let eric = Dev("eric")
}
log.info("xxx", userInfo:  Domain.monitor | Dev.eric)

Feel free to change the Constants,baseIdentifier value

PS1 : if XCGLogger accept my next PR, I will be able to use also image in tag

public struct Image: UserInfoTaggingProtocol {

    public typealias DataType = UIImage

    public var name: UIImage
    public var dictionary: [String: Any] {
        return [XCGNSLoggerLogDestination.Constants.userInfoKeyImage: name]
    }
    public init(_ name: UIImage) {
        self.name = name
    }
    public static func name(_ name: UIImage) -> Image {
        return Image(name)
    }
}
extension Image {
    static let done = Image(UIImage(named: "doneImage")!)
}

PS2: then a subspec of your project could be created dependant on 'XCGLogger/UserInfoHelpers' with Domain and Image structs

PS3: we could also add a userInfo key Marker to call NSLogger function LogMarker