Introducing a new way to review your PRs.

Read the announcement

capcom6 / android-sms-gateway

The SMS Gateway for Android™ app enables sending and receiving SMS messages through an API that can be accessed directly on the device or via a cloud server when direct device access is not possible.

View on GitHub

20 documents · Updated Nov 20, 2026

docs / operational-logs

Operational logs and retention

Operational logs give the Android gateway a local record of events and errors from its main services. The app keeps these records for troubleshooting, shows the newest entries in Settings, and exposes a time-bounded view through the local API. Separate cleanup jobs remove old operational logs and old message or inbox records according to their own settings.

Operational log lifecycle

Log record model

An operational log entry records a severity, the producing module, a human-readable message, an optional structured context, and its creation time. The severity values are DEBUG, INFO, WARN, and ERROR; the record also has a local numeric ID that gives each entry a stable identity. LogEntry in app/…/LogEntry.kt defines this durable shape.

The context is optional JSON. Services use it to attach event data or failure details without changing the main message fields. This makes a log useful for diagnosis while allowing different gateway areas to add different supporting data. LogsUtils provides common context shapes for Android intents and exceptions (app/…/LogsUtils.kt• L6-19).

References: app/…/LogEntry.kt• L8-23 app/…/LogsUtils.kt• L6-19

Log write and query paths

LogsService is the main service boundary for operational logs. Gateway services call it to write entries, while readers can request entries for a time range; missing range limits mean all records from the start of time through the current time. The service converts optional context values to JSON before it stores them (app/…/LogsService.kt• L10-14, app/…/LogsService.kt• L24-48).

The log store supports two reader views. A period query returns entries between two timestamps. A live query returns the 50 newest entries by ID, and the repository applies change filtering before the view model exposes that stream. The data access layer also owns insertion and deletion by time (app/…/LogEntriesDao.kt• L8-20, app/…/LogsRepository.kt• L6-10).

When a log insert fails, the service prints the error and does not pass the failure back to the caller. Logging therefore remains a diagnostic aid and does not normally stop the gateway operation that tried to record the event (app/…/LogsService.kt• L34-51).

The normal write and read relationships are:

flowchart LR
    A[Gateway services and workers] --> B[LogsService]
    B --> C[LogEntriesDao]
    C --> D[(Local log store)]
    D --> E[LogsRepository]
    E --> F[LogsViewModel]
    F --> G[Settings log screen]
    B --> H[Local API log route]

References: app/…/LogsService.kt• L10-59 app/…/LogEntriesDao.kt• L8-20 app/…/LogsRepository.kt• L6-10 app/…/LogsViewModel.kt• L8-12

Log access

Settings log screen

The Settings area provides a log view for quick local troubleshooting. LogsListFragment observes the view model and submits each update to the list adapter, so a new entry appears without a manual refresh; the list moves to the newest position after an update (app/…/LogsListFragment.kt• L17-35).

Each row shows the entry date and time, message, module, and a severity icon. The message is limited to two display lines, and the screen is intentionally limited to the latest 50 records by the data query. The screen does not present the structured context field (app/…/LogItemsAdapter.kt• L14-49, app/…/item_log_entry.xml• L11-57).

References: app/…/LogsListFragment.kt• L17-54 app/…/LogItemsAdapter.kt• L14-49 app/…/fragment_logs.xml• L2-13 app/…/item_log_entry.xml• L11-57

Local API log access

The local server exposes operational logs through its log route for authorized API clients. Clients can provide ISO date-time values in from and to; the route passes the resulting time range to LogsService and maps each stored entry to an API response. A request without these values uses the service defaults. Access requires the LogsRead scope, and invalid date input returns a bad-request response (app/…/LogsRoutes.kt• L14-39).

The API response preserves the severity, module, message, ID, structured context, and creation time. It represents the stored millisecond timestamp as a date value for API consumers (app/…/GetLogsResponse.kt• L7-23). This path is broader than the Settings screen because it reads a selected period rather than only the newest 50 entries.

References: app/…/LogsRoutes.kt• L24-39 app/…/GetLogsResponse.kt• L7-23 app/…/LogsService.kt• L24-32

Retention and cleanup

Operational log retention

The root Settings screen exposes logs.lifetime_days as the operational log retention setting. A positive number means that entries older than that many days are eligible for deletion. The log settings object uses 30 days when it first migrates an older or empty settings store, and it treats missing or non-positive values as no active retention period (app/…/LogsSettings.kt• L8-31).

The setting can also move through the settings export and import path. Imports accept a positive whole-day value or clear the setting; a value below one day is rejected. SettingsService includes the logs settings under the logs group when it serves the wider settings API (app/…/LogsSettings.kt• L41-62, app/…/SettingsService.kt• L25-37).

References: app/…/root_preferences.xml• L49-59 app/…/LogsSettings.kt• L8-31 app/…/LogsSettings.kt• L41-62 app/…/SettingsService.kt• L25-37

