Skip to content

Commit

Permalink
Move log methods to Log class. Leave configuration on SLLog level
Browse files Browse the repository at this point in the history
  • Loading branch information
Shial committed Nov 14, 2017
1 parent f289e9e commit 8b47d7e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 51 deletions.
52 changes: 28 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,25 @@ On top of your file import:
import SLLog
```

### 2 Initialize
### 2 Usage

Log any information you need with `Log` class
```swift
Log.d("ABC")
Log.w("#%^$&@")
Log.e("1233")
```
Any object.
```swift
SLLog.addHandler(SLLogConsole())
Log.v(123)
Log.i("ABC")
Log.d("@$#!^%")
Log.w(Date())
Log.e([0.1,1,"A",Date()])
```

### 3 Initialize

Setup SLLoger
```swift
Expand Down Expand Up @@ -74,24 +92,6 @@ then add it to SLLog
SLLog.addHandler(MyHandler())
```

### 3 Usage

Log any information you need.
```swift
SLLog.d("ABC")
SLLog.w("#%^$&@")
SLLog.e("1233")
```
Any object.
```swift
SLLog.addHandler(SLLogConsole())
SLLog.v(123)
SLLog.i("ABC")
SLLog.d("@$#!^%")
SLLog.w(Date())
SLLog.e([0.1,1,"A",Date()])
```

### 4 Providers

SLLog can have custom providers. To add your own provider
Expand Down Expand Up @@ -126,19 +126,23 @@ Use isTerminal property to choose between terminal or console settings.

By default, logs are set for a terminal.
log format `":d :t :f::l :m"` where
`:d` is replaced in string to display date
`:t` is replaced by log type
`:f` is replaced by file name
`:l` is replaced by line number
`:m` is replaced by message object

`:d` is replaced in string to display date
`:t` is replaced by log type
`:f` is replaced by file name
`:l` is replaced by line number
`:m` is replaced by message object

Default date format used by logger is as follow `"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"`

To get rid of default log colors pass empty dictionary in `logColors` or dictionary with your own definitions.
For more information about terminal color take a look: `https://misc.flogisoft.com/bash/tip_colors_and_formatting`

### LogColor

LogColor is defined as follow `init(_ terminal: String, _ console: String)`
For XCode console emoticons are used as collor.

```swift
SLLog.LogType.verbose:LogColor(TerminalColor.lightGray, "☑️"),
SLLog.LogType.info:LogColor(TerminalColor.lightCyan, "Ⓜ️"),
Expand Down
24 changes: 13 additions & 11 deletions Sources/SLLog/SLLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public protocol LogHandler {
}

public class SLLog {
private static var providers: [LogProvider] = []
private static var targets: [LogHandler] = []
private static var dateFormat: DateFormatter = DateFormatter(dateFormat: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
timeZone: "UTC",
locale: "en_US_POSIX")
public private(set) static var providers: [LogProvider] = []
public private(set) static var targets: [LogHandler] = []
public private(set) static var dateFormat: DateFormatter = DateFormatter(dateFormat: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
timeZone: "UTC",
locale: "en_US_POSIX")

fileprivate class func send(level: SLLog.LogType, spot: Occurrence, message: @autoclosure () -> Any) {
let object = message()
Expand Down Expand Up @@ -74,24 +74,26 @@ public extension SLLog {
case warning = 3
case error = 4
}

}

public final class Log {
public class func v(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) {
send(level: .verbose, spot: (file, line), message: message)
SLLog.send(level: .verbose, spot: (file, line), message: message)
}

public class func i(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) {
send(level: .info, spot: (file, line), message: message)
SLLog.send(level: .info, spot: (file, line), message: message)
}

public class func d(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) {
send(level: .debug, spot: (file, line), message: message)
SLLog.send(level: .debug, spot: (file, line), message: message)
}

public class func w(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) {
send(level: .warning, spot: (file, line), message: message)
SLLog.send(level: .warning, spot: (file, line), message: message)
}

public class func e(_ message: @autoclosure () -> Any, _ file: String = #file, _ line: UInt = #line) {
send(level: .error, spot: (file, line), message: message)
SLLog.send(level: .error, spot: (file, line), message: message)
}
}
20 changes: 10 additions & 10 deletions Tests/SLLogTests/SLLogConsoleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ class SLLogConsoleTests: XCTestCase {
func testLogs() {
SLLog.clearHandlers()
SLLog.addHandler(SLLogConsole(isTerminal: false))
SLLog.v(123)
SLLog.i("ABC")
SLLog.d("@$#!^%")
SLLog.w(Date())
SLLog.e([0.1,1,"A",Date()])
Log.v(123)
Log.i("ABC")
Log.d("@$#!^%")
Log.w(Date())
Log.e([0.1,1,"A",Date()])
XCTAssert(true)
}

func testTerminal() {
SLLog.clearHandlers()
SLLog.addHandler(SLLogConsole(isTerminal: true))
SLLog.v(123)
SLLog.i("ABC")
SLLog.d("@$#!^%")
SLLog.w(Date())
SLLog.e([0.1,1,"A",Date()])
Log.v(123)
Log.i("ABC")
Log.d("@$#!^%")
Log.w(Date())
Log.e([0.1,1,"A",Date()])
XCTAssert(true)
}
}
12 changes: 6 additions & 6 deletions Tests/SLLogTests/SLLogFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SLLogFileTests: XCTestCase {
let exp = expectation(description: "logToFile")

SLLog.addHandler(try! SLLogFile(path))
SLLog.w("#%^$&@")
Log.w("#%^$&@")

DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 2) {
if FileManager.default.fileExists(atPath: "\(self.path)/sllogs/\(self.dateFormat.string(from: Date())).log") {
Expand All @@ -59,9 +59,9 @@ class SLLogFileTests: XCTestCase {
let exp = expectation(description: "logToFile")

SLLog.addHandler(try! SLLogFile(path))
SLLog.d("ABC")
SLLog.w("#%^$&@")
SLLog.e("1233")
Log.d("ABC")
Log.w("#%^$&@")
Log.e("1233")

DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 6) {
let filePath = "\(self.path)/sllogs/\(self.dateFormat.string(from: Date())).log"
Expand Down Expand Up @@ -89,7 +89,7 @@ class SLLogFileTests: XCTestCase {
let exp = expectation(description: "logToFile")

SLLog.addHandler(try! SLLogFile(path))
SLLog.w("#%^$&@")
Log.w("#%^$&@")

DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 6) {
let filePath = "\(self.path)/sllogs/\(self.dateFormat.string(from: Date())).log"
Expand Down Expand Up @@ -158,7 +158,7 @@ class SLLogFileTests: XCTestCase {

XCTAssert((try? FileManager.default.contentsOfDirectory(atPath: "\(self.path)/sllogs/"))?.count ?? 0 > 3)
SLLog.addHandler(try! SLLogFile(path))
SLLog.w("test")
Log.w("test")
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 4) {
let count = (try? FileManager.default.contentsOfDirectory(atPath: "\(self.path)/sllogs/"))?.count ?? 0
XCTAssertTrue(count == 4, "count: \(count)")
Expand Down

0 comments on commit 8b47d7e

Please sign in to comment.