You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+41-26Lines changed: 41 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ A [SwiftLog](https://github.com/apple/swift-log) `LogHandler` that logs GCP Sta
4
4
For more information on Stackdriver structured logging, see: https://cloud.google.com/logging/docs/structured-logging and [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry)
5
5
6
6
## Dependencies
7
-
This Stackdriver `LogHandler`has a dependency on [SwiftNIO](https://github.com/apple/swift-nio) which is used to create and save your new log entries in a non-blocking fashion.
7
+
This Stackdriver `LogHandler`depends on [SwiftNIO](https://github.com/apple/swift-nio) which is used to create and save your new log entries in a non-blocking fashion.
8
8
9
9
## How to install
10
10
@@ -19,29 +19,45 @@ In your target's dependencies add `"StackdriverLogging"` e.g. like this:
19
19
```
20
20
21
21
## Bootstrapping
22
-
**Check out [bootstrapping Stackdriver logging for a Vapor 4 application](https://gist.github.com/jordanebelanger/4307bf34b4ff256c9c8ec52d94db905b) for a practical example using Vapor 4.**
23
-
24
-
A factory is used to instantiate `StackdriverLogHandler` instances. Before bootstrapping your `LoggingSystem`, you must first call the `StackdriverLogHandlerFactory.prepare(:)` function with a `StackdriverLoggingConfiguration`, an NIO `NonBlockingFileIO` to write the logs asynchronously and an `EventLoopGroup` used to process new log entries in the background.
25
-
26
-
You are responsible for gracefully shutting down the NIO dependencies used to prepare the `StackdriverLogHandlerFactory`.
27
-
28
-
Here's an example of how this works:
29
-
```Swift
30
-
let config =StackdriverLoggingConfiguration(logFilePath: "var/log/myapp.log", defaultLogLevel: "debug")
31
-
32
-
let threadPool =NIOThreadPool(numberOfThreads: NonBlockingFileIO.defaultThreadPoolSize)
33
-
threadPool.start()
34
-
let fileIO =NonBlockingFileIO(threadPool: threadPool)
35
-
let eventLoopGroup =MultiThreadedEventLoopGroup(numberOfThreads: NonBlockingFileIO.defaultThreadPoolSize)
A factory is used to instantiate `StackdriverLogHandler` instances. Before bootstrapping your swift-log `LoggingSystem`, you must first call the `StackdriverLogHandler.Factory.prepare(_:_:)` with your logging destination.
23
+
The Logging destination can be either the standard output which would be whats expected under a gcp Cloud Run environment or a file of your choice.
24
+
You are also responsible for gracefully shutting down the NIO dependencies used internally by the `StackdriverLogHandler.Factory` by calling its shutdown function, preferably in a defer statement right after preparing the factory.
try LoggingSystem.bootstrap(from: &env) { (logLevel) -> (String) -> LogHandler in
49
+
return { label -> LogHandler in
50
+
var logger = StackdriverLogHandler.Factory.make()
51
+
logger.logLevel= logLevel
52
+
return logger
53
+
}
54
+
}
55
+
let app =Application(env)
56
+
defer { app.shutdown() }
57
+
tryconfigure(app)
58
+
try app.run()
59
+
```
60
+
45
61
## Logging JSON values using `Logger.MetadataValue`
46
62
To log metadata values as JSON, simply log all JSON values other than `String` as a `Logger.MetadataValue.stringConvertible` and, instead of the usual conversion of your value to a `String` in the log entry, it will keep the original JSON type of your values whenever possible.
47
63
@@ -91,8 +107,10 @@ Will log the non pretty-printed representation of:
91
107
```
92
108
93
109
## Stackdriver logging agent + fluentd config
94
-
You must use this LogHandler in combination with the Stackdriver logging agent https://cloud.google.com/logging/docs/agent/installation and a matching json format
95
-
google-fluentd config (/etc/google-fluentd/config.d/example.conf) to automatically send your JSON logs to Stackdriver.
110
+
You should preferably run the agent using the standard output destination `StackdriverLogHandler.Destination.stdout` which will get you up and running automatically under certain gcp environments such as Cloud Run.
111
+
112
+
If you prefer logging to a file, you can use a file destination `StackdriverLogHandler.Destination.stdout` in combination with the Stackdriver logging agent https://cloud.google.com/logging/docs/agent/installation and a matching json format
113
+
google-fluentd config (/etc/google-fluentd/config.d/example.conf) to automatically send your JSON logs to Stackdriver for you.
96
114
97
115
Here's an example google-fluentd conf file that monitors a json based logfile and send new log entries to Stackdriver:
98
116
```
@@ -111,6 +129,3 @@ Here's an example google-fluentd conf file that monitors a json based logfile an
111
129
tag exampletag
112
130
</source>
113
131
```
114
-
115
-
## Future
116
-
A Stackdriver gRPC API based implementation is being considered.
/// Creates a new `StackdriverLogHandler` instance.
62
-
publicstaticfunc make()->StackdriverLogHandler{
63
-
assert(initialized ==true,"You must prepare the `StackdriverLogHandlerFactory` with the `prepare` method before creating new loggers.")
64
-
return logger
65
-
}
66
-
67
-
}
68
-
69
6
/// `LogHandler` to log JSON to GCP Stackdriver using a fluentd config and the GCP logging-assistant.
70
-
/// Use the `MetadataValue.stringConvertible` case to log non-string JSON values supported by JSONSerializer like NSNull, Bool, Int, Float/Double, NSNumber, etc.
7
+
/// Use the `MetadataValue.stringConvertible` case to log non-string JSON values supported by JSONSerializer such as NSNull, Bool, Int, Float/Double, NSNumber, etc.
71
8
/// The `MetadataValue.stringConvertible` type will also take care of automatically logging `Date` as an iso8601 timestamp and `Data` as a base64
72
9
/// encoded `String`.
73
10
///
74
11
/// The log entry format matches https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
75
12
///
76
-
/// ** Use the `StackdriverLogHandlerFactory` to instantiate new `StackdriverLogHandler` instances.
13
+
/// ** Use the `StackdriverLogHandler.Factory` to instantiate new `StackdriverLogHandler` instances.
/// Creates a new `StackdriverLogHandler` instance.
295
+
publicstaticfunc make()->StackdriverLogHandler{
296
+
assert(state ==.running,"You must prepare the `StackdriverLogHandler.Factory` with the `prepare` method before creating new loggers.")
297
+
return logger
298
+
}
299
+
300
+
}
301
+
}
302
+
264
303
// Stackdriver related metadata helpers
265
304
extensionLogger{
266
305
/// Set the metadata for a Stackdriver formatted "LogEntryOperation", i.e used to give a unique tag to all the log entries related to some, potentially long running, operation
0 commit comments