Skip to content

Commit bbf7513

Browse files
Switch to always logging timestamps
1 parent 51fed78 commit bbf7513

2 files changed

Lines changed: 11 additions & 22 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ Here's an example of how this works:
2525

2626
```Swift
2727
try! StackdriverLogHandlerFactory.prepare(with: .init(logFilePath: "/var/log/my-app.log",
28-
defaultLogLevel: .debug,
29-
logTimestamps: true))
28+
defaultLogLevel: .debug))
3029
LoggingSystem.bootstrap { label in
3130
return StackdriverLogHandlerFactory.make()
3231
}

Sources/StackdriverLogging/StackdriverLogHandler.swift

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@ public struct StackdriverLoggingConfiguration: Codable {
1212
/// The default Logger.Level of your factory's loggers.
1313
public var logLevel: Logger.Level
1414

15-
/// Controls if a timestamp is attached to log entries. The recommended value is `false` for production environments
16-
/// in order to defer the responsability of attaching timestamps to log entries to Stackdriver itself.
17-
public var logTimestamps: Bool = false
18-
19-
public init(logFilePath: String, defaultLogLevel logLevel: Logger.Level, logTimestamps: Bool = false) {
15+
public init(logFilePath: String, defaultLogLevel logLevel: Logger.Level) {
2016
self.logFilePath = logFilePath
2117
self.logLevel = logLevel
22-
self.logTimestamps = logTimestamps
2318
}
2419

2520
}
@@ -57,8 +52,7 @@ public enum StackdriverLogHandlerFactory {
5752
var logger = StackdriverLogHandler(logFileURL: logFileURL,
5853
fileHandle: fileHandle,
5954
fileIO: fileIO,
60-
processingEventLoopGroup: eventLoopGroup,
61-
logTimestamps: config.logTimestamps)
55+
processingEventLoopGroup: eventLoopGroup)
6256
logger.logLevel = config.logLevel
6357
return logger
6458
}
@@ -72,7 +66,7 @@ public enum StackdriverLogHandlerFactory {
7266

7367
}
7468

75-
/// A `LogHandler` to log json to GCP Stackdriver using a fluentd config and the GCP logging-assistant.
69+
/// `LogHandler` to log JSON to GCP Stackdriver using a fluentd config and the GCP logging-assistant.
7670
/// Use the `MetadataValue.stringConvertible` case to log non-string JSON values supported by JSONSerializer like NSNull, Bool, Int, Float/Double, NSNumber, etc.
7771
/// The `MetadataValue.stringConvertible` type will also take care of automatically logging `Date` as an iso8601 timestamp and `Data` as a base64
7872
/// encoded `String`.
@@ -95,14 +89,11 @@ public struct StackdriverLogHandler: LogHandler {
9589

9690
private let processingEventLoopGroup: EventLoopGroup
9791

98-
private let logTimestamps: Bool
99-
100-
fileprivate init(logFileURL: URL, fileHandle: NIOFileHandle, fileIO: NonBlockingFileIO, processingEventLoopGroup: EventLoopGroup, logTimestamps: Bool) {
92+
fileprivate init(logFileURL: URL, fileHandle: NIOFileHandle, fileIO: NonBlockingFileIO, processingEventLoopGroup: EventLoopGroup) {
10193
self.logFileURL = logFileURL
10294
self.fileHandle = fileHandle
10395
self.fileIO = fileIO
10496
self.processingEventLoopGroup = processingEventLoopGroup
105-
self.logTimestamps = logTimestamps
10697
}
10798

10899
public subscript(metadataKey key: String) -> Logger.Metadata.Value? {
@@ -132,13 +123,12 @@ public struct StackdriverLogHandler: LogHandler {
132123
assert(json["message"] == nil, "'message' is a metadata field reserved by Stackdriver, your custom 'message' metadata value will be overriden in production")
133124
assert(json["severity"] == nil, "'severity' is a metadata field reserved by Stackdriver, your custom 'severity' metadata value will be overriden in production")
134125
assert(json["sourceLocation"] == nil, "'sourceLocation' is a metadata field reserved by Stackdriver, your custom 'sourceLocation' metadata value will be overriden in production")
126+
assert(json["timestamp"] == nil, "'timestamp' is a metadata field reserved by Stackdriver, your custom 'timestamp' metadata value will be overriden in production")
135127

136128
json["message"] = message.description
137129
json["severity"] = Severity.fromLoggerLevel(level).rawValue
138130
json["sourceLocation"] = ["file": Self.conciseSourcePath(file), "line": line, "function": function]
139-
if self.logTimestamps {
140-
json["timestamp"] = Self.iso8601DateFormatter.string(from: Date())
141-
}
131+
json["timestamp"] = Self.iso8601DateFormatter.string(from: Date())
142132

143133
do {
144134
let entry = try JSONSerialization.data(withJSONObject: json, options: [])
@@ -288,18 +278,18 @@ extension Logger {
288278
public mutating func setHTTPRequestMetadata(requestMethod: String?,
289279
requestUrl: String?,
290280
requestSize: String? = nil,
291-
status: Int?,
281+
status: Int? = nil,
292282
responseSize: String? = nil,
293-
userAgent: String?,
283+
userAgent: String? = nil,
294284
remoteIp: String? = nil,
295285
serverIp: String? = nil,
296-
referer: String?,
286+
referer: String? = nil,
297287
latency: String? = nil,
298288
cacheLookup: Bool? = nil,
299289
cacheHit: Bool? = nil,
300290
cacheValidatedWithOriginServer: Bool? = nil,
301291
cacheFillBytes: String? = nil,
302-
protocol: String?) {
292+
protocol: String? = nil) {
303293
var metadataValue: Logger.Metadata = [:]
304294
metadataValue["requestMethod"] = .optionalString(requestMethod)
305295
metadataValue["requestUrl"] = .optionalString(requestUrl)

0 commit comments

Comments
 (0)