SwiftSyslog is a framework that helps you to send logs to remote Syslog server securely. This framework using parts from an awesome Swift Puree log aggregator.
Key features
- TLS secured transport.
- API key-based authorization.
- Automatically append metadata about your application.
- The log output is buffered. Logs are stored in the buffer until it's time to send them.
- If a connection error occurred, the transmission automatically retries.
github "pykaso/SwiftSyslog"
dependencies: [
.package(url: "https://github.com/pykaso/SwiftSyslog", from: "1.0")
]
Initialize the Syslogger
object.
let syslogger = Syslogger(host: "syslog-ng.ho.st", port: 6514, useTLS: true, apiKey: "valid-token")
Optionally, set detailed info about your app user.
syslogger.userInfo = "myAppUserID"
And then, just write some log messages.
syslogger.write(.debug, message: "Hello captain!")
The outgoing message format is standard Rfc5424 message. As described here the message is composed of three parts. The Header and structured data part will be filled automatically by Syslogger. The last part named message
is the message you send.
- header (priority, version, timestamp, host, application, pid, message id)
- structured data - section with square brackets
- message
<priority>VERSION ISOTIMESTAMP HOSTNAME APPLICATION PID MESSAGEID [STRUCTURED-DATA] MESSAGE
Any syslog server configured to receive messages in the Rfc5424 format should accept messages from this framework. If your company using Splunk, you could use already configured Syslog Splunk Server. If not, look at this syslog-ng configuration file to inspire yourself.
Message sent by our sample above will look like this:
<13>1 2020-04-15T12:25:07.874000+02:00 syslogger-ios;0.1 cz.zonky.ios.benxy.dev/0.1.1100 - - [meta token="valid-token" device="Simulator_iPhone_8" install="4F8A6CE8-4A33-47BC-9467-3E5F57F80C0B"] Hello captain!
<priority> - <13>
VERSION - 1
ISOTIMESTAMP - 2020-04-15T12:25:07.874000+02:00
HOSTNAME - syslogger-ios;1.0 # sdk identification
APPLICATION - cz.zonky.ios.benxy.dev/0.1.1100 # your app bundle id / version
PID - -
MESSAGEID - -
STRUCTURED-DATA
- token="valid-token" # api key
- device="Simulator_iPhone_8" # device model name
- install="4F8A6CE8-4A33-47BC-9467-3E5F57F80C0B" # unique installation ID
MESSAGE - Hello captain!