Skip to content

Instantly share code, notes, and snippets.

@dblandin
Forked from iwarshak/Gemfile
Last active December 19, 2015 21:39
Show Gist options
  • Save dblandin/6021930 to your computer and use it in GitHub Desktop.
Save dblandin/6021930 to your computer and use it in GitHub Desktop.
RubyMotion Papertrail Logger
Log = Motion::Log
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
.....
Log.addLogger(papertrail_logger)
Log.level = NSBundle.mainBundle.infoDictionary['LOG_LEVEL'].to_sym
....
end
def papertrail_logger
PapertrailLogger.alloc.init.tap do |logger|
logger.host = ENV['PAPERTRAIL_HOST']
logger.port = ENV['PAPERTRAIL_PORT']
logger.app_name = App.name
logger.app_version = NSBundle.mainBundle.objectForInfoDictionaryKey(KCFBundleVersionKey)
end
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, :app_name, :app_version
def logMessage(message)
message_string = message.logMsg
formatted_message = format_message(message_string)
send_message(formatted_message)
end
def send_message(message)
udp_socket.sendData(message.to_data, toHost: host, port: port, withTimeout: -1, tag: 1)
end
def udp_socket
GCDAsyncUdpSocket.alloc.initWithDelegate(nil, delegateQueue: Dispatch::Queue.main)
end
def format_message(message)
"<22>#{time} #{app_name} #{app_version}: #{message}"
end
def time
Time.now.strftime("%b %e %H:%M:%S")
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment