Log
is a powerful logging framework that provides built-in themes and formatters, and a nice API to define your owns.
Get the most out of
Log
by installingXcodeColors
andKZLinkedConsole
Usage • Installation • License
Usage
The basics
- Use
Log
just as you would useprint
.
let Log = Logger()
Log.trace("Called!!!")
Log.debug(["Who is self: ", self])
Log.info([one, two, three])
Log.warn([one, two, three], separator: " - ")
Log.error([error], terminator: "😱😱😱\n")
Log.fatal("Fail fast!") // @noreturn
- Disable
Log
by settingenabled
tofalse
:
Log.enabled = false
- Define a minimum level of severity to only print the messages with a greater or equal severity:
Log.level = .warning
The severity levels are
trace
,debug
,info
,warning
,error
andfatal
.
Customization
- Create your own
Logger
by specify itsTheme
and/orFormatter
.
let minimal = Logger(formatter: .minimal, theme: .solarized([.level, .message]))
let colorful = Logger(formatter: .basic, theme: .solarized(.all))
A suggested way of doing it is by extending Formatters
and Themes
:
extension Formatter {
static func named(name: String) -> Formatter {
return Formatter("[%@] %@ | %@ [\(name)] - %@", [
.date(format: "yyyy-MM-dd HH:mm:ss.SSS"),
.level(equalWidthByPrependingSpace),
.location,
.message
])
}
}
extension Theme {
static let tomorrowNight = Theme.new(
).foreground(
trace: 0xC5C8C6,
debug: 0x81A2BE,
info: 0xB5BD68,
warn: 0xF0C674,
error: 0xCC6666,
fatal: 0xFFFFFF
).background(
fatal: 0xCC6666
).components(
[.level, .message]
).build()
}
Then create a new logger.
let Log = Logger(formatter: .named("Named"), theme: .tomorrowNight)
Supported Formatter Components
-
.date(format: String)
-
.level(LevelFormatterOption)
Options: .none, .equalWidthByPrependingSpace, .equalWidthByAppendingSpace, .equalWidthByTruncatingTail(width: Int)
-
.file(fullPath: Bool, withExtension: Bool)
-
.line
-
.function
-
.location
-
.message
-
.custom(content: Void -> String)
Note: Theme.ComponentOptions
is an OptionSetType
with the same entries of above.
Supported Theme Builder Methods
foreground(trace: Color?, debug: Color?...) // Set the foreground colors.
background(trace: Color?, debug: Color?...) // Set the background colors.
components(Theme.ComponentOptions) // Set the components to colorize.
Note1: typealias Color = UInt32
Note2: Method Theme.new(trace: Color?, debug: Color?...)
creates a theme builder with specified foreground colors and a single colorizing component .level
See the built-in formatters and themes for more examples.
Tip: Logger.format
and Logger.colors
can be useful to visually debug your logger.
Nothing prevents you from creating as many loggers as you want!
let basic = Logger(formatter: .concise, theme: nil)
let minimal = Logger(level: .info, formatter: .minimal, theme: .flat)
- Turn off the colors by setting the theme to
nil
:
Log.theme = nil
Installation
Carthage
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Log into your Xcode project using Carthage, specify it in your Cartfile
:
github "kAzec/Log"
License
Copyright (c) 2015-2016 Damien (http://delba.io) and kAzec
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.