Skip to content

Commit 05ef9cf

Browse files
committed
Added the rich function for log tracking type.(Info, Warn, Error)
Able to print the logs in different colour based on log type.
1 parent 347aca9 commit 05ef9cf

2 files changed

Lines changed: 67 additions & 13 deletions

File tree

SwiftLoggly.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "SwiftLoggly"
19-
s.version = "0.6.0"
19+
s.version = "0.6.1"
2020
s.summary = "Simple way to logging with rich feature framework in Swift."
2121

2222
# This description is used to generate tags and improve search results.

SwiftLoggly/Loggly.swift

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ extension NSDictionary {
4848
}
4949
}
5050

51+
// Struct for Color Log
52+
struct ColorLog {
53+
static let ESCAPE = "\u{001b}["
54+
55+
static let RESET_FG = ESCAPE + "fg;" // Clear any foreground color
56+
static let RESET_BG = ESCAPE + "bg;" // Clear any background color
57+
static let RESET = ESCAPE + ";" // Clear any foreground or background color
58+
59+
static func red<T>(object: T) {
60+
print("\(ESCAPE)fg255,0,0;\(object)\(RESET)", terminator: "")
61+
}
62+
63+
static func green<T>(object: T) {
64+
print("\(ESCAPE)fg0,255,0;\(object)\(RESET)", terminator: "")
65+
}
66+
67+
static func blue<T>(object: T) {
68+
print("\(ESCAPE)fg0,0,255;\(object)\(RESET)", terminator: "")
69+
}
70+
}
71+
72+
// Public Enumaration for log type
73+
public enum LogType {
74+
case LogInfo
75+
case LogError
76+
case LogWarn
77+
}
78+
5179
open class Loggly {
5280

5381
///The max size a log file can be in Kilobytes. Default is 1024 (1 MB)
@@ -86,8 +114,34 @@ open class Loggly {
86114
return formatter
87115
}
88116

117+
///gets the log type with String
118+
func logTypeName(_ type: LogType) -> String {
119+
var logTypeStr = "";
120+
switch type {
121+
case .LogInfo:
122+
logTypeStr = "Info - "
123+
case .LogWarn:
124+
logTypeStr = "Warn - "
125+
case .LogError:
126+
logTypeStr = "Error - "
127+
}
128+
return logTypeStr;
129+
}
130+
131+
/// Prints the log type with String and type color code.
132+
func printLog(_ type: LogType, text:String) {
133+
switch type {
134+
case .LogInfo:
135+
ColorLog.blue(object: text)
136+
case .LogWarn:
137+
ColorLog.blue(object: text)
138+
case .LogError:
139+
ColorLog.red(object: text)
140+
}
141+
}
142+
89143
///write content to the current log file.
90-
open func write(_ text: String) {
144+
open func write(_ type: LogType, text: String) {
91145
let path = "\(directory)/\(logName(0))"
92146
let fileManager = FileManager.default
93147
if !fileManager.fileExists(atPath: path) {
@@ -98,11 +152,11 @@ open class Loggly {
98152
}
99153
if let fileHandle = FileHandle(forWritingAtPath: path) {
100154
let dateStr = dateFormatter.string(from: Date())
101-
let writeText = "[\(dateStr)]: \(text)\n"
155+
let writeText = "[\(logTypeName(type)) \(dateStr)]: \(text)\n"
102156
fileHandle.seekToEndOfFile()
103157
fileHandle.write(writeText.data(using: String.Encoding.utf8)!)
104158
fileHandle.closeFile()
105-
print(writeText, terminator: "")
159+
printLog(type, text: text)
106160
cleanup()
107161
}
108162
}
@@ -177,17 +231,17 @@ open class Loggly {
177231

178232
}
179233

180-
///a free function to make writing to the log much nicer
181-
public func loggly(_ text: String) {
182-
Loggly.logger.write(text)
234+
///a free function to make writing to the log with Log type
235+
public func loggly(_ type: LogType, text: String) {
236+
Loggly.logger.write(type, text: text)
183237
}
184238

185-
///a free function to make writing to the log much nicer
186-
public func loggly(_ dictionary: Dictionary<AnyHashable, Any>) {
187-
Loggly.logger.write(dictionary.jsonString)
239+
///a free function to make writing to the log with Log type
240+
public func loggly(_ type: LogType, dictionary: Dictionary<AnyHashable, Any>) {
241+
Loggly.logger.write(type, text: dictionary.jsonString)
188242
}
189243

190-
///a free function to make writing to the log much nicer
191-
public func loggly(_ dictionary: NSDictionary) {
192-
Loggly.logger.write(dictionary.jsonString)
244+
///a free function to make writing to the log with Log type
245+
public func loggly(_ type: LogType, dictionary: NSDictionary) {
246+
Loggly.logger.write(type, text: dictionary.jsonString)
193247
}

0 commit comments

Comments
 (0)