SwiftLogger is a pure Swift library designed for logging in iOS, macOS, and other Apple platforms.
SwiftLogger provides a simple, efficient logging solution for Swift applications. It offers configurable log levels, easy integration, and comprehensive debugging information to help developers track application behavior and troubleshoot issues effectively.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
- File > Swift Packages > Add Package Dependency
- Add
https://github.com/sahibhussain/SwiftLogger.git
- Select "Up to Next Major" with "1.0.0"
Once you have your Swift package set up, adding SwiftLogger as a dependency is as easy as adding it to the dependencies
value of your Package.swift
or the Package list in Xcode.
dependencies: [
.package(url: "https://github.com/sahibhussain/SwiftLogger.git", .upToNextMajor(from: "1.0.0"))
]
Normally you'll want to depend on the SwiftLogger
target:
.product(name: "SwiftLogger", package: "SwiftLogger")
SwiftLogger supports the following log levels:
info
- General information messagesdebug
- Detailed debugging informationwarning
- Warning messages for potential issueserror
- Error messages for actual problems
The values are Int
types in incremental order of the above levels.
You can choose to omit logs up to a certain level. For example:
// If you want to log only warnings and errors
SwiftLogger.shared.logLevel = .warning
Each logging function accepts the following parameters:
message: String
: A message that you want to logproperties: [String: Any]?
: All the properties that you want to printfileName: String
: Name of the file from which the log was initiatedline: Int
: Line number of the code where the log was initiatedcolumn: Int
: Column number of the code where the log was initiatedfunctionName: String
: Name of the function where the log was initiatedloggerCategory: String
: Category of the log
SwiftLogger.shared.info("message", properties: nil, fileName: #file, line: #line, column: #column, functionName: #function, loggerCategory: "Default")
SwiftLogger.shared.debug("message", properties: nil, fileName: #file, line: #line, column: #column, functionName: #function, loggerCategory: "Default")
SwiftLogger.shared.warning("message", properties: nil, fileName: #file, line: #line, column: #column, functionName: #function, loggerCategory: "Default")
SwiftLogger.shared.error("message", properties: nil, fileName: #file, line: #line, column: #column, functionName: #function, loggerCategory: "Default")
All the logs above the log level provided will get saved in cachesDirectory
and hence can be retrieved for further analysis. This is particularly helpful in a production environment where you can't debug your app directly.
Follow and contact me on X (Twitter). If you find an issue, open a ticket.