I want to create two log files in my iPhone application. If I redirect the NSLog output to a file using freopen(), then all the log statements will go into one mentioned files.
But I want to put some log statements into one file while some into different file.
Any idea, how can I do that?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- how do I log requests and responses for debugging
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
Start with the Lumberjack framework: https://github.com/robbiehanson/CocoaLumberjack
There are a couple ways to achieve this. Here's one example: Add 2 file loggers:
So at this point, all log statements will get directed to both fileLogger1 & fileLogger2. Next we're going to add a "filter" to each fileLogger so they ignore log statements not directed at them.
We'll do this by creating 2 different logging macros: ALog() & BLog()
fileLoggerA will only write log statements from ALog(). fileLoggerB will only write log statements from BLog().
To set this up we can take advantage of the "logging context". You could do something as simple as:
Obviously you can get more advanced than this (e.g. add support for log levels, etc) But the thing to note is how ALog has a "context" of 1, and BLog has a "context" of 2.
Now you just need to create a "custom formatter/filter". Here's an example:
And, of course, add your custom formatter/filter to the file loggers:
More information on these topics can be found via the Lumberjack project pages:
https://github.com/robbiehanson/CocoaLumberjack/wiki/GettingStarted https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomFormatters https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomContext https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomLogLevels
The best approach would be to write your own logger class as a drop in replacement that you use instead of NSLog(). That way you can easily determine in run-time which logs should be written to which file. Your custom class can use NLog() to write to the console at the same time. This seems like the optimum approach, since
freopen()
simply bulk redirects all log output.