Logger System / Flash Log / Crash Log on Nordic nRF5 SDK

Flash logger backend

The flash logger backend is used to store logs in non-volatile memory.

The backend consists of the following logical parts:

  • flashlog – used to store log entries during application lifetime,
  • crashlog – used to dump messages logged before an error condition (NRF_LOG_FINAL_FLUSH) and between NRF_LOG_FINAL_FLUSH and reset.

From the logger perspective, these parts are seen as independent logger backends. However, internally they share the same scheme of storing and reading log entries.

Flash operations

The module uses dedicated flash pages that are configured in SDK configuration header file. Exact pages can be specified, or pages following the application code can be used (NRF_LOG_BACKEND_FLASH_START_PAGE set to 0). The module uses Flash storage (fstorage) to store the logs.

The flashlog uses the Flash storage (fstorage) asynchronous API provided during intialization, while the crashlog reinitializes Flash storage (fstorage) to work in a direct, synchronous way.

Log entries are serialized and stored in a dedicated area one by one until there is no more space for them. Logs are dropped if there is no space available and the area must be explicitly erased by using nrf_log_backend_flash_erase or a CLI command. On initialization, the flash area is scanned to find the last entry and the following entries are stored after it.NoteStrings that are used as an argument to the log message and are stored in RAM (for example, when NRF_LOG_PUSH is used) will be corrupted, since only the address of the string is stored.

Flashlog operations

Once the flashlog backend is initialized and enabled, it receives log messages that match the filtering configured for this backend. Flashlog attempts to write messages to flash by using asynchronous Flash storage (fstorage) API. Since access to flash is time-consuming and the area is limited, improper logger filtering configuration can lead to system overload. It is recommended to store only warning and error messages. When flashlog is used, it is recommended to increase NRF_LOG_MSGPOOL_ELEMENT_COUNT. This is related to the fact that flashlog keeps a log message until it is written to flash.

Crashlog operations

In a standard operation, the crashlog keeps a FIFO of the most recent log entries that match the filtering. Whenever a new log message is put into the FIFO, the oldest one is freed. Crashlog does not perform any flash operation until panic occurs (NRF_LOG_FINAL_FLUSH). When panic occurs, Flash storage (fstorage) is reconfigured to synchronous mode and all logs from the FIFO are written to flash memory. Any log messages received in panic mode are directly written to flash. When crashlog is used, NRF_LOG_MSGPOOL_ELEMENT_COUNT must be increased at least by NRF_LOG_BACKEND_CRASHLOG_FIFO_SIZE, compared to the default value.

How to use the flash log / crash log

Step 1:

Add the flash log / crash log configuration inside the SDK_CONFIG.h (refer to the examples\ble_peripheral\experimental\ble_app_cli).

// <e> NRF_LOG_BACKEND_FLASH_ENABLED - nrf_log_backend_flash - Log flash backend
// <q> NRF_LOG_BACKEND_FLASH_CLI_CMDS  - Enable CLI commands for this module.
// <e> NRF_LOG_BACKEND_CRASHLOG_ENABLED - Enable crashlog backend.

Configure the flash page address

// <h> Flash log location - Configuration of flash area used for storing the logs.

// <o> NRF_LOG_BACKEND_FLASH_START_PAGE - Starting page.  
// <i> If 0, then pages directly after the application are used.


// <o> NRF_LOG_BACKEND_PAGES - Number of pages. 

Step 2:

Add the file (nrf_log_backend_flash.c) inside the Segger Embedded Studio project

Step 3:

Declare the variables of m_flash_log_backend and m_crash_log_backend

Step 4:

Initialize the flash log module

After running on the flash log, it would have the flash log command on the CLI manual.

If you have any comments, welcome to leave here.

2 thoughts on “Logger System / Flash Log / Crash Log on Nordic nRF5 SDK

  1. Hi, when reading your page on PC some of the page is missing – all the crashlog part is seen only when browsing from iphone


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.