Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is XCGLogger thread safe?

I am considering using XCGLogger to replace CocoaLumberjack and would like to know if is it permissible to log the following from any thread using a global logger created and setup on the main thread as per the README?

log.info("This is not a valid format: \(inputStr)")
like image 575
Neil Avatar asked Feb 10 '23 10:02

Neil


1 Answers

TL;DR: Yes, XCGLogger is thread safe, but it uses println() which itself it's thread safe, so other callers of println() can make it appear as if XCGLogger itself isn't.

XCGLogger uses a queue to ensure all println() it calls are called and completed in a thread safe manner.

Note however that if you call println() directly from elsewhere in your app, or another library does, those calls are not thread safe and could still interfere with the calls from XCGLogger.

There's a unit test in the project (testMultiThreaded) that shows multiple calls to XCGLogger through a concurrent queue and they all write safely. But you can see that Xcode itself outputs information about the tests as they're running and that output can become tangled with the log output.

like image 73
Dave Wood Avatar answered Feb 12 '23 23:02

Dave Wood