Cocoalumberjack: Absorbing third party NSLog statements

Created on 14 Mar 2014  路  5Comments  路  Source: CocoaLumberjack/CocoaLumberjack

Is there a way for NSLog statements generated by Cocoa libraries (and third party libraries for which we have no control over) to be absorbed by Lumberjack? Most specifically, we'd like those to be included in the FileLogger, since the file logger will miss out on important log statements.

Most helpful comment

It's not evident anywhere easily, but for future people finding this (which is #1 on google for me) this feature is now a part of CocoaLumberjack via the DDASLLogCapture class:

https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/Classes/DDASLLogCapture.h

Pretty much all you have to do is:
[DDASLLogCapture start];

All 5 comments

Maybe a post install hook could add

#undef NSLog
#define NSLog DDLog...

To all the libraries' prefix files.

Hi. Unfortunately, that won't catch NSLog statements emitted by Foundation and other Apple frameworks. Also, that assumes you use Cocoapods.

I played around with it a bit last week and you can redirect stderr to file easily by using freopen() as is documented on the web. This works except that there is no synchronization between NSLog and Lumberjack, and thus the log statements end up overwriting each other as you would expect given multi-threaded code which isn't synchronized.

I also found this https://github.com/JensAyton/watchlog/blob/master/watchlog/main.m, which shows how you can get notified about ASL log events. I hacked it together into the file logger just to see if it would work, and with some massaging, it does seem to. One issue is that the log events come out of order, since ASL doesn't immediately broadcast the log notification, but that is better than not having NSLog events at all.

One question which I don't know the answer to is whether Apple will allow use these ASL notification functions in the app store? When testing on my Mac, I get all app log messages unless I filter by my process id. On my device, I only get log messages from my own app, so it appears ASL is respecting the sandbox environment the app is running in. Hopefully that would suggest that we can use this API in a production app. Anyone know the answer to this?

That sounds cool.

It would be even better if you could feed those messages to DDLog directly so it can benefit any logger, not only logging to files.

Do you think you can come up with a pull request to start with?

I can create a pull request. I'll add a little more flesh to it, though I'm not that familiar with the inner logic of your library so it may be better for you to properly integrate it.

It's not evident anywhere easily, but for future people finding this (which is #1 on google for me) this feature is now a part of CocoaLumberjack via the DDASLLogCapture class:

https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/Classes/DDASLLogCapture.h

Pretty much all you have to do is:
[DDASLLogCapture start];

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jason-Chan picture Jason-Chan  路  10Comments

ivulyov90 picture ivulyov90  路  6Comments

adib picture adib  路  6Comments

levey picture levey  路  5Comments

Nathan187 picture Nathan187  路  7Comments