From 60b620a84bcc313c9a24bfdcea4f93919d65b681 Mon Sep 17 00:00:00 2001 From: Vignesh Kumar Date: Mon, 30 Jan 2017 20:44:55 +0530 Subject: [PATCH] Able to log the NSDictionary and Dictionary value in log file. Updated the pod spec with new version. --- SwiftLoggly.podspec | 2 +- SwiftLoggly/Loggly.swift | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/SwiftLoggly.podspec b/SwiftLoggly.podspec index 32178bd..12f0596 100644 --- a/SwiftLoggly.podspec +++ b/SwiftLoggly.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "SwiftLoggly" - s.version = "0.5.9" + s.version = "0.6.0" s.summary = "Simple way to logging with rich feature framework in Swift." # This description is used to generate tags and improve search results. diff --git a/SwiftLoggly/Loggly.swift b/SwiftLoggly/Loggly.swift index d83d268..88fc2e5 100644 --- a/SwiftLoggly/Loggly.swift +++ b/SwiftLoggly/Loggly.swift @@ -15,6 +15,39 @@ extension String { } } +extension Dictionary { + var jsonString: String { + let invalidJson = "Not a valid JSON" + do { + let jsonData = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted) + return String(bytes: jsonData, encoding: String.Encoding.utf8) ?? invalidJson + } catch { + return invalidJson + } + } + + func printJson() { + print(jsonString) + } +} + + +extension NSDictionary { + var jsonString: String { + let invalidJson = "Not a valid JSON" + do { + let jsonData = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted) + return String(bytes: jsonData, encoding: String.Encoding.utf8) ?? invalidJson + } catch { + return invalidJson + } + } + + func printJson() { + print(jsonString) + } +} + open class Loggly { ///The max size a log file can be in Kilobytes. Default is 1024 (1 MB) @@ -148,3 +181,13 @@ open class Loggly { public func loggly(_ text: String) { Loggly.logger.write(text) } + +///a free function to make writing to the log much nicer +public func loggly(_ dictionary: Dictionary) { + Loggly.logger.write(dictionary.jsonString) +} + +///a free function to make writing to the log much nicer +public func loggly(_ dictionary: NSDictionary) { + Loggly.logger.write(dictionary.jsonString) +}