//create sink
let sink = StandardSink.out()
//create formation
let formation = Formation.standard
//create log
var logger = Logger(label: "LogName") {
Handler(name: $0, sink: sink, formation: formation, logLevel: .trace)
}
// do some logs
logger.trace("This is a trace message")
logger.debug("This is a debug message")
logger.info("This is a info message")
logger.notice("This is a notice message")
logger.warning("This is a warning message")
logger.error("This is a error message")
logger.critical("This is a critical message")
Terminal out :
Change LogLevel
logger.logLevel = .info
logger.debug("will not log")
logger.info("will log")
Add MetaData
logger[metadataKey: "UserID"] = .stringConvertible(9527)
logger.info("message with logger meta data")
logger.info("message with both logger and message meta data", metadata: ["UserName": .string("L1MeN9Yu")])
let sink = StandardSink.out()
let formation = Formation.standard
var logger = Logger(label: "LogName") {
Handler(name: $0, sink: sink, formation: formation, logLevel: .trace)
}
logger[metadataKey: "UserID"] = .stringConvertible(9527)
#if DEBUG
let privacy = Privacy.public
#else
let privacy = Privacy.private
#endif
// default is private
logger.senna.notice("the user name is \("L1MeN9Yu")")
logger.senna.notice("the user name is \("L1MeN9Yu", privacy: privacy)")
Senna is available under the MIT license. See the LICENSE file for details.
Thanks | 鸣谢
Thanks to JetBrains for “Licenses for Open Source Development”. JetBrains supports non-commercial open source projects by providing core project contributors with a set of best-in-class
developer tools free of charge.
Senna
a logging engine backend of Apple’s swift-log
Status
Features
apple/swift-log
[stdout/stderr,file,oslog,syslog]
Examples
Use Builtin Sinks
Simple stdout
Terminal out :
Change LogLevel
Add MetaData
Terminal out :
Builtin Sinks
FileSink
Log message will write to file.
See Tests for more.
OSLogSink (Apple platform only)
Log message will write to OS log (Apple’s syslog). Use
Connsole.app
in your macOS to watch oslog messages.See Tests for more.
SystemLogSink (Linux)
Log message will write to syslog.
See Tests for more.
Formation
Formation
generate the log message.There are some builtin
Formation
:Formation.standard
. This is default forstdout/stderr
.Formation.standardXcode
. This is default forstdout/stderr
display in Xcode due Xcode’s console is not support ANSI escape code.Formation.file
. This is default forFile
.Formation.os
. This is default forOSLog
.Formation.system
. This is default forsyslog
.Advanced Customize Formation
Create
Formation
is simple :Formation(components: <#T##[Component]##[Senna.Component]#>, printer: <#T##Printable?##Senna.Printable?#>, separator: <#T##String?##Swift.String?#>)
The Formation contains
components: [Component]
,printer: Printable?
andseparator: String?
.Component
Component is your log message’s element. See Component file.
Printable
The
Printable
enhanced your log messages, It can add color or style to the component.The builtin
Printer
has two default instance.Printer.standard
forstdout/stderr
.Printer.xcode
forstdout/stderr
in Xcode.You can create new
Printer
instance or UseYourPrinter
which implementsPrintable
protocol.See Printer and Printable for more.
Separator
The
Separator
string is used for split log message’s component. TheFormation.standard
‘s separator is" ▶ "
.Apple’s Unified Logging System
Senna reimplement the string interpolation behavior of Apple’s Unified Logging System
Terminal out :
see LoggerSennaTests.swift for more usages.
Installation
Swift Package Manager
Add the following to your
Package.swift
file:Credit
License
Senna is available under the MIT license. See the LICENSE file for details.
Thanks | 鸣谢
Thanks to JetBrains for “Licenses for Open Source Development”. JetBrains supports non-commercial open source projects by providing core project contributors with a set of best-in-class developer tools free of charge.
感谢 JetBrains 提供的开源开发许可证。JetBrains 通过为项目核心开发者免费提供开发工具来支持非商业开源项目。