I observed the NSLog()
does log all my messages with error level Warning
but when I look to the console I see other messages with different error levels like Info
, or `Error.
How can I control the error level of my messages?
NSLog outputs messages to the Apple System Log facility or to the Console app (usually prefixed with the time and the process id). Many of the system frameworks use NSLog for logging exceptions and errors, but there is no requirement to restrict its usage to those purposes.
NSLog is a very useful function call that iOS developers are able to utilize and it is one of the most common development tools used to debug iOS apps.
Another possible option you could use as a replacement for NSLog is OSLog, it comes with some nice advantages, is flexible and easy to use. For using it create an OSLog extension to set some configurations:
import Foundation
import os.log
extension OSLog {
private static var subsystem = Bundle.main.bundleIdentifier!
/// Defining my custom log objects
/// Log View Cycles.
static let viewCycle = OSLog(subsystem: subsystem, category: "View Cycle")
/// Log User Actions.
static let userAction = OSLog(subsystem: subsystem, category: "User Action")
}
Then just use it whenever you want in your View Controllers:
import UIKit
import os.log
class MenuViewController: UIViewController {
@IBAction func didTapMenu(_ sender: UIBarButtonItem) {
os_log("Did tap %@ option", log: .userAction, type: .info, "Some UI element")
}
}
As you can see you can specify a log level as a type parameter and define your own custom log objects.
Console Output: 2019-12-11 10:21:44.788204-0300 ProjectName[70193:3015307] [User Action] Did tap Some UI element option
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With