如何将所有错误(包括未捕获的异常,NSLog调用和其他日志)重定向到Mac OS X上的日志文件? [英] How do I redirect all errors, including uncaught exceptions, NSLog calls, and other logs, to a log file on Mac OS X?

查看:255
本文介绍了如何将所有错误(包括未捕获的异常,NSLog调用和其他日志)重定向到Mac OS X上的日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试寻找用ObjC编写的Cocoa应用程序的日志记录框架.

到目前为止我尝试过的事情:

  1. 使用NSLog,但随后意识到配置和重定向非常困难.我想我可以破解它,并编写一个宏来获取我想要的信息,例如线程ID,当前函数,当前行,当前文件,当前时间,消息等,然后使用NSLog,但是……
  2. NSLog最终使用了NSLogv,后者最终使用了asl,因此我认为很棒",而是尝试使用asl,并使用默认的客户端和上下文(如果正确执行此操作,则必须创建一个新的客户端但是,除非我创建一个宏,否则它也非常冗长,而且我注意到通过asl发送的日志在整个广播系统范围内传播,而NSLog仅记录到stderr,并且我希望它们都进入同一目录记录!
  3. 然后我注意到错误的格式是不同的(不同的日期戳等),因此现在有了第三个日志记录上下文.

我可以使用哪种日志记录框架设置以方便的方式通过该框架记录所有消息,以便在应用程序出现问题时,开发人员可以获取日志文件,并找出出了什么问题? /p>

我不想简单地重定向stderr,我想拥有一个包含所有日志的结构化日志输出.我不希望某些日志进入标准输出,我不希望任何日志发送到syslogd,我只希望将所有日志写入一个文件,该文件可以可靠地标识有关该日志消息的所有相关信息(例如线程ID,消息,调用记录器的功能等),格式易于查看和可视化.

我想要的是将所有当前日志重定向到新的目的地.

有人有什么建议吗?

有效地,以ObjC术语来说,我想做的是:

  1. 在NSLog功能上执行方法筛选".这可能吗?是否可以(重新)配置Apple System Logger的使用以覆盖同一应用程序中服务的任何先前配置?
  2. 确定所有必须捕获未处理异常的地方.这包括但可能不限于:未处理的可可异常.未处理的ObjC异常.未处理的C ++异常. Unix信号.
  3. 捕获并记录堆栈中的错误,例如CoreGraphics引发的错误. (仅记录一条消息,即使用调试器添加断点!!"的消息.).

解决方案

您可以使用_NSSetLogCStringFunction()拦截NSLog()消息(但通常不能拦截ASL). Windows版WebObjects 4已在此处进行了记录,但在当前的Mac OS和iOS版本也是如此.但是,它是一个私有函数,随时可能消失,因此您不应在已发布的代码中依赖它.

如果您希望能够对非调试版本安全地执行此操作,建议您复制上的雷达.

I am attempting to find a logging framework for a Cocoa application, written in ObjC.

What I've attempted so far:

  1. Use NSLog, but then realise that it is very hard to configure and redirect. I suppose I could hack it and write a macro that obtains the information I want, such as thread ID, current function, current line, current file, current time, the message, etcetera, and then uses NSLog, however...
  2. NSLog ultimately uses NSLogv, which ultimately uses asl, so I thought "fantastic", I tried using asl instead, with the default client and context (If I was doing this properly, I would've had to create a new client for each thread), however, unless I create a macro, this is also very verbose, and I noticed that the logs sent via asl got broadcast system wide, whereas NSLog only logged to stderr, and I want them to both go to the same log!
  3. I then noticed that errors, are formatted in a different way (different datestamp, etc), so there is now a third logging context.

What l logging framework setup can I use to have ALL messages logged through that framework in a convenient fashion so that if there is a problem with an application, a developer can get the log files, and figure out what went wrong?

I don't want to simply redirect stderr, I want to have a structured log output that contains all of the logs. I don't want some logs going to standard output, I don't want any logs sent to a syslogd, I just want all the logs written to a single file, that reliably identifies all the pertinent information about that log message (such as thread ID, message, the function that called the logger, etcetera), in format that is easy to view and visualise.

What I want is to redirect all current logs to the new destination.

Does anyone have any suggestions?

EDIT:

Effectively, what I want to do, in ObjC terms is:

  1. Do "method swizzling" on the NSLog function. Is this possible? Is it possible to (re)configure the use of the Apple System Logger to override any prior configuration of the service within the same application?
  2. Determine all the places where I have to catch unhandled exceptions. THis includes, but possibly isn't limited to: Unhandled Cocoa Exceptions. Unhandled ObjC exceptions. Unhandled C++ exceptions. Unix Signals.
  3. Catch and log the stack for errors such as those raised by CoreGraphics. (The ones that simply log a message saying "Add a breakpoint using your debugger!!!").

解决方案

You can intercept NSLog() messages (but not ASL in general) using _NSSetLogCStringFunction(). It’s documented here for WebObjects 4 for Windows, but it exists in current Mac OS and iOS releases too. However, it’s a private function that may go away at any time, so you shouldn’t rely on it in released code.

If you want to be able to safely do this for non-debug builds, I suggest duplicating this enhancement request on Radar.

这篇关于如何将所有错误(包括未捕获的异常,NSLog调用和其他日志)重定向到Mac OS X上的日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