CocoaLumberjack / CocoaLumberjack

A fast & simple, yet powerful & flexible logging framework for macOS, iOS, tvOS and watchOS

Home Page:https://cocoalumberjack.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use cocoalumberjack with Swift

GuyFran opened this issue · comments

Hi.
I've been reading here and there, and I am unsure how to use the framework with swift.

I've seen people aking their own wrappers in swift, but that was quite some months ago.

Following the issues in there, my understanding is that there's an official swift wrapper for using cocoalumberjack.

Having retrieved the lib with cocoapods , the beta 4 from 2.0.0, I still don't see any swift class there, and none of the example projects uses a swift also.

So I'm kinda confused how to make that work with the "official" means.

Any light that you can shed on this ?

Thx

I think that for now you should install the standard Objective-C library with CocoaPods and then manually import the CocoaLumberjack.swift wrapper, as CocoaPods doesn't yet support Swift.

I will just make sure that the wrapper is preserved in the directory as currently it gets deleted by CocoaPods.

Also there is #400.

I have added , as usual, the lib pod to my podfile. It's there.

Now I've manually imported the CocoaLumberjack.swift to my project (in swift).
But I get a compile error : import CocoaLumberjack -> "No such module"

Do I need to add on something ? Tweak the bridging header file ?

I have followed the procedure indicated, and it's working for alamofire and swiftjson.

Not working for cocoalumberjack though.
I tried to paste

pod 'CocoaLumberjack', :git => "https://github.com/CocoaLumberjack/CocoaLumberjack.git", :branch => "podspec"

in my Podfile. No success. What would be the proper line for the podfile for this library ?

@crazykoala Give this a try:

pod 'CocoaLumberjack', :git => "https://github.com/ValCapri/CocoaLumberjack.git"

Ok this retrieves the pod with the .swift wrapper.

But still it doesn't compile afterwards.

  • If I uses "pod install" to retrieve my pods, i get this error "import CocoaLumberjack : No such module'
  • If I use "bundle exec pod install" to retrieve the pods , the .h of the other pods are not found. Erf.

Really no luck :/

@palewar Maybe also include the :commit.

@rivera-ernesto , is there such a way with the "official" repo ?

Or is there a step by step guide on how to include the lib with a swift project ? (with or without cocoapods)

Yes. You just set the official repository in the :git part.

I see , but I'm actually trying to get it working with Swift.

@palewar , when I use this branch, I get the swift class, but I get an error upon compile : " No such module CocoaLumberjack" on the import line.

Could you help me fix this ?

Closing this in favor of #383.

@crazykoala Probably a silly question, but are you using .xcworkspace file to open and build project? Please share your Podfile with me or your github URL if it's a public project and I will see if I can do anything to get it working.

I will try to set up a proper Swift Demo project.

@palewar yes , sure I am using the workspace.

@rivera-ernesto , a demo with the cocoalumberjack as pod dependency. I'd be very curious to see how it's set up, cause mine fails no matter what i try, Thx

Gonna make one tomorrow.

What was the outcome here guys? I am having the same issue.

Yup, got the same question. I know this ticket is closed, but I got the same "No such module" error.

CocoaPods is very close to releasing Swift support. Does anyone wants to give a try to the betas?

Sadly I didn't get the time to work on it.

Let's reopen this in the mean time.

@crazykoala @nateuni @depl0y
I am using current release version of CocoPods (0.35.0) and I got the same issue compile error : import CocoaLumberjack -> "No such module"
To get past this for now, I edited the CocoaLumberjack.swift file to remove the line:
import CocoaLumberjack

Like @rivera-ernesto said, the next version of CocoaPods will have support for Swift (and frameworks) so this issue will be resolved.

Hope this helps.

What would be possible is to create a 2.1.x Swift branch that uses 0.36.x betas.

Yes, I think a separate branch is appropriate.

I'm also trying to add CocoaLumberjack to a project using Swift.

I'm using Cocoapods Version 0.36.0.rc.1
My Podfile looks like this:

platform :ios, "8.1"

pod "CocoaLumberjack", '2.0.0-rc2'

After pod install I open the xcworkspace. The CocoaLumberjack.swift file is not in the Pods project. So I add it manually from the filesystem. And now I get a "No such module 'CocoaLumberjack'" on the import CocoaLumberjack line in the CocoaLumberjack.swift file. Same error happens if I try to import it to my own swift files in my own project.

Is there any way I can solve this myself before CocoaPods and CocoaLumberjack moves forward?

Solution:

So I managed to find a solution. What I did was to first update the Podfile as follows:

platform :ios, "8.1"

pod "CocoaLumberjack", '2.0.0-rc2'

use_frameworks!

Note the use_frameworks! row at the bottom. After that run pod update / pod install as usual. And finally find the CocoaLumberjack.swift file in the filesystem and add it to the Pods-CocoaLumberjack target. Worky-worky!

The only thing I don't like now is that Xcode autocompletes DDLogXX as follows:

DDLogInfo(logText: @autoclosure () -> String, level: DDLogLevel, asynchronous: Bool)

I don't want to set level and asynchronous since they are optional. So I have to remove those every time I write a log statement. Minor issue, but still a little bit irritating.

Nice to hear this @petaren.
As you're using 0.36 you could also add the Swift files to the podspec. We can then add your code to a separate branch and release Swift support as soon as 0.36.0 is released.

@rivera-ernesto I created a pull-request. Not sure I did this github-think completely right, just tell me if it's not.

Thanks for the help!

