Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use XCGLogger in a framework

I want to use XCGLogger inside of a Swift framework I'm writing. The app that includes this framework may or may not use XCGLogger as well.

What's the best way to approach this sort of scenario? Would I use something like dependency injection to let the app send the XCGLogger instance to the framework? Where in the framework would I call XCGLogger's setup method?

like image 656
0x6A75616E Avatar asked Mar 12 '16 07:03

0x6A75616E


1 Answers

I'm able to use the same logger in the project and a framework by simply referencing the same XCGLogger.defaultInstance() in both.

First, import XCGLogger in the imports, Then instantiate the log instance in the project using let log = XCGLogger.defaultInstance().

After that I instantiate the sharedInstance of the framework I'm using (per my particular use case).

Within the framework, import XCGLogger & instantiate the logger let log = XCGLogger.defaultInstance() before my Class declarations.

Then, back in didFinishLaunchingWithOptions of AppDelegate.swift, I do the log.setup(...your params...).

In the case of you authoring the framework, you'll want some logic and readme notes to setup the logger if one isn't setup the way you prefer.

Probably not the most elegant way to get it done, but it does work.

Hope that helps.

like image 164
Jason Wiener Avatar answered Sep 20 '22 20:09

Jason Wiener