【问题标题】:NSLog redirecting to a file will also write to /var/log/system.logNSLog 重定向到文件也会写入 /var/log/system.log
【发布时间】:2012-12-18 10:07:34
【问题描述】:

我有一个应用程序,我必须在其中维护该应用程序的日志文件。我正在使用 NSLog 进行日志记录。但这也会写入系统日志。如何摆脱这个。

方法:

NSString *appending_filename=[log_dir_path stringByAppendingString:@"/MyApp.log"];

const char *log_p = [appending_filename cStringUsingEncoding:NSUTF8StringEncoding];

newStderr= freopen(log_p, "a", stderr);

NSLog(@" This should only goes to MyApp.log");

【问题讨论】:

  • 那为什么要使用NSLog,使用一些用户自定义的方法。
  • 因为使用 NSLog 会自动获取带有 PID、时间、应用程序名称的日志。而且您不必一次又一次地打开/关闭文件描述符。

标签: objective-c macos cocoa nslog freopen


【解决方案1】:

NSLog 将始终记录到系统日志,请参阅documentation

您可能想查看这篇文章以了解其他一些可以执行您想要的操作的日志库:

is there a popular open source NSLog replacement that handles enabling only in DEBUG and different log levels?

【讨论】:

    【解决方案2】:

    SO 中搜索 Quietlog 并在 google 中执行相同操作。将Quietlog()复制到你的程序中,如果需要修改它然后

    #define NSLog() Quietlog()
    

    在您的程序中。因此,您可以将输出重定向(和格式化)到您想要的任何内容。

    【讨论】:

      【解决方案3】:

      即使尝试了一天不同的事情,我也没有得到任何解决方案。所以继续使用用户定义的函数本身。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-19
        • 2015-12-07
        • 2018-09-24
        • 2021-01-14
        相关资源
        最近更新 更多