Skip to content

Commit 51fed78

Browse files
Merge pull request #2 from Brainfinance/factory
Switch to a config and factory based initialization to avoid needless…
2 parents 38a0379 + 74d6107 commit 51fed78

File tree

4 files changed

+130
-189
lines changed

4 files changed

+130
-189
lines changed

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import PackageDescription
55

66
let package = Package(
77
name: "StackdriverLogging",
8+
platforms: [
9+
.macOS(.v10_14)
10+
],
811
products: [
912
.library(name: "StackdriverLogging", targets: ["StackdriverLogging"]),
1013
],
@@ -13,7 +16,7 @@ let package = Package(
1316
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
1417

1518
// Used for non-blocking fileIO
16-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.2.0"),
19+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.2.0")
1720
],
1821
targets: [
1922
.target(name: "StackdriverLogging", dependencies: ["NIO", "Logging"]),

README.md

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,26 @@ This Stackdriver `LogHandler` has a dependency on [SwiftNIO](https://github.com/
1111
### Swift Package Manager
1212

1313
```swift
14-
.package(url: "https://github.com/Brainfinance/StackdriverLogging.git", from:"1.0.1")
14+
.package(url: "https://github.com/Brainfinance/StackdriverLogging.git", from:"2.0.0")
1515
```
1616
In your target's dependencies add `"StackdriverLogging"` e.g. like this:
1717
```swift
1818
.target(name: "App", dependencies: ["StackdriverLogging"]),
1919
```
2020

2121
## Bootstrapping
22-
Here is an example of how a `StackdriverLogHandler` could be bootstrapped, notice that the `StackdriverLogHandler` initializer will throw if it receives an invalid filepath.
22+
A factory `StackdriverLogHandlerFactory` is used to instantiate `StackdriverLogHandler` instances. Before bootstrapping your `LoggingSystem`, you must first call the `StackdriverLogHandlerFactory.prepare(:)` function with a `StackdriverLoggingConfiguration` to prepare the factory.
23+
24+
Here's an example of how this works:
2325

2426
```Swift
25-
LoggingSystem.bootstrap { label -> LogHandler in
26-
// ...
27-
if label == "Stackdriver" {
28-
do {
29-
var logHandler = try StackdriverLogHandler(logFilePath: "/var/log/my_app.log")
30-
logHandler.logLevel = .error
31-
return logHandler
32-
} catch {
33-
// The logFilePath does not exist or is inacessible, defaulting to a console logger
34-
print("Failed to create a StackdriverLogHandler with error: '\(error.localizedDescription)'")
35-
return ConsoleLogger()
36-
}
37-
}
38-
// ...
39-
}
27+
try! StackdriverLogHandlerFactory.prepare(with: .init(logFilePath: "/var/log/my-app.log",
28+
defaultLogLevel: .debug,
29+
logTimestamps: true))
30+
LoggingSystem.bootstrap { label in
31+
return StackdriverLogHandlerFactory.make()
32+
}
33+
4034
```
4135
## Logging JSON values using `Logger.MetadataValue`
4236
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.
@@ -87,7 +81,7 @@ Will log the non pretty-printed representation of:
8781
```
8882

8983
## Stackdriver logging agent + fluentd config
90-
You can use this LogHandler in combination with the Stackdriver logging agent https://cloud.google.com/logging/docs/agent/installation and a matching json format
84+
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
9185
google-fluentd config (/etc/google-fluentd/config.d/example.conf) to automatically send your JSON logs to Stackdriver.
9286

9387
Here's an example google-fluentd conf file that monitors a json based logfile and send new log entries to Stackdriver:
@@ -107,3 +101,6 @@ Here's an example google-fluentd conf file that monitors a json based logfile an
107101
tag exampletag
108102
</source>
109103
```
104+
105+
## Future
106+
A Stackdriver gRPC API based implementation is being considered.

Sources/StackdriverLogging/ReadWriteLock.swift

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)