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

3

13

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?

Vaporish answered 9/6, 2011 at 14:53 Comment(0)
D
9

I don't believe you can alter the logging level of NSLog() messages. You can use 3rd party logging solutions (or write your own macro) to insert different error level strings into the logs that can then be filtered on.

Check out the following libraries for pre-built logging solutions.

Driskell answered 20/1, 2012 at 1:0 Comment(3)
Do you have any opinions on these 3?Whisper
Personally I just use the DLog macro that Marcus Zarra posted here http://www.cimgf.com/2010/05/02/my-current-prefix-pch-file/ but out of the above 3 solutions, I'd only use the SOS max one if I needed remote debugging (very rare for me). LibComponentLogging seems flexible but complex. I'd look at CocoaLumberjack in the first instance.Driskell
Ha, Lumberjack - that's a clever name.Foggy
V
6

Use the ASL log:

asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!!!");

Where the ASL_LEVEL_INFO can be any of these:

ASL_LEVEL_EMERG 
ASL_LEVEL_ALERT
ASL_LEVEL_CRIT
ASL_LEVEL_ERR
ASL_LEVEL_WARNING
ASL_LEVEL_NOTICE
ASL_LEVEL_INFO
ASL_LEVEL_DEBUG
Vigorous answered 10/4, 2015 at 9:7 Comment(0)
T
2

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

Tamberg answered 11/12, 2019 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.