Skip to content

Instantly share code, notes, and snippets.

@iwarshak
Last active May 12, 2018 21:54
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iwarshak/5773987 to your computer and use it in GitHub Desktop.
Save iwarshak/5773987 to your computer and use it in GitHub Desktop.
Here is what you need to get your Rubymotion application logs sent to papertrailapp.com. I am using Cocoalumberjack (CLJ) which seems to be the logging framework of choice for Cocoa, motion-logger which is a thin RM wrapper around Cocoalumberjack. CLJ allows you to write your own loggers, which is what PapertrailLogger is. It simply fires off lo…
Log = Motion::Log
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
.....
Log.addLogger(DDASLLogger.sharedInstance())
Log.addLogger(DDTTYLogger.sharedInstance())
papertrail_logger = PapertrailLogger.sharedInstance('logs.papertrailapp.com', YOUR_PORT)
Log.addLogger(papertrail_logger)
Log.level = ENV["log_level"].to_sym
....
end
end
source :rubygems
gem "rake"
gem 'motion-logger' #cocoalumberjack wrapper
##### this file belongs in vendor/umberjack_patch/log_message.m
#import "../Pods/CocoaLumberjack/Lumberjack/DDLog.h"
@interface DDLogMessage (InstVar)
- (NSString*) logMsg;
- (NSNumber *) logLevel;
@end
@implementation DDLogMessage (InstVar)
- (NSString *)logMsg {
return logMsg;
}
-(NSNumber *) logLevel {
return [NSNumber numberWithInt:logLevel];
}
@end
class PapertrailLogger < DDAbstractLogger
attr_accessor :host, :port
def initWithHost(host, port:port)
init
@host = host
@port = port
@app_version = NSBundle.mainBundle.objectForInfoDictionaryKey(KCFBundleVersionKey)
return self
end
def self.sharedInstance(host, port)
Dispatch.once do
@logger ||= PapertrailLogger.alloc.initWithHost(host, port:port.to_i)
end
@logger
end
def logMessage(msg)
udpSocket = GCDAsyncUdpSocket.alloc.initWithDelegate(nil, delegateQueue:Dispatch::Queue.main)
message = msg.logMsg
ts = Time.now.strftime("%b %e %H:%M:%S")
email = App.delegate.current_user.email if App.delegate.current_user
formatted_str = "<22>#{ts} APPNAME #{@app_version}: #{message}"
udpSocket.sendData(formatted_str.to_data, toHost:@host, port:@port, withTimeout:-1, tag:1 )
end
end
# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require 'bundler'
Bundler.require
Motion::Project::App.setup do |app|
app.pods do
pod 'CocoaLumberjack'
pod 'CocoaAsyncSocket'
end
app.vendor_project 'vendor/lumberjack_patch', :static
end
@iwarshak
Copy link
Author

I would not use this in production. I noticed very strange phone behavior when this was installed on my app (and logging was happening in the background).

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