Skip to content

Commit

Permalink
Implementing SDKLogger Overrides (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
mliao95 authored Oct 17, 2024
1 parent d455549 commit c36f1b4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [GlobalConfig](#globalconfig)
* [GlobalConfig.init](#globalconfiginit)
* [Updating configuration](#updating-configuration)
* [SDKLogger](#sdklogger)
* [ChatSession APIs](#chatsession-apis)
* [ChatSession Events](#chatsession-events)
* [Classes and Structs](#classes-and-structs)
Expand Down Expand Up @@ -138,6 +139,41 @@ let globalConfig = GlobalConfig(region: .USEast1)
chatSession.configure(config: globalConfig)
```

### SDKLogger
The `SDKLogger` class is responsible for logging relevant runtime information to the console which is useful for debugging purposes. The `SDKLogger` will log key events such as establishing a connection or failures such as failing to send a message.

#### `SDKLogger.configure`
This API will allow you to override the SDK's built-in logger with your own [SDKLoggerProtocol](#sdkloggerprotocol) implementation. This is especially useful in cases where you would want to store logs for debugging purposes. Attaching these logs to issues filed in this project will greatly expedite the resolution process.

```
public static func configureLogger(_ logger: SDKLoggerProtocol) {
SDKLogger.logger = logger
}
```

#### SDKLoggerProtocol
The SDKLoggerProtocol is a protocol used for the `SDKLogger`. Users can override the `SDKLogger` with any class that implements SDKLoggerProtocol.

```
public protocol SDKLoggerProtocol {
func logVerbose(
_ message: @autoclosure () -> String
)
func logInfo(
_ message: @autoclosure () -> String
)
func logDebug(
_ message: @autoclosure () -> String
)
func logFault(
_ message: @autoclosure () -> String
)
func logError(
_ message: @autoclosure () -> String
)
}
```

--------------------

### ChatSession APIs
Expand Down
1 change: 0 additions & 1 deletion Sources/Core/Service/ChatSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public class ChatSession: ChatSessionProtocol {
guard let messageItem = transcriptItem as? Message,
!messageItem.text.isEmpty,
messageItem.messageDirection == .Incoming else {
SDKLogger.logger.logError("Could not send \(eventType.rawValue) receipt for \(String(describing: (transcriptItem as? Message)?.text))")
return
}

Expand Down
17 changes: 10 additions & 7 deletions Sources/Core/Utils/Logger/SDKLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,38 @@ import Foundation
import OSLog


class SDKLogger: SDKLoggerProtocol {
public class SDKLogger: SDKLoggerProtocol {

static let logger = SDKLogger()
static var logger: SDKLoggerProtocol = SDKLogger()
private let osLog: OSLog

private init() {
osLog = OSLog(subsystem: Bundle.main.bundleIdentifier ?? "SDK.DefaultSubsystem", category: "SDKLogging")
}

func logVerbose(_ message: @autoclosure () -> String) {
public func logVerbose(_ message: @autoclosure () -> String) {
os_log("%{public}@", log: osLog, type: .default, message())
}

func logInfo(_ message: @autoclosure () -> String) {
public func logInfo(_ message: @autoclosure () -> String) {
os_log("%{public}@", log: osLog, type: .info, message())
}

func logDebug(_ message: @autoclosure () -> String) {
public func logDebug(_ message: @autoclosure () -> String) {
os_log("%{public}@", log: osLog, type: .debug, message())
}

func logFault(_ message: @autoclosure () -> String) {
public func logFault(_ message: @autoclosure () -> String) {
os_log("%{public}@", log: osLog, type: .fault, message())
}

func logError(_ message: @autoclosure () -> String) {
public func logError(_ message: @autoclosure () -> String) {
os_log("%{public}@", log: osLog, type: .error, message())
}

public static func configureLogger(_ logger: SDKLoggerProtocol) {
SDKLogger.logger = logger
}
}


Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/Utils/Logger/SDKLoggerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import Foundation

protocol SDKLoggerProtocol {
public protocol SDKLoggerProtocol {
func logVerbose(
_ message: @autoclosure () -> String
)
Expand Down

0 comments on commit c36f1b4

Please sign in to comment.