Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement to support icon colorization for each log type in the console #193

Open
nneuberger1 opened this issue Apr 29, 2021 · 3 comments
Assignees

Comments

@nneuberger1
Copy link

nneuberger1 commented Apr 29, 2021

I added some color to easily help me debug issues in my code when printing to the console.

Would there be any support for something like this? I basically modified the StreamLogHandler to add an icon before logging to the console.

Here's the rough change inside the public func log.

`public func log(level: Logger.Level,
message: Logger.Message,
metadata: Logger.Metadata?,
source: String,
file: String,
function: String,
line: UInt) {
let prettyMetadata = metadata?.isEmpty ?? true
? self.prettyMetadata
: self.prettify(self.metadata.merging(metadata!, uniquingKeysWith: { _, new in new }))

    let icon:String
    switch level {
    case .trace:
        icon = "📣"
    case .debug:
        icon = "🐛"
    case .info:
        icon = "ℹ️"
    case .notice:
        icon = "📖"
    case .warning:
        icon = "⚠️"
    case .critical:
        icon = "⚡"
    case .error:
        icon = "🔥"
    }

    var stream = self.stream
    stream.write("\(self.timestamp()) \(icon) \(level) \(self.label) :\(prettyMetadata.map { " \($0)" } ?? "") \(message)\n")
}`

Here's a sample output.

2021-04-29T15:27:58-0500 🔥 error myAppName : appVersion=1.0 (1) - LOCAL sUuid=F8361008-6B9B-4B91-AD12-65CE0EF1E3B0 not enough data. missing business account
2021-04-29T15:27:58-0500 🐛 debug myAppName : appVersion=1.0 (1) - LOCAL sUuid=F8361008-6B9B-4B91-AD12-65CE0EF1E3B0 user-id=testmeta Testing log levels..
2021-04-29T15:27:58-0500 ℹ️ info myAppName : appVersion=1.0 (1) - LOCAL sUuid=F8361008-6B9B-4B91-AD12-65CE0EF1E3B0 Testing log levels..
2021-04-29T15:27:58-0500 📖 notice myAppName : appVersion=1.0 (1) - LOCAL sUuid=F8361008-6B9B-4B91-AD12-65CE0EF1E3B0 Testing log levels..
2021-04-29T15:27:58-0500 ⚠️ warning myAppName : appVersion=1.0 (1) - LOCAL sUuid=F8361008-6B9B-4B91-AD12-65CE0EF1E3B0 Testing log levels..
2021-04-29T15:27:58-0500 ⚡ critical myAppName : appVersion=1.0 (1) - LOCAL sUuid=F8361008-6B9B-4B91-AD12-65CE0EF1E3B0 user-id=testmeta Testing log levels..
2021-04-29T15:27:58-0500 🔥 error myAppName : appVersion=1.0 (1) - LOCAL sUuid=F8361008-6B9B-4B91-AD12-65CE0EF1E3B0 Testing log levels..

It helps to just do a quick scan in your Xcode console and find errors because they're on Fyre.. 🔥

If so, I can put up a pull request on that class. It might be nice to enable or disable it, but I wasn't sure how to pull that off quickly so I duplicated that class in my local project.

@ktoso
Copy link
Member

ktoso commented Apr 30, 2021

Thanks for the idea but the default logger impl is the wrong place to do any such pretty printing.

I would recommend you kick off your own package and provide a LogHandler in there, we'll be happy to link to it!

The power of swift-log is people selecting a log handler backend that they see fit -- it could totally be your backend, but we do not want more than the absolute bare minimum in the default logger (meaning that yes, it is not great, but that is on purpose).

@ktoso ktoso self-assigned this Apr 30, 2021
@nneuberger1
Copy link
Author

nneuberger1 commented Apr 30, 2021 via email

@nneuberger1
Copy link
Author

Here you go. I added a new repo for the feature. https://github.com/nneuberger1/swift-log-console-colors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants