Skip to content

Instantly share code, notes, and snippets.

@cfr
Forked from stigi/CocoaLumberjack.swift
Last active May 9, 2018 06:46
Show Gist options
  • Save cfr/7da8da56654288fad7aa to your computer and use it in GitHub Desktop.
Save cfr/7da8da56654288fad7aa to your computer and use it in GitHub Desktop.
Swift 1.2 CocoaLumberjack wrapper

CocoaLumberjack.swift

Prerequisites

  • Add CocoaLumberjack 2.0+ in your Podfile, i.e. pod 'CocoaLumberjack/Default', '~> 2.0.0-rc'.
  • Also import CocoaLumberjack/CocoaLumberjack.h in your bridging header.

Configuring the loggin

  • As usual add all the loggers you might want/need: DDLog.addLogger(DDTTYLogger.sharedInstance())
  • Configure the log level like so: DDLog.logLevel = .Info
  • Log away :)
  • You might also want to enable/disable async logging: DDLog.logAsync = false

License

This Gist is under MIT

// Updated to Swift 1.2 and CocoaLumberjack 2.0.0-rc by Stan Serebryakov on 2015-02-16
import Foundation
extension DDLog {
private struct State {
static var logLevel: DDLogLevel = .Error
static var logAsync: Bool = true
}
class var logLevel: DDLogLevel {
get { return State.logLevel }
set { State.logLevel = newValue }
}
class var logAsync: Bool {
get { return (self.logLevel != .Error) && State.logAsync }
set { State.logAsync = newValue }
}
class func log(flag: DDLogFlag, @autoclosure message: () -> String,
function: String = __FUNCTION__, file: String = __FILE__, line: UInt = __LINE__) {
if flag.rawValue & logLevel.rawValue != 0 {
var logMsg = DDLogMessage(message: message(), level: logLevel, flag: flag, context: 0,
file: file, function: function, line: line,
tag: nil, options: DDLogMessageOptions(0), timestamp: nil)
DDLog.log(logAsync, message: logMsg)
}
}
}
func logError(@autoclosure message: () -> String, function: String = __FUNCTION__,
file: String = __FILE__, line: UInt = __LINE__) {
DDLog.log(.Error, message: message, function: function, file: file, line: line)
}
func logWarn(@autoclosure message: () -> String, function: String = __FUNCTION__,
file: String = __FILE__, line: UInt = __LINE__) {
DDLog.log(.Warning, message: message, function: function, file: file, line: line)
}
func logInfo(@autoclosure message: () -> String, function: String = __FUNCTION__,
file: String = __FILE__, line: UInt = __LINE__) {
DDLog.log(.Info, message: message, function: function, file: file, line: line)
}
func logDebug(@autoclosure message: () -> String, function: String = __FUNCTION__,
file: String = __FILE__, line: UInt = __LINE__) {
DDLog.log(.Debug, message: message, function: function, file: file, line: line)
}
func logVerbose(@autoclosure message: () -> String, function: String = __FUNCTION__,
file: String = __FILE__, line: UInt = __LINE__) {
DDLog.log(.Verbose, message: message, function: function, file: file, line: line)
}
@melvinmt
Copy link

melvinmt commented Nov 5, 2014

@cfr do you know how I should pass a formatted string to @autoclosure?

I wanted to do this, but doesn't work:

logError("oops error: %@", error.localizedDescription)

this works, but seems rather elaborate for log messaging:

logError(NSString(format:"oops error: %@", error.localizedDescription))

@melvinmt
Copy link

melvinmt commented Nov 5, 2014

@cfr oops never mind, forgot I can use inline variables in swift strings, just need to rewrite a bunch of logs

logError("oops error: \(error.localizedDescription)")

@chourobin
Copy link

Just FYI, cocoalumberjack has been updated to include Swift support and it's own cocoalumberjack.swift file.

@bkeet
Copy link

bkeet commented Nov 12, 2014

@chourobin managed to get beta4 working with CocoaLumberjack's included CocoaLumberjack.swift file (had to include all the additional module header files in the bridging-header and comment out the cocoalumberjack import in the swift file), but unfortunately could not get the DDLogLevel filtering to work, so still using @cfr's implementation which works perfectly.

@rismay
Copy link

rismay commented Dec 8, 2014

Cool stuff! Unfortunately, for some reason, I'm getting the following error on line 23 after I remove the timestamp parameter (for some reason it doesn't recognize that method signature). I'm using beta3. Any ideas?
Cannot invoke 'init' with an argument list of type '(logMsg: @autoclosure () -> String, level: @lvalue DDLogLevel, flag: DDLogFlag, context: IntegerLiteralConvertible, file: String, function: String, line: UInt, tag: NilLiteralConvertible, options: $T10)'

@cfr
Copy link
Author

cfr commented Feb 3, 2015

Works well with CocoaLumberjack 2.0.0-rc.

@svetlanama
Copy link

thanks it is really helpful!!!
I managed to display
logError("here is some error")
logDebug("here is some error") etc

However I can't see the function name, line etc information. Could someone help please? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment