Logger Store

How to use Pulse logger store

With LoggerStore, you have full access to all of the stored messages.

Default Store #

By default, all of the messages are stored in the default store.

Managing Messages #

Accessing Messages #

You have complete access to the stored messages using the store container (which is a NSPersistentContainer). There are also two convenience APIs for fetching all messages.

func allMessages() throws -> [LoggerMessageEntity] 
func allNetworkRequests() throws -> [LoggerNetworkRequestEntity] 

To learn more about the entities, see the API reference:

Accessing Blobs #

The request and response bodies are stored separately in a file system. You can access them using getData(forKey:) method.

LoggerStore.default.getData(forKey: entity.responseBodyKey)

Blob store has optimization in place where responses automatically get deduplicated. It calculates a cryptographic hash (sha256) for each response. If two responses have the same hash, only one blob gets stored. This is great especially if the app keeps fetching the resource that doesn’t change over and over again.

Removing Messages #

You can use removeAll() to remove all messages.

LoggerStore.default.removeAll()

Size Limits #

The default database​Size​Limit is 30 MB. The limit is approximate. The default blobSizeLimit is 200 MB. The store periodically sweeps the store to reduce its size so that it doesn’t exceed the specified limits. You can change the limits at any time.

Export #

You can use copy(to:) method to export all of the logged messages along with the blob files to a separate file (Pulse document with .pulse extension). The document is optimized for storage (the contents are compressed).

// The destination directory must already exist.
// But If the file at the destination URL already exists, throws an error.
let storeUrl = ...
try? LoggerStore.default.copy(to: storeUrl)

You can learn more about the pulse store in “What’s a Document”.

Import #

To read a Pulse document, use LoggerStore initializer.

let storeUrl = ...
let store = try? LoggerStore(storeURL:)

When you open an existing Pulse document, the store is readonly. But you can also pass a .create option to create your own readwrite store to use instead of the default one. In practice, this should not be necessary.