XCGLogger
A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Install / Use
/learn @DaveWoodCom/XCGLoggerREADME
![XCGLogger][xcglogger-logo]
[![badge-language]][swift.org] [![badge-platforms]][swift.org] [![badge-license]][license]
[![badge-swiftpm]][swiftpm] [![badge-cocoapods]][cocoapods-xcglogger] [![badge-carthage]][carthage]
[![badge-mastodon]][mastodon-davewoodx] [![badge-twitter]][twitter-davewoodx]
[![badge-sponsors]][cerebral-gardens] [![badge-patreon]][patreon-davewoodx]
tl;dr
XCGLogger is the original debug log module for use in Swift projects.
Swift does not include a C preprocessor so developers are unable to use the debug log #define macros they would use in Objective-C. This means our traditional way of generating nice debug logs no longer works. Resorting to just plain old print calls means you lose a lot of helpful information, or requires you to type a lot more code.
XCGLogger allows you to log details to the console (and optionally a file, or other custom destinations), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Go from this:
Simple message
to this:
2014-06-09 06:44:43.600 [Debug] [AppDelegate.swift:40] application(_:didFinishLaunchingWithOptions:): Simple message
Example
<img src="https://raw.githubusercontent.com/DaveWoodCom/XCGLogger/main/ReadMeImages/SampleLog.png" alt="Example" style="width: 690px;" />Communication (Hat Tip AlamoFire)
- If you need help, use [Stack Overflow][stackoverflow] (Tag '[xcglogger][stackoverflow]').
- If you'd like to ask a general question, use [Stack Overflow][stackoverflow].
- If you've found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
- If you use XCGLogger, please Star the project on [GitHub][github-xcglogger]
Installation
Git Submodule
Execute:
git submodule add https://github.com/DaveWoodCom/XCGLogger.git
in your repository folder.
[Carthage][carthage]
Add the following line to your Cartfile.
github "DaveWoodCom/XCGLogger" ~> 7.1.5
Then run carthage update --no-use-binaries or just carthage update. For details of the installation and usage of Carthage, visit [its project page][carthage].
Developers running 5.0 and above in Swift will need to add $(SRCROOT)/Carthage/Build/iOS/ObjcExceptionBridging.framework to their Input Files in the Copy Carthage Frameworks Build Phase.
[CocoaPods][cocoapods]
Add something similar to the following lines to your Podfile. You may need to adjust based on your platform, version/branch etc.
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
pod 'XCGLogger', '~> 7.1.5'
Specifying the pod XCGLogger on its own will include the core framework. We're starting to add subspecs to allow you to include optional components as well:
pod 'XCGLogger/UserInfoHelpers', '~> 7.1.5': Include some experimental code to help deal with using UserInfo dictionaries to tag log messages.
Then run pod install. For details of the installation and usage of CocoaPods, visit [its official web site][cocoapods].
Note: Before CocoaPods 1.4.0 it was not possible to use multiple pods with a mixture of Swift versions. You may need to ensure each pod is configured for the correct Swift version (check the targets in the pod project of your workspace). If you manually adjust the Swift version for a project, it'll reset the next time you run pod install. You can add a post_install hook into your podfile to automate setting the correct Swift versions. This is largely untested, and I'm not sure it's a good solution, but it seems to work:
post_install do |installer|
installer.pods_project.targets.each do |target|
if ['SomeTarget-iOS', 'SomeTarget-watchOS'].include? "#{target}"
print "Setting #{target}'s SWIFT_VERSION to 4.2\n"
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '4.2'
end
else
print "Setting #{target}'s SWIFT_VERSION to Undefined (Xcode will automatically resolve)\n"
target.build_configurations.each do |config|
config.build_settings.delete('SWIFT_VERSION')
end
end
end
print "Setting the default SWIFT_VERSION to 3.2\n"
installer.pods_project.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '3.2'
end
end
You can adjust that to suit your needs of course.
[Swift Package Manager][swiftpm]
Add the following entry to your package's dependencies:
.Package(url: "https://github.com/DaveWoodCom/XCGLogger.git", majorVersion: 7)
Backwards Compatibility
Use:
- XCGLogger version [7.1.5][xcglogger-7.1.5] for Swift 5.0
- XCGLogger version [6.1.0][xcglogger-6.1.0] for Swift 4.2
- XCGLogger version [6.0.4][xcglogger-6.0.4] for Swift 4.1
- XCGLogger version [6.0.2][xcglogger-6.0.2] for Swift 4.0
- XCGLogger version [5.0.5][xcglogger-5.0.5] for Swift 3.0-3.2
- XCGLogger version [3.6.0][xcglogger-3.6.0] for Swift 2.3
- XCGLogger version [3.5.3][xcglogger-3.5.3] for Swift 2.2
- XCGLogger version [3.2][xcglogger-3.2] for Swift 2.0-2.1
- XCGLogger version [2.x][xcglogger-2.x] for Swift 1.2
- XCGLogger version [1.x][xcglogger-1.x] for Swift 1.1 and below.
Basic Usage (Quick Start)
This quick start method is intended just to get you up and running with the logger. You should however use the advanced usage below to get the most out of this library.
Add the XCGLogger project as a subproject to your project, and add the appropriate library as a dependency of your target(s).
Under the General tab of your target, add XCGLogger.framework and ObjcExceptionBridging.framework to the Embedded Binaries section.
Then, in each source file:
import XCGLogger
In your AppDelegate (or other global file), declare a global constant to the default XCGLogger instance.
let log = XCGLogger.default
In the
application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) // iOS, tvOS
or
applicationDidFinishLaunching(_ notification: Notification) // macOS
function, configure the options you need:
log.setup(level: .debug, showThreadName: true, showLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: "path/to/file", fileLevel: .debug)
The value for writeToFile: can be a String or URL. If the file already exists, it will be cleared before we use it. Omit the parameter or set it to nil to log to the console only. You can optionally set a different log level for the file output using the fileLevel: parameter. Set it to nil or omit it to use the same log level as the console.
Then, whenever you'd like to log something, use one of the convenience methods:
log.verbose("A verbose message, usually useful when working on a specific problem")
log.debug("A debug message")
log.info("An info message, probably useful to power users looking in console.app")
log.notice("A notice message")
log.warning("A warning message, may indicate a possible error")
log.error("An error occurred, but it's recoverable, just info about what happened")
log.severe("A severe error occurred, we are likely about to crash now")
log.alert("An alert error occurred, a log destination could be made to email someone")
log.emergency("An emergency error occurred, a log destination could be made to text someone")
The different methods set the log level of the message. XCGLogger will only print messages with a log level that is greater to or equal to its current log level setting. So a logger with a level of .error will only output log messages with a level of .error, .severe, .alert, or .emergency.
Advanced Usage (Recommended)
XCGLogger aims to be simple to use and get you up and running quickly with as few as 2 lines of code above. But it allows for much greater control and flexibility.
A logger can be configured to deliver log messages to a variety of destinations. Using the basic setup above, the logger will output log messages to the standard Xcode debug console, and optionally a file if a path is provided. It's quite likely you'll want to send logs to more interesting places, such as the Apple System Console, a database, third party server, or another application such as [NSLogger][NSLogger]. This is accomplished by adding the destination to the logger.
Here's an example of configuring the logger to output to the Apple System Log as well as a file.
// Create a logger object with no destinations
let log = XCGLogger(identifier: "advancedLogger", includeDefaultDestinations: false)
// Create a destination for the system console log (via NSLog)
let systemDestination = AppleSystemLogDestination(identifier: "advancedLogger.systemDestination")
// Optionally set some configuration options
systemDestination.outputLevel = .debug
systemDestination.showLogIdentifier = false
systemDestination.showFunctionName = true
systemDestination.showThreadName = true
systemDestination.showLevel = true
systemDestination.showFileName = true
systemDestination.showLineNumber = true
systemDestination.showDate = true
// Add the destination to the logger
log.add(destination: systemDestination)
// Create a file log destination
let fileDestination = FileDestination(writeToFile: "/path/to/file", identifier: "advancedLogger.fileDestination")
// Optionally set some configuration options
fileDestination.outputLevel = .debug
fileDestination.showLogIdentifier = false
fileDestination.showFunctionName = true
fileDestination.showThreadName = true
fileDestination.showLevel = true
fileDestination.showFileName = true
fileDestination.showLineNumber = true
fileDestination.showDate = true
// Process this destination in the background
fileDestination.logQueue = XCGLog
Related Skills
node-connect
326.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
80.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
326.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
80.4kCommit, push, and open a PR
