Skip to content

Commit 8d98c14

Browse files
committed
Initial version with Slack and Mattermost support
1 parent a56e85f commit 8d98c14

File tree

9 files changed

+823
-0
lines changed

9 files changed

+823
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
9+
.claude
10+
Package.resolved
11+
CLAUDE.md

Package.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// swift-tools-version: 6.2
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "WebhookLogging",
7+
platforms: [
8+
.macOS(.v10_15),
9+
.iOS(.v13),
10+
.tvOS(.v13),
11+
.watchOS(.v6)
12+
],
13+
products: [
14+
.library(
15+
name: "WebhookLogging",
16+
targets: ["WebhookLogging"]
17+
)
18+
],
19+
dependencies: [
20+
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0")
21+
],
22+
targets: [
23+
.target(
24+
name: "WebhookLogging",
25+
dependencies: [
26+
.product(name: "Logging", package: "swift-log")
27+
]
28+
),
29+
.testTarget(
30+
name: "WebhookLoggingTests",
31+
dependencies: ["WebhookLogging"]
32+
)
33+
]
34+
)

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# WebhookLogging
2+
3+
A Swift-Log API extension that adds webhook logging capabilities to any regular swift-log Logger instance. Currently comes with Slack and Mattermost webhook integration.
4+
5+
## Features
6+
7+
- **Works with any swift-log Logger**: No need to replace your existing Logger instances
8+
- **User-defined namespaces**: Create your own webhook namespaces
9+
- **Dual logging**: Messages go to both regular swift-log handlers AND webhooks when webhook parameter is provided
10+
- Built-in Slack and Mattermost webhook support with rich formatting
11+
- Configurable webhook endpoints with custom timeouts
12+
- All standard log levels supported (trace, debug, info, notice, warning, error, critical)
13+
- Extensibility for custom webhook providers
14+
15+
## Installation
16+
17+
Add the following to your `Package.swift`:
18+
19+
```swift
20+
dependencies: [
21+
.package(url: "https://github.com/diegotl/swift-log-webhook.git", from: "1.0.0")
22+
]
23+
```
24+
25+
## Usage
26+
27+
### Basic Setup
28+
29+
Define your custom webhook namespace and use regular swift-log Logger instances:
30+
31+
```swift
32+
import Logging
33+
import WebhookLogging
34+
35+
// Define your own webhook namespace
36+
enum MyWebhooks {
37+
static let appLogs = SlackWebhook(url: "https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK")
38+
static let criticalLogs = MattermostWebhook(url: "https://example.com/hooks/YOURWEBHOOK")
39+
}
40+
41+
// Create regular swift-log Logger
42+
let logger = Logger(label: "com.yourapp.logger")
43+
```
44+
45+
### Logging with Webhook Parameter
46+
47+
All log methods accept an additional `webhook` parameter using your defined webhooks:
48+
49+
```swift
50+
// Basic logging with your custom namespace
51+
logger.info("Application started", webhook: MyWebhooks.appLogs)
52+
logger.warning("Low memory warning", webhook: MyWebhooks.criticalLogs)
53+
logger.error("Failed to connect to database", webhook: MyWebhooks.criticalLogs)
54+
55+
// With metadata
56+
logger.error(
57+
"User authentication failed",
58+
metadata: ["userId": "12345", "ip": "192.168.1.1"],
59+
webhook: MyWebhooks.slack
60+
)
61+
```
62+
63+
### Creating Custom Providers
64+
65+
To add support for other webhook services, implement the `Webhook` protocol:
66+
67+
```swift
68+
public struct DiscordWebhook: Webhook {
69+
...
70+
}
71+
72+
// Add your custom providers to your namespace
73+
enum MyWebhooks {
74+
static let discord = DiscordWebhookConfiguration(url: "https://discord.com/api/webhooks/...")
75+
}
76+
```
77+
78+
## License
79+
80+
MIT License
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Foundation
2+
import Logging
3+
4+
public protocol Webhook: Sendable {
5+
var url: String { get }
6+
var headers: [String: String] { get }
7+
var timeout: TimeInterval { get }
8+
9+
func createPayload(
10+
level: Logger.Level,
11+
message: Logger.Message,
12+
metadata: Logger.Metadata,
13+
source: String,
14+
file: String,
15+
function: String,
16+
line: UInt
17+
) -> [String: Any]
18+
}

0 commit comments

Comments
 (0)