TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Nov 2016 |
Maintained by Mats Melke.
LogglyLogger-CocoaLumberjack is a custom logger for CocoaLumberjack that logs to Loggly.
(From 2.3.0 of LogglyLogger-CocoaLumberjack, AFNetworking is no longer a dependency)
LogglyLogger-CocoaLumberjack uses ARC. If your project doesn't use ARC, you can enable it per file using the -fobjc-arc
compiler flag under "Build Phases" and "Compile Sources" on your project's target in Xcode.
First, make sure that you have created an API key in your Loggly account (they call this Customer Token, and can be created in the Loggly Web UI under the tab "Source setup").
In your App Delegate:
#import "LogglyLogger.h"
#import "LogglyFormatter.h"
static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
In didFinishLaunchingWithOptions
LogglyLogger *logglyLogger = [[LogglyLogger alloc] init];
[logglyLogger setLogFormatter:[[LogglyFormatter alloc] init]];
logglyLogger.logglyKey = @"your-loggly-api-key";
// Set posting interval every 15 seconds, just for testing this out, but the default value of 600 seconds is better in apps
// that normally don't access the network very often. When the user suspends the app, the logs will always be posted.
logglyLogger.saveInterval = 15;
[DDLog addLogger:logglyLogger];
// Do some logging
DDLogVerbose(@"{\"myJsonKey\":\"some verbose json value\"}");
This is all there is to it. The log posts will include all the json fields that you put in the log message plus some standard fields that the logger adds automatically:
Note that you don't have to log statements in json format, but it is much easier to do facet searches in Loggly if you do use JSON. Word of warning, don't use too many json keys. It will mess up the Loggly UI. Figure out smart json keys that you can reuse in many of your log statements.
LogglyLogger will use the bundle id of your app as a Loggly tag. You can create a "source group" in Loggly that contains all log statements that has a specific tag. This way, you can easily use the same Loggly account for many apps. If you don't want to use the bundle id as the tag or if you want to use multiple tags, you can set the property logglyTags in the LogglyLogger. (comma-separated list of tags, no whitespace in or between tags)
There are some settings you can set on the LogglyLogger. Most of them are inherited from the abstract class and they all have reasonable default values.
By default, the logmessage will always be logged in the Loggly field "rawlogmessage", even though the log message was successfully parsed and logged as individual JSON fields. To prevent logging of rawlogmessage in this situation, set the LogglyFormatter property alwaysIncludeRawMessage to NO
LogglyFormatter *logglyFormatter = [[LogglyFormatter alloc] init];
logglyFormatter.alwaysIncludeRawMessage = NO;
LogglyLogger *logglyLogger = [[LogglyLogger alloc] init];
[logglyLogger setLogFormatter:logglyFormatter];
To track a specific user you can set the userid property on the LogglyFields object. The username will then be included in every log statement until the app is terminated by iOS.
Similarily you can set a custom session id. If you don't, the session id will be a random string. Let's say that a user complains about having problems in your app. You can then have some input view in your app where the user can enter a string, which you set as the sessionid. At the same time you can set the CocoaLumberjack log level to a finer level for this user. Now you can follow the detailed logs in Loggly for this particular user, by filtering out all but this particular session. Pretty nice, huh?
If you're not happy with the standard fields that are logged with every request, you can implement your own LogglyFieldsDelegate You only need to implement one method:
@protocol LogglyFieldsDelegate
- (NSDictionary *)logglyFieldsToIncludeInEveryLogStatement;
@end
Use your custom delegate by using this init method when creating the LogglyFormatter:
LogglyFormatter *logglyFormatter = [[LogglyFormatter alloc] initWithLogglyFieldsDelegate:yourCustomDelegate];
All feedback and contribution is very appreciated. Please send pull requests, create issues or just send an email to [email protected].