Code Monkey home page Code Monkey logo

jkloggerdispatcher's Introduction

JKLoggerDispatcher

Check the swift version here โ˜ž LogDispatcher.Swift

##Overview When you call NSLog() to print/log something, JKLogDispatcher will walk through a list of registered log processing modules, choose the proper one, and use it to do some additional processing for the log.

##Why overload NSLog()?

The answer is we want to keep the code clean and no redundancy.

Rememeber how you add analytics code to your project?

Let say you need to track how many times the button user have tapped. you add a method responsding to UIButton's UIControlEventTouchUpInside event, and write the analytic code straighthod in this method, Right? It doesn't look anything wrong. BUT when you come back to maintain the code you will know how awful it looks, Or occasionally, the analytic platform update it's SDK for some reason(god knows), you will NEVER make your code run again...

With JKLoggerDispatcher, you don't need to worry about all these shit coming up to you again.

All you need to do is write your own module.

If you want to track how many time the button have been tapped.

The past:

#import <Analytics/Analytics.h>
- (void)buttonDidTap:(id)sender {
	Analytics(@"ButtonTapEvent");
}

Now:

- (void)buttonDidTap:(id)sender {
	NSLog(@"[Analytic] ButtonTapEvent");
}

Then when NSLog() get executed JKLoggerDispatcher will ask your module to process the log message. Easy right?

##Examples

###CocoaLumberjack Connector Module Clone this git and run the demo(Don't forget run pod update before running the project), you will see the log below in the console. Unsurprisingly, you can't see any color in your console. it's actually a XCode pluggin. Check it here XcodeColors.

Screenshot

On the past, you probably need to write these code to do that:

#import <CocoaLumberjack/CocoaLumberjack.h>

DDLogError(@"[ERROR] This is an error.");
DDLogWarn(@"[WARNING] This is a warning.");
DDLogDebug(@"[DEBUG] This is a debug.");
DDLogVerbose(@"[VERBOSE] This is a verbose.");
DDLogInfo(@"[INFO] This is a info.");

With JKLoggerDispatcher it becomes extremely simple. And no import thirdparty library, No DDLog() anymore.

NSLog(@"[ERROR] This is an error.");
NSLog(@"[WARNING] This is a warning.");
NSLog(@"[DEBUG] This is a debug.");
NSLog(@"[VERBOSE] This is a verbose.");
NSLog(@"[INFO] This is a info.");

##Usage ###Add to your project ####Cocoapods add following line in your Podfile.

pod "JKLoggerDispatcher"

####Manual installation copy JKLoggerDispatcher.h and JKLoggerDispatcher.m to your project.

###Add header to project_prefix.pch file If your project is created by XCode 6 and you cannot find your pch file, check this link.

#ifdef __OBJC__
	#import <JKLoggerDispatcher/JKLoggerDispatcher.h>
#endif

We recommend you to put the import in pch file considering it's extremely common for NSLog(). Without importing 'JKLoggerDispatcher.h' JKLoggerDispatcher will not work as expected.

###Write a logger module You can take a look at the JKConsoleLoggerModule in the demo.

In a word, Make your class confirms to JKLoggerModule protocol and implement - (BOOL)dispatcher:(JKLoggerDispatcher *)dispatcher canModuleProcessLogWithFileName:(NSString *)fileName method:(NSString *)method line:(int)line text:(NSString *)format arguments:(va_list)arguments.

Here's example:

//JKExampleLoggerModule.h
@interface JKExampleLoggerModule : NSObject <JKLoggerModule>
@end

//JKExampleLoggerModule.m
@implementation JKExampleLoggerModule
- (BOOL)dispatcher:(JKLoggerDispatcher *)dispatcher canModuleProcessLogWithFileName:(NSString *)fileName 	method:(NSString *)method line:(int)line text:(NSString *)format arguments:(va_list)arguments {
	//Do your job here.Like analytics or colour the log message.
	//Return YES if log have been processed. No otherwise.
}
@end

###Register your logger module - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[JKLoggerDispatcher defaultDispatcher] registerLoggerModule:[[JKExampleLoggerModule alloc] init]]; return YES; }

If no moudle is registered, NSLog() works as usual.

##Requirements

  • Automatic Reference Counting (ARC)
  • iOS 5.0+
  • Xcode 4.0+
  • Demo project requires cocoapods: pod install

##Contributing If you have any questions, bug reports, improvements, please file an issue.

jkloggerdispatcher's People

Contributors

fsjack avatar

Watchers

Wong Jun avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.