Skip to content

Commit

Permalink
Able to log the NSDictionary and Dictionary value in log file.
Browse files Browse the repository at this point in the history
Updated the pod spec with new version.
  • Loading branch information
vigneshuvi committed Jan 30, 2017
1 parent 64087d2 commit 60b620a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SwiftLoggly.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 43 additions & 0 deletions SwiftLoggly/Loggly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<AnyHashable, Any>) {
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)
}

0 comments on commit 60b620a

Please sign in to comment.