Simple Logger(Console and file) for Swift
When things are simple and easy to use then everyone uses it.
When we think about logger then one think come to mind is "print" statement. It is very powerful. Let us see How?
Features -
1. You can different level for log DEBUG,INFO,ERROR,EXCEPTION,WARNING
2. You can configure it for DEBUG, RELEASE or any configurations
3. You can configure statements for console logging.
4. You can use file logger.
5. You can configure statements for file logger.
6. you can configure timestamp, filename, function name and line to be added or not
Why to use this logger? because you don't need to spend time to understand How to use it if you are aware of how to use "print"
How to use ?
print("One operation is completed.",logLevel:.DEBUG)
print("Oeration ",logLevel:.INFO)
print("Devide by zero error",logLevel:.ERROR)
print("Could not able to read the data.",logLevel:.EXCEPTION)
print("Only three items are left.",logLevel:.WARNING)
just use print statement with different log levels.
Print funcation is very powerful
You can pass any object to print not only strings.
let employee = ["name":"Alex","empId":123] as [String : Any]
print(employee,logLevel:.DEBUG)
print(10, logLevel:.WARNING)
then use that powerful feature with with this logger.
You can pass many arguments to print so use it with print.
print(employee,logLevel:.DEBUG)
print(10,logLevel:.WARNING)
print(employee,10,employee,55,logLevel:.DEBUG)
Console Output
Configure debug, info, error.. Statements
But now you might be thinking that all debug, info, error..etc are not needed in release, debug or any configuration. Then configure it as follow -
//
// YBLoggerConfiguration.swift
// SimpleLogger
//
// Created by Yogesh Bhople on 23/07/17.
import Foundation
extension YBLoggerConfiguration {
func allowToPrint() -> Bool {
var allowed = false
if let logLevel = self as? YBLogger {
#if DEBUG
allowed = true
#elseif RELEASE
switch logLevel {
case .DEBUG:
allowed = false
case .INFO:
allowed = false
case .ERROR:
allowed = true
case .EXCEPTION:
allowed = true
case .WARNING:
allowed = false
}
#endif
}
return allowed
}
func allowToLogWrite() -> Bool {
var allowed = false
if let logLevel = self as? YBLogger {
#if DEBUG
allowed = true
#elseif RELEASE
switch logLevel {
case .DEBUG:
allowed = false
case .INFO:
allowed = false
case .ERROR:
allowed = true
case .EXCEPTION:
allowed = true
case .WARNING:
allowed = true
}
#endif
}
return allowed
}
}
Configure timestamp, filename, function name and line to be added or not
extension YBLoggerConfiguration {
func addTimeStamp() -> Bool {return false}
func addFileName() -> Bool {return false}
func addFunctionName() -> Bool {return true}
func addLineNumber() -> Bool {return true}
}
Now let us see How we can use same logger for file logging.
Configure which statements should be added in file logging. And that’s it.
func allowToLogWrite() -> Bool {
var allowed = false
if let logLevel = self as? YBLogger {
#if DEBUG
allowed = true
#elseif RELEASE
switch logLevel {
case .DEBUG:
allowed = false
case .INFO:
allowed = false
case .ERROR:
allowed = true
case .EXCEPTION:
allowed = true
case .WARNING:
allowed = true
}
#endif
}
return allowed
}
The amazing feature is it will create separate log files for each day.
/Users/.../Library/Developer/CoreSimulator/Devices/3A6B7665-9838-4E89-87A2-6DAEBAE09861/data/Containers/Data/Application/B03E2ECA-1C18-4B78-A97D-CD01E0041A36/Documents/20170723YBLogger.txt
Find the full source code on github. https://github.com/yogeshbh/SimpleLogger
If you can add some more exciting features or any improvement, you are welcome. Please fill free to add in git repo.
Comments
Post a Comment