How can I use XCGLogger in a mixed Xcode project (Objective-C and Swift)

395 Views Asked by At

I have a mixed Xcode project. My appDelegate is written in Objective-C and my Controller partly in Swift. Is it possible to use XCGLogger in that project? If yes, how can I initialized the XCGLogger in the appDelegate (e.g. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions)?

1

There are 1 best solutions below

0
Vinh Nguyen On

I found and had the same issue too, wanting to incorporate in a mixed Swift+Objective-C project. Fortunately, I found this PR https://github.com/DaveWoodCom/XCGLogger/pull/122 that helps with a solution.

But in order to check out his PR we need to make some change:

@objc public class XCGLogWrapper: NSObject {
    @objc public static func log(_ level: LogLevel = LogLevel.debug, functionName: String = #function, fileName: String = #file, lineNumber: Int = #line, logMessage: String) {}

notice the omission of level label argument.

===

Explanation:

so from this static function of XCGLogWrapper:

static XCGLogWrapper.log(_:functionName:fileName:lineNumber:logMessage:)

will be translated to this in Objective-C:

[XCGLogWrapper log:level functionName: @(__PRETTY_FUNCTION__) fileName: @__FILE__ lineNumber: __LINE__ logMessage: message ]

===

For more complete answer, check my comment on the PR https://github.com/DaveWoodCom/XCGLogger/pull/122#issuecomment-468136786

Hope it helps!