swift-log
swift-log copied to clipboard
bootstrapping in XCTestCase
LoggingSystem.bootstrap
must only be called once per process. One might assume to bootstrap the logger in XCTestCase.setUp
, but this can be called multiple times and will result in an assertion.
To get around this, a global lazy variable can be used that is asserted in each setUp
method. It will only be run once, the first time it is accessed, allowing for logging to be configured in XCTests:
Declare the global variable like so:
let isLoggingConfigured: Bool = {
LoggingSystem.bootstrap { label in
var handler = StreamLogHandler.standardOutput(label: label)
handler.logLevel = .debug
return handler
}
return true
}()
Then, in your XCTestCase
s, use like so:
import XCTest
final class FooTests: XCTestCase {
override func setUp() {
XCTAssert(isLoggingConfigured)
...
}
}
We should consider documenting this or providing some sort of helper.
Sounds good to me, would you have a moment PR such section into the README.md perhaps?
But what if I want to test different LogHandlers with the LoggingSystem? Seems to be impossible for now 😢
So I found a workaround in @testable import Logging
, so LoggingSystem.internalBootstrap
works and it seems to be a nice way to replace LoggingSystem's factory in tests 🌚
Btw you can find beta of my LoggingKit here 🙂
@maximkrouk you can construct a logger with a backend explicitly:
let logger = Logger(label: "foo") { label in
MyAwesomeBackend(label)
}
see docs.
Does this solution apply when using SwiftLog in an iOS app and its test cases? I'm finding the host app bootstraps the logging system fine, but then when loading tests I get the second bootstrapping error. I have attempted to use the lazy global variable approach above, but and finding its not protecting the bootstrapping from attempting again.
@leisurehound could you send us the code you use with the lazy global? That should work.
sorry, I had an unrelated config issue that was causing this, once I fixed that the tests now run without double bootstrapping the logger. Sorry for the noise.
Thanks for looping back, closing the ticket then 👍
Actually, the original ticket stands as to documenting the pattern, so opening.