I know that it is duplicate. But the issue is still not resolved. :-(

I have added swift as per petaren's comment on Feb 25..

But I have errors galore.. anyone?

image

That code looks like pre-Swift 1.2. Are you using the latest swift_support branch?

Ops, I looked around but couldn't find any clear info that.. what is the string that i need to add to cocoapods ?

How do I use the swift_support branch for cocoapods? pod 'CocoaLumberjack', :git => 'https://github.com/CocoaLumberjack/CocoaLumberjack.git', :branch => 'swift_support' gives an error.

See my duplicate issue in #537. For Obj-C I fixed the issue. The problem is that the .modulemap needs to be namedmodule.modulemap and not CocoaLumberjack.modulemap. Furthermore, the .modulemap needs to be exposed as a public header in the podspec via s.public_headers = ...

Then you can do @import CocoaLumberjack; and it works. This is with version 0.37.1 of Cocoapods.

@thejeraldo please retry, it should work now. pod 'CocoaLumberjack/Swift', :swift_support

@dnkoutso could you create a PR targeting the swift_support branch?

@bpoplauschi Thanks it worked as pod 'CocoaLumberjack', :git => 'https://github.com/CocoaLumberjack/CocoaLumberjack.git', :branch => 'swift_support'. Though I still have to manually add the CocoaLumberjack.swift file to my project but it's okay. Cheers.

@bpoplauschi Let me verify 100% this is the way to go. I find it weird module.map must be specified in public_headers for this to work.

@bpoplauschi Seems like its working when I use use_frameworks! in my Podfile, otherwise I have to do the changes I mentioned for it to work.

@thejeraldo thanks, your comments help me a lot:-)

commented

Somebody can make a summary on how to use Cocoalumberjack with SWIFT 2.0 ?

I created a test project here> https://github.com/johndpope/CocoaLumberjack-Swift

pod 'CocoaLumberjack/Swift', :git => 'https://github.com/CocoaLumberjack/CocoaLumberjack.git', :branch => 'swift_2.0'

I had troubles with the original Formatter class in this repo - so feel free to grab my one.

import UIKit
import CocoaLumberjack

let ddloglevel = DDLogLevel.Verbose

private func printSomething() {
DDLogVerbose("Verbose");
DDLogDebug("Debug");
DDLogInfo("Info");
DDLogWarn("Warn");
DDLogError("Error");
}

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

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let formatter = Formatter()
    DDTTYLogger.sharedInstance().logFormatter = formatter

    DDLog.addLogger(DDTTYLogger.sharedInstance())

    DDLogVerbose("Verbose");
    DDLogDebug("Debug");
    DDLogInfo("Info");
    DDLogWarn("Warn");
    DDLogError("Error");

    printSomething()

    defaultDebugLevel = ddloglevel

    printSomething()

    return true
}
commented

Thanks a lot John ! This was be very helpfull ! This little exemple save my night !

@johndpope thanks! extremely helpful!

I installed PubNub which has dependency of CocoaLumberjack using CocoaPods. It installed fine and CocoaLumberjack.swift is created without trouble.

commented

@bpoplauschi Is still an issue in master branch? it would be great help if we get out of this workaround?

I'm not sure. You should try for yourself :)

I've been building an objective-c to swift converter.
Ping me if you want to check out source code. http://objc2swift.io

commented

Thanks @johndpope is it beta? and will it work as original Lumberjack(Objective-c) version?

It's early beta - but stable. Fire up the app - point it to an existing repository. It will trawl through .m source files en masse and translate to .swift files.

Defect list is growing - but I have been collecting them.
https://rink.hockeyapp.net/recruit/acc09efcee42415aa406036f3a24ab0b

this is a sample repo - from a few weeks back.

johndpope/EZForm@21c27bd

commented

Thanks a lot i will play with this in sometime

commented

Just run the demo project.
https://github.com/johndpope/CocoaLumberjack-Swift

i get compile error. any idea?

:0: error: could not build Objective-C module 'CocoaLumberjack'
0 swift 0x00000001105b22b8 llvm::sys::PrintStackTrace(__sFILE*) + 40
1 swift 0x00000001105b2794 SignalHandler(int) + 452
2 libsystem_platform.dylib 0x00007fff8f37ef1a _sigtramp + 26
3 libsystem_platform.dylib 0x00007fff4fd67198 _sigtramp + 3231613592
4 swift 0x000000010fe9a4e6 main + 1814
5 libdyld.dylib 0x00007fff87e235c9 start + 1
Stack dump:

I just tested with the https://github.com/johndpope/CocoaLumberjack-Swift project and after a few updates, everything worked fine. I have created a PR with the updates. I think our current Swift branch looks good and can be merged into master.

I have just released a Beta 2.1.0 version via CocoaPods including the Swift 2.0 support (also available in the latest master). Please test it and tell us if it works as expected.

@bpoplauschi Is there any documentation around swift support yet? such as setup and usage?

I think that would have to come from contributors through pull requests ;)

@bpoplauschi @rivera-ernesto with swift 2.0 support in master, do we still have to manually import the swift wrapper file (CocoaLumberjack.swift)?

Thx @bpoplauschi I've merged that request.

fyi - it's possible to use bundler to manage dependencies around which cocoapods version a project should use. As swift has been in a state of flux / one week project may work - the next - broken.

I don't have time to add to project - but I highly recommend this if you're working in teams with different cocoapod / xcode versions.
http://www.sm-cloud.com/rvm-bundler-cocoapods-happy-dependency-management/

I think we still need to write the documentation part. Anyone can help with that?

Closing this with the following answer (Cocoapods):

pod 'CocoaLumberjack/Swift', :tag => '2.1.0-beta'