-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathLogger.swift
80 lines (63 loc) · 2.04 KB
/
Logger.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Logger.swift
// NoMAD
//
// Created by Joel Rennich on 9/6/16.
// Copyright © 2016 Orchard & Grove Inc. All rights reserved.
//
/// A singleton `Logger` instance for the app to use.
let myLogger = Logger()
import Foundation
import os.log
/// The individual logging levels to use when logging in NoMAD
///
/// - base: General errors
/// - info: Positive info
/// - notice: Nice to know issues that may, or may not, cause issues
/// - debug: Lots of verbose logging
enum LogLevel: Int {
/// General errors
case base = 0
/// Positive info
case info = 1
/// Nice to know issues that may, or may not, cause issues
case notice = 2
/// Lots of verbose logging
case debug = 3
}
var log: OSLog? {
if #available(OSX 10.12, *) {
return OSLog(subsystem: "menu.nomad.login.ad", category: "framework")
} else {
return nil
}
}
/// Simple class to handle logging levels. Use the `LogLevel` enum to specify the logging details.
class Logger {
/// Set to a level from `LogLevel` enum to control what gets logged.
var loglevel: LogLevel
/// Init method simply check to see if Verbose logging is enabled or not for the Logger object.
init() {
let defaults = UserDefaults.init(suiteName: "menu.nomad.login.ad")
if defaults?.bool(forKey: "Verbose") ?? false {
NSLog("Enaging verbose logging")
loglevel = .debug
} else {
loglevel = .base
}
}
/// Simple wrapper around NSLog to provide control of logging.
///
/// - Parameters:
/// - level: A value from `LogLevel` enum
/// - message: A `String` that describes the information to be logged
func logit(_ level: LogLevel, message: String) {
if (level.rawValue <= loglevel.rawValue) {
if #available(OSX 10.12, *) {
os_log("%{public}@", log: log!, type: .debug, message)
} else {
NSLog("level: \(level) - " + message)
}
}
}
}