Retention scope separation

The app has other lifetime settings that do not control the operational log table. The messages setting controls message-processing log records, while the inbox setting controls stored incoming messages and their related attachments. Each setting is shown in its own feature area and is checked by that feature’s service (app/…/messages_preferences.xml• L13-18, app/…/inbox_preferences.xml• L12-16).

This separation matters during troubleshooting. Increasing logs.lifetime_days preserves operational events, but it does not preserve message or inbox records if their own positive lifetime settings remove them. The three cleanup paths use separate workers and separate service methods (app/…/MessagesService.kt• L249-253, app/…/IncomingMessagesService.kt• L142-146).

References: app/…/messages_preferences.xml• L13-18 app/…/inbox_preferences.xml• L12-16 app/…/MessagesService.kt• L249-253 app/…/IncomingMessagesService.kt• L133-146

Scheduled truncation workers

The operational log cleanup worker runs once per day when the logs service starts. It keeps one unique periodic WorkManager task and requires that the device battery is not low. Each run deletes operational log entries older than the current retention boundary. If cleanup fails, the worker writes an error entry and asks WorkManager to retry (app/…/LogsService.kt• L16-22, app/…/TruncateWorker.kt• L16-56).

Message and inbox cleanup follow the same daily, battery-aware scheduling pattern, but they call their own feature services. Inbox cleanup also removes attachments linked to expired incoming records. These workers start and stop with their owning services, so a feature’s lifecycle controls whether its cleanup task remains scheduled (app/…/LogTruncateWorker.kt• L15-52, app/…/IncomingLogTruncateWorker.kt• L16-55, app/…/IncomingMessagesService.kt• L133-146).

The cleanup boundary is calculated as the current time minus the configured number of 24-hour periods. A disabled or missing lifetime causes the owning cleanup method to return without deleting records (app/…/LogsService.kt• L54-59, app/…/MessagesService.kt• L249-253, app/…/IncomingMessagesService.kt• L142-146).

References: app/…/LogsService.kt• L16-22 app/…/LogsService.kt• L54-59 app/…/TruncateWorker.kt• L16-56 app/…/LogTruncateWorker.kt• L15-52 app/…/IncomingLogTruncateWorker.kt• L16-55

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the stored LogEntry fields aligned with GetLogsResponse and with the fields shown in the Settings row. A new stored field is not automatically visible in both access paths.
  • Keep the logs.lifetime_days preference, LogsSettings import/export key, and the scheduled worker on the same retention contract. A key mismatch can make a setting look changed while cleanup uses a different value.
  • Keep the three lifetime settings separate. Changes to message or inbox cleanup must not change the retention meaning of operational logs.
  • Keep the API date parsing and LogsService.select time units aligned. The route parses dates to epoch milliseconds before it queries the store.
  • Preserve unique worker names when changing scheduling. They define which daily cleanup task is started or stopped.

Important failure modes

TriggerConsequenceGuard
logs.lifetime_days is missing or non-positiveOperational log cleanup returns without deleting old entriesSet a positive retention value and allow the daily worker to run
The device battery is lowWorkManager delays the daily cleanup taskThe worker keeps the task scheduled and runs when the battery constraint is met
Operational log cleanup throws an exceptionThe failure is recorded as an error log and the worker requests a retryCheck the worker result and the log store when old entries remain
A log insert throws an exceptionThe diagnostic entry is lost, but the calling gateway operation continuesUse the caller’s own error path; do not treat logging as durable business state
An API client lacks LogsRead or sends an invalid dateThe route does not return the requested log data; invalid input returns a bad requestGrant the required scope and send valid ISO date-time values
The Settings screen is used to inspect older than the newest 50 entriesThe screen does not show those entries even if retention keeps themUse the local API with a time range for a wider view

Key files

FileWhy to read it
app/…/LogsService.ktMain boundary for log writes, period queries, service lifecycle, and operational retention
app/…/LogEntry.ktStored log record shape and severity values
app/…/LogEntriesDao.ktPersistent query views, newest-entry limit, and time-based deletion contract
app/…/LogsRepository.ktRepository stream used by the local log screen
app/…/LogsViewModel.ktView-model boundary between stored entries and the Settings UI
app/…/LogsListFragment.ktScreen lifecycle and live list updates
app/…/LogItemsAdapter.ktUser-facing mapping from a log entry to date, message, module, and severity
app/…/TruncateWorker.ktDaily operational log cleanup schedule and retry behavior
app/…/LogsRoutes.ktAuthorized local API access and date-range handling
app/…/LogsSettings.ktOperational log retention value and settings migration
app/…/LogTruncateWorker.ktSeparate daily cleanup path for message-processing records
app/…/IncomingLogTruncateWorker.ktSeparate daily cleanup path for incoming records and attachments

Parent: User interface and operations
Related: Notifications