Library provides additional LogHandlers for Swift Loggig system. Log hander can write information into file and to Sentry.
For FileLogger
you can chose one of following formatters:
- SingleLineFormatter - writes each log as single plain line in file,
- JsonFormatter - writes each log as JSON data.
You need to add library to Package.swift
file:
- add package to dependencies:
.package(url: "https://github.com/Mikroservices/ExtendedLogging.git", from: "2.0.0")
- and add product to your target:
.target(name: "App", dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "ExtendedLogging", package: "ExtendedLogging")
])
Then you can add log hander to Logging system:
LoggingSystem.bootstrap { label -> LogHandler in
FileLogger(label: label, path: "tests01.log", level: .debug)
}
Also you can combine multiple LogHandlers:
LoggingSystem.bootstrap { label -> LogHandler in
MultiplexLogHandler([
ConsoleLogger(label: label, console: Terminal(), level: .debug),
FileLogger(label: label,
path: "Logs/emails.log",
level: .debug,
logFormatter: JsonFormatter(),
rollingInterval: .month,
fileSizeLimitBytes: 10485760
),
SentryLogger(label: label,
dsn: Environment.get("SENTRY_DSN"),
level: Logger.Level.error)
])
}
And now you can use stadard logging system to log information:
let logger = Logger(label: "mikroservices.mczachurski.dev")
logger.info("Hello World!")
Some frameworks creates Logger
for you. For example in Vapor
you should initialize LoggingSystem
at the top of main.swift
file and use it in your application like this:
request.logger.info("Hello World!")
Download the source code and run in command line:
$ git clone https://github.com/Mikroservices/ExtendedLogging.git
$ swift package update
$ swift build
Run the following command if you want to open project in Xcode:
$ open Package.swift
You can fork and clone repository. Do your changes and pull a request.