Skip to content

Commit

Permalink
Current date and time added on stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSiddhant committed Jan 5, 2024
1 parent c0f5375 commit 31d61d7
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions Sources/SwiftLogger/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import Foundation
enum Log {
enum LogLevel {
case info,
warning,
error
warning,
error

fileprivate var prefix: String {
switch self {
case .info:
return "INFO"
return "ℹ️"
case .warning:
return "WARN ⚠️"
return "⚠️"
case .error:
return "ALERT "
return ""
}
}
}
Expand All @@ -31,15 +31,24 @@ enum Log {
let line: Int

var description: String {
return "\((file as NSString).lastPathComponent):\(line) \(function)"
return "[\((file as NSString).lastPathComponent):\(line) \(function)] "
}
}

static fileprivate var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
formatter.locale = Locale.current
formatter.timeZone = TimeZone.current
return formatter
}

fileprivate static func handleLog(level: LogLevel, str: String, shouldLogContext: Bool, context: Context) {
var logComponents = ["[\(level.prefix)]", " -> \(str)"]
var logComponents = [Date().toString(), "[\(level.prefix)]"]
if shouldLogContext {
logComponents.insert("\(context.description)", at: 1)
}
logComponents.append(" -> \(str)")

#if DEBUG
print(logComponents.joined(separator: " "))
Expand All @@ -51,3 +60,9 @@ enum Log {
Log.handleLog(level: logLevel, str: str.description, shouldLogContext: shouldLogContext, context: context)
}
}

internal extension Date {
func toString() -> String {
return Log.dateFormatter.string(from: self as Date)
}
}

0 comments on commit 31d61d7

Please sign in to comment.