Skip to content

Instantly share code, notes, and snippets.

@Pash237
Last active August 29, 2015 14:06
Show Gist options
  • Save Pash237/25abaa32db1d17c27dc4 to your computer and use it in GitHub Desktop.
Save Pash237/25abaa32db1d17c27dc4 to your computer and use it in GitHub Desktop.
//
// Created by Pavel Alexeev on 03.09.14.
//
#import <Foundation/Foundation.h>
@interface Logger : NSObject
+ (void)logString:(NSString *)string;
+ (void)log:(NSString *)format, ... NS_REQUIRES_NIL_TERMINATION;
@end
//
// Created by Pavel Alexeev on 03.09.14.
//
#import "Logger.h"
#import "AFHTTPRequestOperation.h"
#import "Session.h"
#import "NSString+PhoneNumberFormat.h"
#import "NSDate+Formatter.h"
@implementation Logger
static NSMutableString *messagesToSend;
static NSURL *logglyURL;
static NSString *bundleId;
static NSString *deviceId;
static NSString *version;
+ (void)logString:(NSString *)string
{
NSLog(@"%@", string);
if (!messagesToSend) {
messagesToSend = [NSMutableString string];
[messagesToSend appendString:@".\n"];
[messagesToSend appendString:@".\n"];
[messagesToSend appendString:@".\n"];
[messagesToSend appendString:@"----------------------------------------------\n"];
[messagesToSend appendFormat:@"Starting session at %@\n", [[NSDate date] isoString]];
[messagesToSend appendString:@"----------------------------------------------\n"];
[messagesToSend appendString:@".\n"];
[[NSNotificationCenter defaultCenter] addObserver: [self class]
selector: @selector(applicationDidEnterBackground:)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
}
[messagesToSend appendFormat:@"%@\n", string];
if ([string containsString:@"error"] || messagesToSend.length > 1000) {
[self sendLogs];
}
}
+ (void)applicationDidEnterBackground:(NSNotification *)notification
{
[self sendLogs];
}
+ (void)log:(NSString *)format, ...
{
va_list args;
va_start(args, format);
NSString *string = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
[self logString:string];
}
+ (void)sendLogs
{
NSMutableString *sendingMessages = messagesToSend;
messagesToSend = [NSMutableString string];
if (!logglyURL) {
logglyURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://logs-01.loggly.com/bulk/%@/tag/%@/",
[Logger logglyKey],
[Logger logglyTags]]];
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:logglyURL];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/plain;charset=UTF-8" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[sendingMessages dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// Make sure the post request can finish in background
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
// Handle what to do when the background time has been consumed and iOS will shut us down
// So, let's do... nothing at all
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *op, id responseObject) {
} failure:^(AFHTTPRequestOperation *op, NSError *error) {
NSLog(@"Loggly post error: %@", error);
[messagesToSend insertString:@"\n" atIndex:0];
[messagesToSend insertString:sendingMessages atIndex:0];
}];
[operation start];
}
+ (NSString *)logglyKey
{
return @"YOUR-LOGGLY-API-KEY";
}
+ (NSString *)logglyTags
{
if (!bundleId) {
bundleId = [NSBundle mainBundle].infoDictionary[@"CFBundleIdentifier"];
}
if (!version) {
version = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
}
if (!deviceId) {
deviceId = [[UIDevice currentDevice] identifierForVendor].UUIDString;
}
return [NSString stringWithFormat:@"%@,%@,%@,%@,", bundleId, version, deviceId, [[Session currentSession].phoneNumber rawPhoneNumberWithoutPlus]];
}
@end
#define Log(format, ...) [Logger logString:[NSString stringWithFormat:@"%s:%d " format, __func__, __LINE__, ## __VA_ARGS__]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment