Skip to content

Commit 841bdd4

Browse files
committed
Initial Commit
0 parents  commit 841bdd4

File tree

10 files changed

+302
-0
lines changed

10 files changed

+302
-0
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Build & Test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: macos-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Build
17+
run: swift build -v
18+
- name: Run tests
19+
run: swift test -v

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
Package.resolved
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1320"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "VariadicBootstrap"
18+
BuildableName = "VariadicBootstrap"
19+
BlueprintName = "VariadicBootstrap"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "swift-log-variadic-bootstrap-tests"
32+
BuildableName = "swift-log-variadic-bootstrap-tests"
33+
BlueprintName = "swift-log-variadic-bootstrap-tests"
34+
ReferencedContainer = "container:">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
<BuildActionEntry
38+
buildForTesting = "YES"
39+
buildForRunning = "YES"
40+
buildForProfiling = "YES"
41+
buildForArchiving = "YES"
42+
buildForAnalyzing = "YES">
43+
<BuildableReference
44+
BuildableIdentifier = "primary"
45+
BlueprintIdentifier = "swift-log-variadic-bootstrap"
46+
BuildableName = "swift-log-variadic-bootstrap"
47+
BlueprintName = "swift-log-variadic-bootstrap"
48+
ReferencedContainer = "container:">
49+
</BuildableReference>
50+
</BuildActionEntry>
51+
</BuildActionEntries>
52+
</BuildAction>
53+
<TestAction
54+
buildConfiguration = "Debug"
55+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
56+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
57+
shouldUseLaunchSchemeArgsEnv = "YES"
58+
codeCoverageEnabled = "YES">
59+
<Testables>
60+
<TestableReference
61+
skipped = "NO">
62+
<BuildableReference
63+
BuildableIdentifier = "primary"
64+
BlueprintIdentifier = "swift-log-variadic-bootstrap-tests"
65+
BuildableName = "swift-log-variadic-bootstrap-tests"
66+
BlueprintName = "swift-log-variadic-bootstrap-tests"
67+
ReferencedContainer = "container:">
68+
</BuildableReference>
69+
</TestableReference>
70+
</Testables>
71+
</TestAction>
72+
<LaunchAction
73+
buildConfiguration = "Debug"
74+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
75+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
76+
launchStyle = "0"
77+
useCustomWorkingDirectory = "NO"
78+
ignoresPersistentStateOnLaunch = "NO"
79+
debugDocumentVersioning = "YES"
80+
debugServiceExtension = "internal"
81+
allowLocationSimulation = "YES">
82+
</LaunchAction>
83+
<ProfileAction
84+
buildConfiguration = "Release"
85+
shouldUseLaunchSchemeArgsEnv = "YES"
86+
savedToolIdentifier = ""
87+
useCustomWorkingDirectory = "NO"
88+
debugDocumentVersioning = "YES">
89+
<MacroExpansion>
90+
<BuildableReference
91+
BuildableIdentifier = "primary"
92+
BlueprintIdentifier = "VariadicBootstrap"
93+
BuildableName = "VariadicBootstrap"
94+
BlueprintName = "VariadicBootstrap"
95+
ReferencedContainer = "container:">
96+
</BuildableReference>
97+
</MacroExpansion>
98+
</ProfileAction>
99+
<AnalyzeAction
100+
buildConfiguration = "Debug">
101+
</AnalyzeAction>
102+
<ArchiveAction
103+
buildConfiguration = "Release"
104+
revealArchiveInOrganizer = "YES">
105+
</ArchiveAction>
106+
</Scheme>

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Eric Lewis.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21+
OR OTHER DEALINGS IN THE SOFTWARE.

