mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +02:00
Added Logger and LogStringConvertible protocols
This commit is contained in:
parent
ce402d769c
commit
b65fbcee40
3 changed files with 92 additions and 1 deletions
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B3D2122AA34008E4117 /* Logger.swift */; };
|
2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B3D2122AA34008E4117 /* Logger.swift */; };
|
||||||
|
2EBF4B452122ACD6008E4117 /* LogStringConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B442122ACD6008E4117 /* LogStringConvertible.swift */; };
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXCopyFilesBuildPhase section */
|
/* Begin PBXCopyFilesBuildPhase section */
|
||||||
|
@ -25,6 +26,7 @@
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
2EBF4B3A2122AA34008E4117 /* libLogger.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libLogger.a; sourceTree = BUILT_PRODUCTS_DIR; };
|
2EBF4B3A2122AA34008E4117 /* libLogger.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libLogger.a; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
2EBF4B3D2122AA34008E4117 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
|
2EBF4B3D2122AA34008E4117 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
|
||||||
|
2EBF4B442122ACD6008E4117 /* LogStringConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LogStringConvertible.swift; sourceTree = "<group>"; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
|
@ -57,6 +59,7 @@
|
||||||
2EBF4B3C2122AA34008E4117 /* Logger */ = {
|
2EBF4B3C2122AA34008E4117 /* Logger */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
2EBF4B442122ACD6008E4117 /* LogStringConvertible.swift */,
|
||||||
2EBF4B3D2122AA34008E4117 /* Logger.swift */,
|
2EBF4B3D2122AA34008E4117 /* Logger.swift */,
|
||||||
);
|
);
|
||||||
path = Logger;
|
path = Logger;
|
||||||
|
@ -120,6 +123,7 @@
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */,
|
2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */,
|
||||||
|
2EBF4B452122ACD6008E4117 /* LogStringConvertible.swift in Sources */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
|
|
37
Logger/LogStringConvertible.swift
Normal file
37
Logger/LogStringConvertible.swift
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2018 Wojciech Nagrodzki
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
public protocol LogStringConvertible {
|
||||||
|
var logDescription: String { get }
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Array: LogStringConvertible where Element: LogStringConvertible {
|
||||||
|
public var logDescription: String {
|
||||||
|
let descriptions = map { $0.logDescription }
|
||||||
|
let joinedDescriptions = descriptions.joined(separator: ", ")
|
||||||
|
return "[" + joinedDescriptions + "]"
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,56 @@
|
||||||
// SOFTWARE.
|
// SOFTWARE.
|
||||||
//
|
//
|
||||||
|
|
||||||
class Logger {
|
import Foundation
|
||||||
|
|
||||||
|
public protocol Logger {
|
||||||
|
func log(time: String, level: LogLevel, location: String, object: String)
|
||||||
|
|
||||||
|
func description(for date: Date) -> String
|
||||||
|
func description(for file: String, line: Int, function: String) -> String
|
||||||
|
func description(for object: Any) -> String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum LogLevel: String {
|
||||||
|
case `default` = "Default"
|
||||||
|
case info = "Info"
|
||||||
|
case debug = "Debug"
|
||||||
|
case error = "Error"
|
||||||
|
case fault = "Fault"
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Logger {
|
||||||
|
public func log(_ object: Any, level: LogLevel = .default, file: String = #file, line: Int = #line, function: String = #function) {
|
||||||
|
let now = Date()
|
||||||
|
let time = description(for: now)
|
||||||
|
let location = description(for: file, line: line, function: function)
|
||||||
|
let objectDescription = description(for: object)
|
||||||
|
log(time: time, level: level, location: location, object: objectDescription)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func description(for date: Date) -> String {
|
||||||
|
return dateFormatter.string(from: date)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func description(for file: String, line: Int, function: String) -> String {
|
||||||
|
return filename(fromFilePath: file) + ":\(line) \(function)"
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return URL(fileURLWithPath: path).deletingPathExtension().lastPathComponent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private let dateFormatter: DateFormatter = {
|
||||||
|
let formatter = DateFormatter()
|
||||||
|
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
|
||||||
|
formatter.timeZone = TimeZone(secondsFromGMT: 0)
|
||||||
|
return formatter
|
||||||
|
}()
|
||||||
|
|
Loading…
Add table
Reference in a new issue