Wrapper for SwiftyBeaver
The T21Logger class is a simple wrapper class for the SwiftyBeaver public third party logger library.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
T21Logger is available through Carthage, CocoaPods or Swift Package Manager.
To install T21Logger with Carthage, add the following line to your Cartfile
.
github "worldline-spain/T21LoggerSwift"
Then run carthage update --no-use-binaries
command or just carthage update
. For details of the installation and usage of Carthage, visit its project page.
To install T21Logger with CocoaPods, add the following lines to your Podfile
.
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
pod 'T21LoggerSwift'
Then run pod install
command. For details of the installation and usage of CocoaPods, visit its official website.
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift
.
.package(url: "https://github.com/worldline-spain/T21LoggerSwift.git", .upToNextMajor(from: "2.1.0"))
For details of the installation and usage of Swift Package Manager, visit its official website.
The logger wrapper provides an easy way to add a Context related String for the different logs. In the following example we are creating 3 different loggers:
- To log general messages from the APP.
- To log messages from Realm related queries and stuff.
- To log messages from NSURLSessions related requests and so on.
Creating a Logger is just as simple as creating a T21Logger instance.
let customLogger = T21Logger()
let customLoggerRealm = T21Logger("REALM")
let customLoggerHTTPRequester = T21Logger("NETWORK")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
customLogger.verbose("App verbose message")
customLogger.debug("App verbose message")
customLoggerRealm.info("Realm error message")
customLoggerHTTPRequester.warning("Network warning message")
customLoggerRealm.error("Realm error message")
return true
}
The generated output is the following one:
💭18:21:04.541 VERBOSE AppDelegate.application():22 - [APP] App verbose message
✅18:21:04.545 DEBUG AppDelegate.application():23 - [APP] App verbose message
ℹ️18:21:04.545 INFO AppDelegate.application():24 - [REALM] Realm error message
⚠️18:21:04.546 WARNING AppDelegate.application():25 - [NETWORK] Network warning message
🚫18:21:04.546 ERROR AppDelegate.application():26 - [REALM] Realm error message
This makes trivial to keep different loggers for your app and your internal libraries. Each one will create its logger, making it easy to see from where the log comes from.
The following AppLogger.swift class it's just a set of static methods to avoid having to work with T21Logger instances.
import Foundation
public class AppLogger {
//MARK: Public methods
public class func verbose(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
sharedInstance.verbose(message,file,function, line)
}
public class func debug(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
sharedInstance.debug(message,file,function, line)
}
public class func info(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
sharedInstance.info(message,file,function, line)
}
public class func warning(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
sharedInstance.warning(message,file,function, line)
}
public class func error(_ message: String?, _ file: String = #file, _ function: String = #function, line: Int = #line) {
sharedInstance.error(message,file,function, line)
}
//MARK: Private methods
private static let sharedInstance = T21Logger("APP")
}
Here the usage:
AppLogger.verbose("Verbose message!")
AppLogger.error("Error message!")
💭18:26:04.545 VERBOSE AppDelegate.application():22 - [APP] Verbose message!
🚫18:26:04.546 ERROR AppDelegate.application():26 - [APP] Error message!
When using Cocoapods dependency manager the auto generaed project doesn't add DEBUG flags for the pod dependencies. As T21LoggerSwift works different when compiled with DEBUG options you may add the following post-install script in your Podfile.
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if config.name == 'Debug'
#in case of using Objective-C
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']
#in case of using Swift
config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['$(inherited)','-DDEBUG']
end
end
end
end
- SwiftyBeaver - Convenient logging during development & release in Swift.
- Eloi Guzman Ceron - Initial work
- Patricia De la Rica - Carthage integration
- Marcos Molero - Carthage integration
This project is licensed under the MIT License - see the LICENSE.md file for details
- To Worldline iOS Dev Team.