Package.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "swift-log-variadic-bootstrap",
8+
products: [
9+
.library(
10+
name: "VariadicBootstrap",
11+
targets: ["swift-log-variadic-bootstrap"]),
12+
],
13+
dependencies: [
14+
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
15+
],
16+
targets: [
17+
.target(
18+
name: "swift-log-variadic-bootstrap",
19+
dependencies: [
20+
.product(name: "Logging", package: "swift-log"),
21+
]),
22+
.testTarget(
23+
name: "swift-log-variadic-bootstrap-tests",
24+
dependencies: [
25+
"swift-log-variadic-bootstrap",
26+
.product(name: "Logging", package: "swift-log"),
27+
]),
28+
]
29+
)

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SwiftLogVariadicBootstrap
2+
3+
Variadic bootstrap function for [SwiftLog](https://github.com/apple/swift-log).
4+
5+
## Getting Started
6+
7+
### Adding the dependency
8+
9+
`SwiftLogVariadicBootstrap` is designed for Swift 5. To use the handler, you need to declare your dependency in your `Package.swift`:
10+
11+
```swift
12+
.package(url: "https://github.com/ericlewis/swift-log-variadic-bootstrap.git", from: "0.1.0"),
13+
```
14+
15+
and to your application/library target, add `"SwiftLogVariadicBootstrap"` to your `dependencies`, e.g. like this:
16+
17+
```swift
18+
// Target syntax for Swift up to version 5.1
19+
.target(name: "BestExampleApp", dependencies: ["SwiftLogVariadicBootstrap"]),
20+
21+
// Target for Swift 5.2
22+
.target(name: "BestExampleApp", dependencies: [
23+
.product(name: "SwiftLogVariadicBootstrap", package: "swift-log-variadic-bootstrap")
24+
],
25+
```
26+
27+
### Example
28+
29+
It works exactly like [the existing bootstrap function](https://github.com/apple/swift-log#default-logger-behavior)
30+
but it is variadic!
31+
32+
#### Allow the labels to be passed in via bootstrap
33+
```swift
34+
import Logging
35+
import VariadicBootstrap
36+
import ExampleLogBackend1
37+
import ExampleLogBackend2
38+
39+
LoggingSystem.bootstrap(ExampleLogBackend1.init, ExampleLogBackend2.init)
40+
41+
```
42+
43+
#### Set custom labels per backend
44+
```swift
45+
import Logging
46+
import VariadicBootstrap
47+
import ExampleLogBackend1
48+
import ExampleLogBackend2
49+
50+
let backend1 = ExampleLogBackend1(label: "FirstBackend")
51+
let backend2 = ExampleLogBackend2(label: "SecondBackend")
52+
53+
LoggingSystem.bootstrap(backend1, backend2)
54+
55+
```
56+
57+
## License
58+
59+
This library is released under the MIT license. See [LICENSE](LICENSE.md) for details.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Logging
2+
3+
class BaseMockBackend: LogHandler {
4+
static var didCalledInitWithLabel: String? = nil
5+
6+
subscript(metadataKey key: String) -> Logger.Metadata.Value? {
7+
get { self.metadata[key] }
8+
set { self.metadata[key] = newValue }
9+
}
10+
11+
var metadata: Logger.Metadata = .init()
12+
var logLevel: Logger.Level = .info
13+
var label: String
14+
15+
init(label: String) {
16+
self.label = label
17+
Self.didCalledInitWithLabel = label
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import XCTest
2+
import Logging
3+
4+
@testable import swift_log_variadic_bootstrap
5+
6+
class MockBackendOne: BaseMockBackend {}
7+
class MockBackendTwo: BaseMockBackend {}
8+
9+
final class VariadicBootstrapTests: XCTestCase {
10+
func test_Bootstrap() throws {
11+
LoggingSystem.bootstrap(MockBackendOne.init, MockBackendTwo.init)
12+
13+
let _ = Logger(label: "Testing")
14+
15+
XCTAssertEqual(MockBackendOne.didCalledInitWithLabel, "Testing")
16+
XCTAssertEqual(MockBackendTwo.didCalledInitWithLabel, "Testing")
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Logging
2+
3+
extension LoggingSystem {
4+
public static func bootstrap(_ handlers: (String) -> LogHandler...) {
5+
bootstrap { label in
6+
MultiplexLogHandler(handlers.map({ $0(label) }))
7+
}
8+
}
9+
10+
public static func bootstrap(_ handlers: LogHandler...) {
11+
bootstrap { _ in
12+
MultiplexLogHandler(handlers)
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)