2

Trying to set up XCGLogger and receiving error:

Ambiguous reference to member 'log'

I see this issue was already raised but I'm not clear on the solution..

Per the install guide added this global constant to AppDelegate.swift:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    let log = XCGLogger.defaultInstance()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil, fileLogLevel: .Debug)  
        return true
    }

Then in individual source files:

import XCGLogger
log.debug("A debug message")

What is the proper usage?

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
  • Where in AppDelegate did you put the code? Is it in the AppDelegate class or global? – ryantxr Apr 28 '16 at 19:22
  • `nameOfYourProject.log.debug`? But probably renaming `log` to something else will be better. Were you able to find the colliding identifier `log`? – Sulthan Apr 28 '16 at 19:22
  • @ryantxr: Updated question – Marcus Leon Apr 28 '16 at 19:35
  • It seems `log` is also defined somewhere else in your project, possibly in some library. You could call `self.log` to refer to the `log` you have defined in `AppDelegate`. – Sulthan Apr 28 '16 at 19:50
  • @Sulthan - I don't think that's the issue... same thing if I rename `log` to `logx`. I'm guessing the AppDelegate global constants are not in scope. Is there a recommend method to access them? – Marcus Leon Apr 28 '16 at 19:54
  • I have just tried to install `XCGLogger` using pods and I cannot reproduce the issue. – Sulthan Apr 28 '16 at 20:11
  • Ok I'm using pods as well... Thanks for looking. Will do some more research. Realized I didn't update the Embedded Binaries config that the doc mentions. Did you do that? – Marcus Leon Apr 28 '16 at 20:13

2 Answers2

4

The issue is rather simple. If you declare log inside AppDelegate, you are making an instance variable. To access it, you will have to access it as instance variable:

(UIApplication.sharedApplication().delegate as! AppDelegate).log.debug("test")

If you want log to be accessible everywhere, you will have to make it a global constant:

In your AppDelegate, declare a global constant to the default XCGLogger instance.

let log = XCGLogger.defaultInstance()

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

(there is no need to declare it in AppDelegate file, you can basically put it anywhere in your code)

or make it static:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    static let log = XCGLogger()

and access it using:

AppDelegate.log.debug(...)

To explain the ambigious reference, there is a mathematical function called log, and there is also a log function in the malloc.h file. Since you are passing a String as the first parameter and neither of the two functions is a match, the compiler warns you that it does not know which of the two functions you want to use.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

Also I guess it's better to create global constant file and create something like this (if you've declared log in AppDelegate):

let LOG = (UIApplication.sharedApplication().delegate as! AppDelegate).log

and then simple use LOG.error("error")

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277