JBLog
JBLog is a log output library for use in Swift project.
Using JBLog can easily log output like print()
or NSLog()
.
Can easily change the level of the log to be outpu by using the JBLog.Level
.
Also can easily change the output of additional information(date, file name, line of code, function name, etc).
Install
CocoaPods
Add the following items to the CocoaPods dependency management file(Podfile).
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
target '<YourProjectName>' do
pod 'JBLog', '~> 1.0.0'
end
If already managing dependencies using Podfile, only need to add the JBLog dependency.
pod 'JBLog', '~> 1.0.0'
If added the dependency to Podfile, install the JBLog library by running the following command.
$ pod install
For details of the installation and usage of CocoaPods, visit official web site.
Carthage
Add the following entry to the Carthage dependency management file(Cartfile).
github "bokjaeo/JBLog" ~> 1.0.0
If added the dependency to Cartfile, install the JBLog library by running the following command.
$ carthage update
For details of the installation and usage of Carthage, visit official web site.
Swift Package Manager
Add the following entry to the package dependency file(Package.swift).
.package(url: "https://github.com/bokjaeo/JBLog.git", from: "1.0.0")
If added the dependency to Package.swift, install the JBLog library by running the following command.
$ swift build
For details of the installation and usage of Swift Package Manager, visit official web site.
Basic usage
Add the JBLog module to the file that will use the log output.
import JBLog
Create the JBLog instance object and output the log.
let log = JBLog()
log.warn("Warning message")
Recommended method
It is recommended that you declare and use global constants for the JBLog instance object in the project's app delegate object(AppDelegate) or in the global file.
import UIKit
import JBLog
let log = JBLog()
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? ) -> Bool {
log.warn("Warning message")
someMethod()
return true
}
func someMethod() {
log.error("Error message")
}
//... The rest is omitted ...
}
When executing the above code, the following log is output to the console.
[Warning] > Warning message
[Error] > Error message
Log level
The output of the log message may vary depending on the setting of the JBLog instance object.
Whether or not to log is determined by the log level(JBLog.Level
).
Available log levels
Log level | Description |
---|---|
none |
No logs are output. |
error |
Only error logs output. |
warn |
Outputs error and warning logs. |
info |
Outputs error, warning, and information logs. |
debug |
Outputs error, warning, information, and debug logs. |
verbose |
Outputs error, warning, information, debug, and verbose logs. |
Setting the log level
The log level of the JBLog instance object can be set with the level
property.
Below is an example of setting all logs to be output.
let log = JBLog()
log.level = .verbose
Can set a different log level for each instance object.
let log1 = JBLog()
log1.level = .verbose
let log2 = JBLog()
log2.level = .warn
To set different log levels in the development environment and deployment environment, can set different log levels based on the DEBUG=1
macro in Preprocessor Macros
.
let log = JBLog()
#if DEBUG
log.level = .verbose
#else
log.level = .warn
#endif
If not specify the log level, it defaults to warn
.
Log output
The log output can be output using the log output methods of the JBLog instance object.
Error log output
The error log is always output if the log level is not none
.
let log = JBLog()
log.error("Error message")
Warning log ouput
The warning log is output when the log level is set to verbose
, debug
, info
, or warn
.
let log = JBLog()
log.warn("Warning message")
Information log output
The information log is output when the log level is set to verbose
, debug
, or info
.
let log = JBLog()
log.level = .info
log.info("Information message")
Debug log output
The debug log is output when the log level is set to verbose
or debug
.
let log = JBLog()
log.level = .debug
log.debug("Debug message")
Detail log output
The verbose log is output only when the log level is set to verbose
.
let log = JBLog()
log.level = .verbose
log.verbose("Detail message")