|  | 
|  | 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 | 
0 commit comments