Skip to content

Commit

Permalink
Added logging levels for Verbose and Light
Browse files Browse the repository at this point in the history
  • Loading branch information
andysmart committed Sep 4, 2015
1 parent db5bb73 commit 4db45c5
Showing 1 changed file with 128 additions and 11 deletions.
139 changes: 128 additions & 11 deletions Source/Timberjack.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import Foundation

let TimberjackRequestHandledKey = "Timberjack"
let TimberjackRequestTimeKey = "TimberjackRequestTime"

public enum Style {
case Verbose
case Light
}

public class Timberjack: NSURLProtocol {
var connection: NSURLConnection?
var data: NSMutableData?
var response: NSURLResponse?

public static var logStyle: Style = .Verbose

public class func register() {
self.registerClass(self)
}

public class func unregister() {
self.unregisterClass(self)
}

//MARK: - NSURLProtocol

public override class func canInitWithRequest(request: NSURLRequest) -> Bool {
Expand All @@ -29,27 +45,128 @@ public class Timberjack: NSURLProtocol {
guard let newRequest = request.mutableCopy() as? NSMutableURLRequest else { return }

Timberjack.setProperty(true, forKey: TimberjackRequestHandledKey, inRequest: newRequest)
Timberjack.setProperty(NSDate(), forKey: TimberjackRequestTimeKey, inRequest: newRequest)

connection = NSURLConnection(request: newRequest, delegate: self)

logRequest(newRequest)
}

public override func stopLoading() {
connection?.cancel()
connection = nil
}

// MARK: NSURLConnectionDelegate

func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
let policy = NSURLCacheStoragePolicy(rawValue: request.cachePolicy.rawValue) ?? .NotAllowed
client?.URLProtocol(self, didReceiveResponse: response, cacheStoragePolicy: policy)

self.response = response
self.data = NSMutableData()
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
client?.URLProtocol(self, didLoadData: data)
self.data?.appendData(data)
}

func connectionDidFinishLoading(connection: NSURLConnection!) {
client?.URLProtocolDidFinishLoading(self)

if let response = response {
logResponse(response, data: data)
}
}

func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
client?.URLProtocol(self, didFailWithError: error)
logError(error)
}

//MARK: - Logging

public func logDivider() {
print("---------------------")
}

public func logError(error: NSError) {
logDivider()

if let url = newRequest.URL?.absoluteString {
print("Request: \(url)")
print("Error: \(error.localizedDescription)")

if Timberjack.logStyle == .Verbose {
if let reason = error.localizedFailureReason {
print("Reason: \(reason)")
}

if let suggestion = error.localizedRecoverySuggestion {
print("Suggestion: \(suggestion)")
}
}
}

public func logRequest(request: NSURLRequest) {
logDivider()

print("Method: \(newRequest.HTTPMethod)")
if let url = request.URL?.absoluteString {
print("Request: \(request.HTTPMethod) \(url)")
}

if let headers = newRequest.allHTTPHeaderFields {
print("Headers: [")
for (key, value) in headers {
print(" \(key) : \(value)")
if Timberjack.logStyle == .Verbose {
if let headers = request.allHTTPHeaderFields {
self.logHeaders(headers)
}
print("]")
}
}

public override func stopLoading() {
connection?.cancel()
connection = nil
public func logResponse(response: NSURLResponse, data: NSData? = nil) {
logDivider()

if let url = response.URL?.absoluteString {
print("Response: \(url)")
}

if let httpResponse = response as? NSHTTPURLResponse {
let localisedStatus = NSHTTPURLResponse.localizedStringForStatusCode(httpResponse.statusCode).capitalizedString
print("Status: \(httpResponse.statusCode) - \(localisedStatus)")
}

if Timberjack.logStyle == .Verbose {
if let headers = (response as? NSHTTPURLResponse)?.allHeaderFields as? [String: AnyObject] {
self.logHeaders(headers)
}

if let startDate = Timberjack.propertyForKey(TimberjackRequestTimeKey, inRequest: request) as? NSDate {
let difference = startDate.timeIntervalSinceNow
print("Duration: \(difference) secs")
}

guard let data = data else { return }

do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
let pretty = try NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted)

if let string = NSString(data: pretty, encoding: NSUTF8StringEncoding) {
print("JSON: \(string)")
}
}

catch {
if let string = NSString(data: data, encoding: NSUTF8StringEncoding) {
print("Data: \(string)")
}
}
}
}

public func logHeaders(headers: [String: AnyObject]) {
print("Headers: [")
for (key, value) in headers {
print(" \(key) : \(value)")
}
print("]")
}
}

0 comments on commit 4db45c5

Please sign in to comment.