UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Unified logging

Forums > macOS

I'm here for clarity. I'm trying to wrap my head around Apple's Unified Logging. I found a tutorial that explains how to access the logs via console. I understand that debug and info messages are only kept in memory and only displayed in the debug console in Xcode during development (I still don't know the difference between info and debug messages as they appear to be the same to me).

I would like to extend the Logger class to account for a "debugLevel" to determine what is logged to my Xcode debugging console while developing, but also to use in production by allowing the user to select, via a preference setting, the debugging level for the app. For example:

debugLevel 0 = (Development) no logging will occur - no messages will be displayed in the Xcode debugger console debugLevel 1 = (Development) only debug (level 1) entries will be displayed in the Xcode debugger console debugLevel 2 = (Development) both debug (levels 1 and 2) entries will be displayed in the Xcode debugger console debugLevel 3 = (Development) in addition to levels 1 and 2, inner method debug logging (level 3) will be displayed in the Xcode debugger console

I feel these three levels would be sufficient because anything marked with critical, error, fault, and notice are always displayed and logged and I will not be filling up my logs with debugging information by keeping my entries at the debug level.

For production I would use an additional three levels, so the user could turn on debug logging in order to track down an issue they may have by having the debug messages logged to the console as notices, which could then be sent to me for review.

debugLevel 0 = (Production) no debug messages logged debugLevel 4 = (Production) convert all level 1 debug entries to "notices" to be logged to the console debugLevel 5 = (Production) convert all level 1 and 2 debug entries to "notices" to be logged to the console debugLevel 6 = (Production) convert all level 1, 2, and 3 debug entries to "notices" to be logged to the console

With this, my intention is to make a level 1 debug log entry at the top of each method and then a level 2 method exit debug log entry, and all inner method debug log entries as level 3 so the logging can be somewhat controlled. Of course, critical parts of the code have logger.error, logger.critical, etc., etc., which will always be logged since those persist.

let debugLevel:Int = 3 // Full display of all debugging information
let logger = Logger(subsystem: com.carroll.myapp, category: "App Debugging")
extention Logger {
func message(_ level:Int, _ entry:String) {
        switch debugLevel {
            case 1:
                switch level {
                case 1:
                    logger.debug("\(entry)")
                default:
                    break
                }
            case 2:
                switch level {
                    case 1, 2:
                        logger.debug("\(entry)")
                    default:
                        break
                }
            case 3:
                switch level {
                    case 1, 2, 3:
                        logger.debug("\(entry)")
                    default:
                        break
                }
            case 4:
                switch level {
                    case 1:
                        logger.notice("\(entry)")
                    default:
                        break
                }
            case 5:
                switch level {
                    case 1, 2:
                        logger.notice("\(entry)")
                    default:
                        break
                }
            case 6:
                switch level {
                    case 1, 2, 3:
                        logger.notice("\(entry)")
                    default:
                        break
                }
            default:
                break
        }
    }
}

I'm sure this isn't the most efficient, if not the improper, way of doing this. Am I understanding the Unified Logging system correctly? Is there a better way of skinning this cat?

Thanks!

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.