CocoaLumberjack is a fast & simple, yet powerful & flexible logging framework for macOS, iOS, tvOS and watchOS.
First, install CocoaLumberjack via CocoaPods, Carthage, Swift Package Manager or manually.
Then use DDOSLogger
for iOS 10 and later, or DDTTYLogger
and DDASLLogger
for earlier versions to begin logging messages.
platform :ios, '11.0'
target 'SampleTarget' do
use_frameworks!
pod 'CocoaLumberjack/Swift'
end
Note: Swift
is a subspec which will include all the Obj-C code plus the Swift one, so this is sufficient.
For more details about how to use Swift with Lumberjack, see this conversation.
For Objective-C use the following:
platform :ios, '11.0'
target 'SampleTarget' do
pod 'CocoaLumberjack'
end
Carthage is a lightweight dependency manager for Swift and Objective-C. It leverages CocoaTouch modules and is less invasive than CocoaPods.
To install with Carthage, follow the instruction on Carthage
Cartfile
github "CocoaLumberjack/CocoaLumberjack"
As of CocoaLumberjack 3.6.0, you can use the Swift Package Manager as integration method. If you want to use the Swift Package Manager as integration method, either use Xcode to add the package dependency or add the following dependency to your Package.swift:
.package(url: "https://github.com/CocoaLumberjack/CocoaLumberjack.git", from: "3.8.0"),
Note that you may need to add both products, CocoaLumberjack
and CocoaLumberjackSwift
to your target since SPM sometimes fails to detect that CocoaLumerjackSwift
depends on CocoaLumberjack
.
If you want to install CocoaLumberjack manually, read the manual installation guide for more information.
Usually, you can simply import CocoaLumberjackSwift
. If you installed CocoaLumberjack using CocoaPods, you need to use import CocoaLumberjack
instead.
DDLog.add(DDOSLogger.sharedInstance) // Uses os_log
let fileLogger: DDFileLogger = DDFileLogger() // File Logger
fileLogger.rollingFrequency = 60 * 60 * 24 // 24 hours
fileLogger.logFileManager.maximumNumberOfLogFiles = 7
DDLog.add(fileLogger)
...
DDLogVerbose("Verbose")
DDLogDebug("Debug")
DDLogInfo("Info")
DDLogWarn("Warn")
DDLogError("Error")
If you're using Lumberjack as a framework, you can @import CocoaLumberjack;
.
Otherwise, #import <CocoaLumberjack/CocoaLumberjack.h>
[DDLog addLogger:[DDOSLogger sharedInstance]]; // Uses os_log
DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; // File Logger
fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[DDLog addLogger:fileLogger];
...
DDLogVerbose(@"Verbose");
DDLogDebug(@"Debug");
DDLogInfo(@"Info");
DDLogWarn(@"Warn");
DDLogError(@"Error");
When integrating Lumberjack into an existing Objective-C it is possible to run into Multiple methods named 'tag' found with mismatched result, parameter type or attributes
build error.
Add #define DD_LEGACY_MESSAGE_TAG 0
before importing CocoaLumberjack or add #define DD_LEGACY_MESSAGE_TAG 0
or add -DDD_LEGACY_MESSAGE_TAG=0
to Other C Flags/OTHER_CFLAGS in your Xcode project.
CocoaLumberjack also ships with a backend implementation for swift-log.
Simply add CocoaLumberjack as dependency to your SPM target (see above) and also add the CocoaLumberjackSwiftLogBackend
product as dependency to your target.
You can then use DDLogHandler
as backend for swift-log, which will forward all messages to CocoaLumberjack's DDLog
. You will still configure the loggers and log formatters you want via DDLog
, but writing log messages will be done using Logger
from swift-log.
In your own log formatters, you can make use of the swiftLogInfo
property on DDLogMessage
to retrieve the details of a message that is logged via swift-log.
To use swift-log with CocoaLumberjack, take a look the following code snippet to see how to get started.
import CocoaLumberjack
import CocoaLumberjackSwiftLogBackend
import Logging
// In your application's entry point (e.g. AppDelegate):
DDLog.add(DDOSLogger.sharedInstance) // Configure loggers
LoggingSystem.bootstrapWithCocoaLumberjack() // Use CocoaLumberjack as swift-log backend
It is similar in concept to other popular logging frameworks such as log4j, yet is designed specifically for Objective-C, and takes advantage of features such as multi-threading, grand central dispatch (if available), lockless atomic operations, and the dynamic nature of the Objective-C runtime.
In most cases it is an order of magnitude faster than NSLog.
It takes as little as a single line of code to configure lumberjack when your application launches. Then simply replace your NSLog statements with DDLog statements and that's about it. (And the DDLog macros have the exact same format and syntax as NSLog, so it's super easy.)
One log statement can be sent to multiple loggers, meaning you can log to a file and the console simultaneously. Want more? Create your own loggers (it's easy) and send your log statements over the network. Or to a database or distributed file system. The sky is the limit.
Configure your logging however you want. Change log levels per file (perfect for debugging). Change log levels per logger (verbose console, but concise log file). Change log levels per xcode configuration (verbose debug, but concise release). Have your log statements compiled out of the release build. Customize the number of log levels for your application. Add your own fine-grained logging. Dynamically change log levels during runtime. Choose how & when you want your log files to be rolled. Upload your log files to a central server. Compress archived log files to save disk space...
The current version of Lumberjack requires:
Per App privacy details on the App Store, Apple is requesting app developers to provide info about their data collection, us SDK maintainers must provide them with the same data.
By default, CocoaLumberjack does NOT collect any data on its own.
See our Data Collection Practices list.
CocoaLumberjack is a logging framework which makes it easy to send those logs to different platforms.
This is why collecting data might happen quite easily, if app developers include any sensitive data into their log messages.
Important note: app developers are fully responsible for any sensitive data collected through our logging system!
In consequence, you must comply to the Apple's privacy details policy (mentioned above) and document the ways in which user data is being collected. Since the number of scenarios where data might be indirectly collected through CocoaLumberjack is quite large, it's up to you, as app developers, to properly review your app's code and identify those cases. What we can do to help is raise awareness about potential data collection through our framework.
Private data includes but isn't limited to:
Example: DDLogInfo("User: \(myUser)")
will add the myUser
info to the logs, so if those are forwarded to a 3rd party or sent via email, that may qualify as data collection.