mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +02:00
Merge branch 'feature/message_as_string' into develop
This commit is contained in:
commit
0429d58fdc
5 changed files with 12 additions and 34 deletions
|
@ -35,8 +35,8 @@ public protocol Logger {
|
||||||
/// - time: The date log method was called.
|
/// - time: The date log method was called.
|
||||||
/// - level: The log level.
|
/// - level: The log level.
|
||||||
/// - location: Part of the log message provided by `description(for file: String, line: Int, function: String)` method.
|
/// - location: Part of the log message provided by `description(for file: String, line: Int, function: String)` method.
|
||||||
/// - object: Part of the log message provided by `description(for object: Any)` method.
|
/// - message: The message to be logged.
|
||||||
func log(time: Date, level: LogLevel, location: String, object: String)
|
func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String)
|
||||||
|
|
||||||
/// Transforms passed parameters into location textual representation which will be a part of the log message.
|
/// Transforms passed parameters into location textual representation which will be a part of the log message.
|
||||||
///
|
///
|
||||||
|
@ -47,14 +47,6 @@ public protocol Logger {
|
||||||
/// - line: The line number log method was called at.
|
/// - line: The line number log method was called at.
|
||||||
/// - function: The name of the declaration log method was called within.
|
/// - function: The name of the declaration log method was called within.
|
||||||
func description(for file: String, line: Int, function: String) -> String
|
func description(for file: String, line: Int, function: String) -> String
|
||||||
|
|
||||||
/// Transforms `object` into textual representation which will be a part of the log message.
|
|
||||||
///
|
|
||||||
/// Customization point. Default implementation returns `logDescription` for objects implementing `LogStringConvertible`,
|
|
||||||
/// or `String(describing:object)` otherwise.
|
|
||||||
///
|
|
||||||
/// - Parameter object: The object to be logged.
|
|
||||||
func description(for object: Any) -> String
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Log level controls the conditions under which a message should be logged.
|
/// Log level controls the conditions under which a message should be logged.
|
||||||
|
@ -107,16 +99,15 @@ extension Logger {
|
||||||
/// Sends object to the logging system, optionally specifying a custom log level.
|
/// Sends object to the logging system, optionally specifying a custom log level.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - object: The object to be logged.
|
/// - message: The message to be logged.
|
||||||
/// - level: The log level.
|
/// - level: The log level.
|
||||||
/// - file: **Do not provide a custom value.** The path to the file log method is called from.
|
/// - file: **Do not provide a custom value.** The path to the file log method is called from.
|
||||||
/// - line: **Do not provide a custom value.** The line number log method is called at.
|
/// - line: **Do not provide a custom value.** The line number log method is called at.
|
||||||
/// - function: **Do not provide a custom value.** The name of the declaration log method is called within.
|
/// - function: **Do not provide a custom value.** The name of the declaration log method is called within.
|
||||||
public func log(_ object: Any, level: LogLevel, file: String = #file, line: Int = #line, function: String = #function) {
|
public func log(_ message: @autoclosure () -> String, level: LogLevel, file: String = #file, line: Int = #line, function: String = #function) {
|
||||||
let now = Date()
|
let now = Date()
|
||||||
let location = description(for: file, line: line, function: function)
|
let location = description(for: file, line: line, function: function)
|
||||||
let objectDescription = description(for: object)
|
log(time: now, level: level, location: location, message: message)
|
||||||
log(time: now, level: level, location: location, object: objectDescription)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns location in format `"<file name>:<line> <function>"`.
|
/// Returns location in format `"<file name>:<line> <function>"`.
|
||||||
|
@ -124,14 +115,6 @@ extension Logger {
|
||||||
return filename(fromFilePath: file) + ":\(line) \(function)"
|
return filename(fromFilePath: file) + ":\(line) \(function)"
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `logDescription` for objects implementing `LogStringConvertible`, or `String(describing:object)` otherwise.
|
|
||||||
public func description(for object: Any) -> String {
|
|
||||||
if let logStringConvertible = object as? LogStringConvertible {
|
|
||||||
return logStringConvertible.logDescription
|
|
||||||
}
|
|
||||||
return String(describing: object)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func filename(fromFilePath path: String) -> String {
|
private func filename(fromFilePath path: String) -> String {
|
||||||
return URL(fileURLWithPath: path).deletingPathExtension().lastPathComponent
|
return URL(fileURLWithPath: path).deletingPathExtension().lastPathComponent
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,9 +36,9 @@ public final class AgregateLogger: Logger {
|
||||||
self.loggers = loggers
|
self.loggers = loggers
|
||||||
}
|
}
|
||||||
|
|
||||||
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) {
|
||||||
for logger in self.loggers {
|
for logger in self.loggers {
|
||||||
logger.log(time: time, level: level, location: location, object: object)
|
logger.log(time: time, level: level, location: location, message: message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ public final class ConsoleLogger: Logger {
|
||||||
formatter.timeZone = TimeZone(secondsFromGMT: 0)
|
formatter.timeZone = TimeZone(secondsFromGMT: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) {
|
||||||
print(formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + object)
|
print(formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + message())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,8 +50,8 @@ public final class DiskLogger: Logger {
|
||||||
formatter.timeZone = TimeZone(secondsFromGMT: 0)
|
formatter.timeZone = TimeZone(secondsFromGMT: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) {
|
||||||
let message = formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + object + "\n"
|
let message = formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + message() + "\n"
|
||||||
log(message)
|
log(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ public class NullLogger: Logger {
|
||||||
public init() {
|
public init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) {
|
||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,9 +37,4 @@ public class NullLogger: Logger {
|
||||||
public func description(for file: String, line: Int, function: String) -> String {
|
public func description(for file: String, line: Int, function: String) -> String {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns empty string to minimize observer effect.
|
|
||||||
public func description(for object: Any) -> String {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue