Neil
Neil

Reputation: 601

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)")

Upvotes: 4

Views: 592

Answers (1)

Dave Wood
Dave Wood

Reputation: 13333

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.

Upvotes: 7

Related Questions