Skip to content

Commit ab5a7e3

Browse files
committed
Added new timeProvider property to a logger: PR fix:
- Made it none optional.
1 parent 545a930 commit ab5a7e3

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

Log4swift/Logger.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ A logger is identified by a UTI identifier, it defines a threshold level and a d
4444

4545
/// The UTI string that identifies the logger. Example : product.module.feature
4646
public let identifier: String
47-
47+
4848
internal var parent: Logger?
4949

5050
private var thresholdLevelStorage: LogLevel
5151
private var appendersStorage: [Appender]
5252
private var asynchronousStorage = false
53-
private var timeProviderStorage: (() -> Date)?
53+
private var timeProviderStorage: () -> Date = Date.init
5454

5555
/// If asynchronous is true, only the minimum of work will be done on the main thread, the rest will be deffered to a low priority background thread.
5656
/// The order of the messages will be preserved in async mode.
@@ -105,7 +105,7 @@ A logger is identified by a UTI identifier, it defines a threshold level and a d
105105
}
106106

107107
/// Gets/sets external time provider for log messages.
108-
/// If `nil` then default behavior `Date()` will be used.
108+
/// Default behavior is just `Date()`.
109109
///
110110
/// Allows to "mock" the time - could be useful in unit tests
111111
/// which may run on some "virtual" time instead of real one.
@@ -114,7 +114,7 @@ A logger is identified by a UTI identifier, it defines a threshold level and a d
114114
/// (on the `rootLogger` for example) - entire sub-hierarchy of loggers will have it.
115115
///
116116
@objc
117-
public var timeProvider: (() -> Date)? {
117+
public var timeProvider: () -> Date {
118118
get {
119119
if let parent = self.parent {
120120
return parent.timeProvider
@@ -260,7 +260,7 @@ A logger is identified by a UTI identifier, it defines a threshold level and a d
260260
var info: LogInfoDictionary = [
261261
.LoggerName: self.identifier,
262262
.LogLevel: level,
263-
.Timestamp: (self.timeProvider?() ?? Date()).timeIntervalSince1970,
263+
.Timestamp: self.timeProvider().timeIntervalSince1970,
264264
.ThreadId: currentThreadId(),
265265
.ThreadName: currentThreadName()
266266
]
@@ -289,7 +289,7 @@ A logger is identified by a UTI identifier, it defines a threshold level and a d
289289
var info: LogInfoDictionary = [
290290
.LoggerName: self.identifier,
291291
.LogLevel: level,
292-
.Timestamp: (self.timeProvider?() ?? Date()).timeIntervalSince1970,
292+
.Timestamp: self.timeProvider().timeIntervalSince1970,
293293
.ThreadId: currentThreadId(),
294294
.ThreadName: currentThreadName()
295295
]

Log4swiftTests/LoggerTests.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ class LoggerTests: XCTestCase {
548548
parentLogger.timeProvider = { return testDate }
549549

550550
// Validate
551-
XCTAssertEqual(sonLogger.timeProvider?(), testDate)
551+
XCTAssertEqual(sonLogger.timeProvider(), testDate)
552552
}
553553

554554
func testChangingSonLoggerParameterBreakLinkWithParent() {
@@ -605,8 +605,8 @@ class LoggerTests: XCTestCase {
605605

606606
// Validate
607607
XCTAssertNil(sonLogger.parent)
608-
XCTAssertEqual(parentLogger.timeProvider?(), testDate.0)
609-
XCTAssertEqual(sonLogger.timeProvider?(), testDate.1)
608+
XCTAssertEqual(parentLogger.timeProvider(), testDate.0)
609+
XCTAssertEqual(sonLogger.timeProvider(), testDate.1)
610610
}
611611

612612
func testLoggedTimeIsTakenWhenLogIsRequested() {

0 commit comments

Comments
 (0)