> ## Documentation Index
> Fetch the complete documentation index at: https://primer.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# logger

To gather useful information from the SDK, you can either use default SDK Logger or
implement and assign your own logger that the SDK will use to send log messages.

```swift SWIFT theme={"dark"}
public protocol PrimerLogger {
    var logLevel: LogLevel { get set }
    func log(level: LogLevel, message: String, userInfo: Encodable?, metadata: PrimerLogMetadata)
}
```

Log messages are assigned one of these levels: `debug`, `info`, `warning`, `error`, and `none`.
The default logger has a log level of `.none`.

This allows simple and powerful filtering of log messages so that you can, for example, print
debug messages to the local console and send error messages to an external aggregator.

<Note>
  The default behaviour of the logger is not to send any logs at all unless the
  `DEBUG` compilation symbol is present - this avoids any PCI or PII being
  logged in production reporting tools.
</Note>

### Implementing your own custom logger

To implement a logger, create a class or struct that implements `PrimerLogger`, like so:

```swift SWIFT theme={"dark"}
class ExampleLogger: PrimerLogger {

    private let osLogger = os.Logger()

    var logLevel: LogLevel = .debug

    func log(level: PrimerSDK.LogLevel, message: String, userInfo: Encodable?, metadata: PrimerSDK.PrimerLogMetadata) {
        switch level {
        case .debug:
            osLogger.debug("\(message)")
        case .info:
            osLogger.info("\(message)")
        case .warning:
            osLogger.warning("\(message)")
        case .error:
            osLogger.error("\(message)")
        }
        /* ... Your custom logging logic here ... */
    }
}
```

Then assign an instance to the `logger` property:

```swift SWIFT theme={"dark"}
PrimerLogging.shared.logger = ExampleLogger()
```

If you want to change the logs that you receive based on log level, set the `logLevel` property like so:

```swift SWIFT theme={"dark"}
// Only `warning` and `error` level logs will be sent to your logger
PrimerLogging.shared.logLevel = .warning
```

If you don't want to receive any logs, set the log level to `.none`.

### Default SDK logger

The SDK provides a default logger that will print to the Xcode and/or macOS console using
either `OSLogger` (if available) or `print`.

Note that the default logger has a log level of `none`, so it won't send any logs at all
unless you assign a different level.

You can easily assign `.debug` or any other log level to the default logger by simply calling:

```swift SWIFT theme={"dark"}
PrimerLogging.logger.logLevel = .debug
```

### Logging Parameters

```swift SWIFT theme={"dark"}
func log(level: PrimerSDK.LogLevel, message: String, userInfo: Encodable?, metadata: PrimerSDK.PrimerLogMetadata)
```

<Expandable title="Parameters" defaultOpen>
  <ResponseField name="level" type="LogLevel" required>
    One of '.debug', '.info', 'warning' or 'error'
  </ResponseField>

  <ResponseField name="message" type="String" required>
    The contents of the log message
  </ResponseField>

  <ResponseField name="userInfo" type="Encodable" post={["Optional"]}>
    Contains additional structured information about the log message. For
    example, this might include request headers or a response body when logging
    a network request.
  </ResponseField>

  <ResponseField name="metadata" type="PrimerLogMetadata" required>
    Contains information about the origin of the log including file, function,
    and line number.
  </ResponseField>
</Expandable>
