Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control error level of NSLog messages on iOS?

Tags:

ios

nslog

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?

like image 265
sorin Avatar asked Jun 09 '11 14:06

sorin


People also ask

Where does NSLog write to?

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.

What is NS log?

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.


1 Answers

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

like image 107
Joule87 Avatar answered Sep 21 '22 13:09

Joule87