-
Notifications
You must be signed in to change notification settings - Fork 3k
/
SwiftyBeaverWrapper.swift
66 lines (55 loc) · 2.47 KB
/
SwiftyBeaverWrapper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import Foundation
import SwiftyBeaver
// MARK: - SwiftyBeaverWrapper
protocol SwiftyBeaverWrapper {
static func info(_ message: @autoclosure () -> Any,
_ file: String,
_ function: String,
line: Int,
context: Any?)
static func warning(_ message: @autoclosure () -> Any,
_ file: String,
_ function: String,
line: Int,
context: Any?)
static func error(_ message: @autoclosure () -> Any,
_ file: String,
_ function: String,
line: Int,
context: Any?)
}
extension SwiftyBeaver: SwiftyBeaverWrapper {}
// MARK: - SwiftyBeaverBuilder
protocol SwiftyBeaverBuilder {
func setup(with destination: URL?) -> SwiftyBeaverWrapper.Type
}
struct DefaultSwiftyBeaverBuilder: SwiftyBeaverBuilder {
// Format has full date/time, colored log level, tag, file name and message
// https://docs.swiftybeaver.com/article/20-custom-format
private let defaultFormat = "$Dyyyy-MM-dd HH:mm:ss.SSS$d $C$L$c [$X] $N - $M"
/// Setup SwiftyBeaver as our basic logger for console and file destination.
///
/// Note that filters can be added here on the different destinations like the following:
/// `console.addFilter(Filters.Path.contains("BrowserViewController", minLevel: .debug))`
/// `console.addFilter(Filters.Function.contains("viewDidLoad", required: true))`
/// `console.addFilter(Filters.Path.excludes("Sync", required: true))`
/// `console.addFilter(Filters.Message.contains("HTTP", caseSensitive: true, required: true))`
func setup(with destination: URL?) -> SwiftyBeaverWrapper.Type {
let console = ConsoleDestination()
console.format = defaultFormat
console.minLevel = .info
console.levelString.error = "FATAL"
let file = FileDestination(logFileURL: destination)
file.format = defaultFormat
file.minLevel = .info
file.levelString.error = "FATAL"
let logger = SwiftyBeaver.self
logger.removeAllDestinations()
logger.addDestination(console)
logger.addDestination(file)
return logger
}
}