Giter Site home page Giter Site logo

logdispatcher.swift's Introduction

LogDispatcher.Swift

LogDispatcher is a simple demonstration of how we can use method overloading to override a swift library function println(_:) , giving it an opportunity to be more powerful and useful.

With LogDispatcher, you can create LogProcessingModules.

When you call println(_:) to print/log something, LogDispatcher 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.

For instance, you can have a log processing module that automaically adds a โš ๏ธ sign before the warning log, a log processing module that automaically report the error, etc.

LogDispather is totally transparent.

If your code is included in a target that does not have LogDispatcher, it will compile and all the println(_:) will become normal println(_:). You do not have to change a single line of code.

===

Checkout the NSLog()/(Objective-C) version > JKLoggerDispatcher

##Usage and Examples

The overloaded println(_:) takes a dictionary parameter.

public func println<T>(object: Dictionary<String,T>) -> Bool

You can create LogProcessingModules by confirming to the LogProcessingModuleType protocol

public protocol LogProcessingModuleType {
    var logKey: String { get }
    
    func processLog<T>(content: T) -> Bool /* Bool -> Processed */
}

When you print a dictionary, LogDispatch will take over.

//Example
println(["Error": "Cannot find the saved configuration file, the default configuration will be used"])

It will go through every (key, value) in the dictionary, see if there's a LogProcessingModule which can handle and process the value by matching the key with the LogProcessingModule's logKey property. Once it finds a LogProcessingModule, it will call the LogProcessingModule's func processLog<T>(content: T) -> Bool, passing the value to the method.

####Example: Auto error reporting

An error reporting log processing module can look like this

public class ErrorLogProcessingModule: LogProcessingModuleType {
    public var logKey: String {
        //The identifier of error log will be "Error"
        return "Error"
    }
    public func processLog<T>(content: T) -> Bool {
        if let content = content as? String {
            println("!!ERROR!!\n\(content)")
            //Send error info to your server
            //NSURLConnection.sendAsynchronousRequest(request, queue: nil, completionHandler: nil)
            return true
        } else {
            return false
        }
    }
}

Then, register the log processing module

let errorProcessingModule = ErrorLogProcessingModule()

LogDispatcher.registerLogProcessingModule(errorProcessingModule)

After that, if you call println(["Error": "Cannot find the saved configuration file, the default configuration will be used"]), the error will be printed as below and will be reported to your server as well.

##Implementation

LogDispatcher is simple. Everything is in the LogDispatcher.swift, less than 40 lines of code.

But there's one tricky part:

LogDispatcher wants to call the original println(_:) when it cannot find a LogProcessingModule for a log.

There's already a overloaded version of println(_:) which handles the Dictionary<String,T> input, how can we call the original println(_:) with the Dictionary<String,T> parameter?

The solution is to use a different return type for the overloaded version.

You may have noticed that the overloaded version of println(_:) returns a Bool to indicate whether a log has been processed. Thus, if we explicitly mark the return type as Void, we can reach the original version of the function.

Here comes the unused result

if !processed {
    let result: Void = println(object)
}

##Contributing

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

logdispatcher.swift's People

Contributors

yuao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.