@@ -11,14 +11,23 @@ import JustLog
11
11
12
12
@UIApplicationMain
13
13
class AppDelegate : UIResponder , UIApplicationDelegate {
14
-
14
+
15
15
var window : UIWindow ?
16
16
private var sessionID = UUID ( ) . uuidString
17
-
17
+
18
18
func application( _ application: UIApplication , didFinishLaunchingWithOptions launchOptions: [ UIApplication . LaunchOptionsKey : Any ] ? ) -> Bool {
19
19
setupLogger ( )
20
20
return true
21
21
}
22
+
23
+ struct RegexListReponse : Codable {
24
+ var pattern : String
25
+ var minimumLogLevel : String
26
+ }
27
+
28
+ struct ExceptionListResponse : Codable {
29
+ var value : String
30
+ }
22
31
23
32
func applicationDidEnterBackground( _ application: UIApplication ) {
24
33
forceSendLogs ( application)
@@ -28,9 +37,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
28
37
forceSendLogs ( application)
29
38
}
30
39
31
- func redactValues( message: String , loggerExeptionList: [ String ] , matches: [ NSTextCheckingResult ] ) -> String {
40
+ func redactValues( message: String , loggerExeptionList: [ ExceptionListResponse ] , matches: [ NSTextCheckingResult ] ) -> String {
32
41
33
- var redactedLogMessage = logMessagePlaceholder
42
+ var redactedLogMessage = message
34
43
35
44
for match in matches. reversed ( ) {
36
45
let key = match. range ( at: 1 )
@@ -39,14 +48,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
39
48
let keyRange = Range ( key, in: redactedLogMessage) !
40
49
let valueRange = Range ( value, in: redactedLogMessage) !
41
50
42
- for listedException in loggerExeptionList {
43
- if listedException != redactedLogMessage [ valueRange] {
44
- redactedLogMessage. replaceSubrange ( keyRange, with: " **** " )
45
- redactedLogMessage. replaceSubrange ( valueRange, with: " **** " )
51
+ for exception in loggerExeptionList {
52
+ if exception. value == redactedLogMessage [ valueRange] {
53
+ return redactedLogMessage
46
54
}
47
55
}
56
+
57
+ var redactedKey = redactedLogMessage [ keyRange]
58
+ let redactedKeyStartIndex = redactedKey. index ( redactedKey. startIndex, offsetBy: 1 )
59
+ let redactedKeyEndIndex = redactedKey. index ( redactedKey. endIndex, offsetBy: - 1 )
60
+
61
+ redactedKey. replaceSubrange ( redactedKeyStartIndex..< redactedKeyEndIndex, with: " *** " )
62
+
63
+ var redactedValue = redactedLogMessage [ valueRange]
64
+ let valueReplacementStartIndex = redactedValue. startIndex
65
+ let valueReplacementEndIndex = redactedValue. endIndex
66
+
67
+ redactedValue. replaceSubrange ( valueReplacementStartIndex..< valueReplacementEndIndex, with: " ***** " )
68
+
69
+ redactedLogMessage. replaceSubrange ( valueRange, with: redactedValue)
70
+ redactedLogMessage. replaceSubrange ( keyRange, with: redactedKey)
71
+
48
72
return redactedLogMessage
49
73
}
74
+ return redactedLogMessage
50
75
}
51
76
52
77
private func forceSendLogs( _ application: UIApplication ) {
@@ -63,12 +88,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
63
88
identifier = UIBackgroundTaskIdentifier . invalid
64
89
}
65
90
}
66
-
67
- var regexRules = [ [ #"(name) = \\*"(.*?[^\\]+)"# , " function() " , " minimumLevel " ] ]
68
91
69
92
private func setupLogger( ) {
70
-
71
93
let logger = Logger . shared
94
+ let decoder = JSONDecoder ( )
72
95
73
96
// custom keys
74
97
logger. logTypeKey = " logtype "
@@ -84,27 +107,34 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
84
107
logger. logstashPort = 5052
85
108
logger. logstashTimeout = 5
86
109
logger. logLogstashSocketActivity = true
87
-
88
- logger. sanitizer = { message, type, regexRules, loggerExeptionList in
89
110
111
+
112
+ let regexRuleList = " [{ \" pattern \" : \" (name) = \\ \\ \\ \\ * \\ \" (.*?[^ \\ \\ \\ \\ ]+) \" , \" minimumLogLevel \" : \" warning \" }, { \" pattern \" : \" (token) = \\ \\ \\ \\ * \\ \" (.*?[^ \\ \\ \\ \\ ]+) \" , \" minimumLogLevel \" : \" warning \" }] " . data ( using: . utf8)
113
+ let sanitizationExeceptionList = " [{ \" value \" : \" Dan Jones \" }, { \" value \" : \" Jack Jones \" }] " . data ( using: . utf8)
114
+
115
+ logger. sanitizer = { message, type in
90
116
var sanitizedMessage = message
91
117
92
- for pattern in regexPattern {
93
- if let regex = try ? NSRegularExpression ( pattern: pattern , options: NSRegularExpression . Options. caseInsensitive) {
94
-
95
- let range = NSRange ( message. startIndex..< message. endIndex, in: message)
96
- let matches = regex. matches ( in: message, options: [ ] , range: range)
97
-
98
- sanitizedMessage = self . redactValues ( message: message, loggerExeptionList: [ " " ] , matches: matches)
99
- }
100
- }
118
+ guard let ruleList = try ? decoder. decode ( [ RegexListReponse ] . self, from: regexRuleList!) else { return " sanitizedMessage " }
119
+ guard let exceptionList = try ? decoder. decode ( [ ExceptionListResponse ] . self, from: sanitizationExeceptionList!) else { return " sanitizedMessage " }
101
120
121
+ for value in ruleList {
122
+ if ( value. minimumLogLevel >= type. rawValue) {
123
+ if let regex = try ? NSRegularExpression ( pattern: value. pattern , options: NSRegularExpression . Options. caseInsensitive) {
124
+
125
+ let range = NSRange ( sanitizedMessage. startIndex..< sanitizedMessage. endIndex, in: sanitizedMessage)
126
+ let matches = regex. matches ( in: sanitizedMessage, options: [ ] , range: range)
127
+
128
+ sanitizedMessage = self . redactValues ( message: sanitizedMessage, loggerExeptionList: exceptionList, matches: matches)
129
+ }
130
+ }
131
+ }
102
132
return sanitizedMessage
103
133
}
104
134
105
135
// logz.io support
106
136
//logger.logzioToken = <logzioToken>
107
-
137
+
108
138
// untrusted (self-signed) logstash server support
109
139
//logger.allowUntrustedServer = <Bool>
110
140
@@ -114,5 +144,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
114
144
" session " : sessionID]
115
145
logger. setup ( )
116
146
}
117
-
147
+
148
+
118
149
}
0 commit comments