Skip to content

Commit 31d61d7

Browse files
committed
Current date and time added on stmt
1 parent c0f5375 commit 31d61d7

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

Sources/SwiftLogger/Log.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import Foundation
1010
enum Log {
1111
enum LogLevel {
1212
case info,
13-
warning,
14-
error
13+
warning,
14+
error
1515

1616
fileprivate var prefix: String {
1717
switch self {
1818
case .info:
19-
return "INFO"
19+
return "ℹ️"
2020
case .warning:
21-
return "WARN ⚠️"
21+
return "⚠️"
2222
case .error:
23-
return "ALERT "
23+
return ""
2424
}
2525
}
2626
}
@@ -31,15 +31,24 @@ enum Log {
3131
let line: Int
3232

3333
var description: String {
34-
return "\((file as NSString).lastPathComponent):\(line) \(function)"
34+
return "[\((file as NSString).lastPathComponent):\(line) \(function)] "
3535
}
3636
}
3737

38+
static fileprivate var dateFormatter: DateFormatter {
39+
let formatter = DateFormatter()
40+
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
41+
formatter.locale = Locale.current
42+
formatter.timeZone = TimeZone.current
43+
return formatter
44+
}
45+
3846
fileprivate static func handleLog(level: LogLevel, str: String, shouldLogContext: Bool, context: Context) {
39-
var logComponents = ["[\(level.prefix)]", " -> \(str)"]
47+
var logComponents = [Date().toString(), "[\(level.prefix)]"]
4048
if shouldLogContext {
4149
logComponents.insert("\(context.description)", at: 1)
4250
}
51+
logComponents.append(" -> \(str)")
4352

4453
#if DEBUG
4554
print(logComponents.joined(separator: " "))
@@ -51,3 +60,9 @@ enum Log {
5160
Log.handleLog(level: logLevel, str: str.description, shouldLogContext: shouldLogContext, context: context)
5261
}
5362
}
63+
64+
internal extension Date {
65+
func toString() -> String {
66+
return Log.dateFormatter.string(from: self as Date)
67+
}
68+
}

0 commit comments

Comments
 (0)