ContentstackLogger

View as Markdown

ContentstackLogger

ContentstackLogger controls the log messages of the Swift Delivery SDK. It is an enum with two static properties, so you set them on the type and never create an instance. Use it to

  • turn the request log on while you debug, or turn all logging off
  • send the messages of the SDK to your own logging system

The settings are global. They apply to every stack in the app from the moment you set them.

Class-Level Properties

PropertyTypeAccessDefaultDescription
logTypeContentstackLogger.LogTypeRead/write (static).nsLog on iOS, tvOS, watchOS and macOS, .print elsewhereWhere the SDK writes its log messages.
logLevelContentstackLogger.LogLevelRead/write (static).errorHighest level of message that the SDK logs.

Class-Level Notes

What the SDK logs.

  • At .error, the SDK logs the three kinds of message below.
    • Each API error, with its request URL and, when the SDK can read the error body, the status code and error message.
    • Each transport error, such as a lost connection, with its request URL.
    • Each async call that serves cacheThenNetwork as cacheElseNetwork.
  • At .info, the SDK also logs each successful request with its status code and URL.
  • Each message starts with [Contentstack] , and an error message continues with Error: .
  • Request URLs include the environment and the query parameters. The SDK does not log the API key or the delivery token, which travel in headers.

LogLevel

ContentstackLogger.LogLevel sets the detail level of the log. The SDK logs a message when its level is at or below logLevel. The table below lists every case.

CaseRaw valueMeaning
none0Logs nothing.
error1Logs errors only. The default.
info2Logs errors and every successful request.

LogType

ContentstackLogger.LogType sets where the SDK writes its messages. It has no raw values. The table below lists every case.

CaseMeaning
printWrites with the Swift print function.
nsLogWrites with NSLog.
custom(CustomLogger)Passes each message to the object you give.

CustomLogger

CustomLogger is the protocol of the object that .custom takes. Its one requirement is func log(message: String). The SDK calls it once for each message that passes the logLevel filter, with the full message text.

Method Index

ContentstackLogger has no public methods. The properties table above lists its settings.

Class-Level Snippet

The snippet below sends every SDK message, including successful requests, to a custom logger.

Log every request through your own logger

import ContentstackSwift

struct AppLogger: CustomLogger {
    func log(message: String) {
        // Forward the message to your logging system.
        print("APP LOG: \(message)")
    }
}

ContentstackLogger.logLevel = .info
ContentstackLogger.logType = .custom(AppLogger())

let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
    .fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
        switch result {
        case .success(let entry):
            print(entry.title)
        case .failure(let error):
            if let apiError = error as? APIError {
                print("Error \(apiError.errorCode): \(apiError.errorMessage)")
            } else {
                print(error)
            }
        }
    }