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) +}