> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/smc/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/smc/latest/guides/structured-logging.md).

# Structured logging

SMC event reporting and logging borrows and extends the Problem Details structure
defined in [RFC 7807](https://www.rfc-editor.org/rfc/rfc7807) and [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457).

Event details are defined in YAML files, translated to Erlang source code, and compiled
into the final release. You can modify some per-event settings through the configuration
and an HTTP API. The HTTP API supports the reading of specific or all event definitions.

A custom formatter is included in SMC. Do not modify the provided formatter configuration without assistance.

## Event definition

Events are defined by the developers in YAML files and translated into Erlang source code.
It is not possible to add events at runtime.

At a minimum, events have `cause`, `title`, `detail`, and `documentation` fields.
There are several optional fields; the most notable are `$id` and `logging`. The translator adds a
unique `$id` UUID and `type` URI to all events that don't have one and generates v5 UUIDs
based on the `type` URI. The optional `logging` field defaults to:

```yaml
level: debug
group: unspecified
```

The `title`, `detail`, and `documentation` fields are intended to be used for human-readable
documentation. The `title` and `detail` fields are also included in the `application/problem+json`
responses on HTTP API that are derived from these events.

## Log rate limitation

Some high-level log messages may be important enough to be included in the logs, using the usual Erlang `logger` mechanisms, but may appear very often if their cause affects many user sessions at once. Log rate limitation allows these messages to be present while simultaneously avoiding the saturation of the log stream.

Events can have an optional field named `rateLimit` which specifies the maximum number of messages of this event per second to be logged. To enforce the rate, a logger filter using the provided function `ev_logger_filters:rate_limit/2` must be placed into the logger handler configuration. See the following example:

```Erlang
{kernel,
 [{logger,
   [{handler, problem_details, logger_std_h,
     #{level => all,
       filter_default => stop,
       filters =>
           [{rate_limit, {fun ev_logger_filters:rate_limit/2, ignore}},
            {domain, {fun logger_filters:domain/2, {log,sub,[event]}}}],
       formatter =>
           {ev_logger_formatter,
            #{template =>
                  ["when=", time, " level=", level,
                   {pid, [" pid=", pid], ""},
                   {'$id', [" event_id=", '$id'], ""},
                   {imsi, [" imsi=", imsi], ""},
                   {imei, [" imei=", imei], ""},
                   {msisdn, [" msisdn=", msisdn], ""},
                   {apn, [" apn=", apn], ""},
                   " at=", mfa, ":", line,
                   " msg=", msg, "\n"]
             }
           }
      }]
  }]
}.
```

> [!NOTE]
> Logger filters are applied late in the logging pipeline. The rate-limiting filter cannot be used as a log load regulation or log overload protection.

## Logging events

SMC uses domain logging, as described in the [Erlang documentation](https://www.erlang.org/doc/apps/kernel/logger_filters#domain/2). Events are internally handed as reports to the Erlang [logger](https://www.erlang.org/doc/apps/kernel/logger.html) with the `domain` metadata set to `[event, Group]`, where the value of **Group** is one of:

* `core` for events from the SMC core engine
* `session` for events related to session handling
* `gtp` for GTP-related events
* `diameter` for Diameter-related events
* `radius` for RADIUS-related events
* `upf` for UPG management and PFCP events
* `pcc` for policy and charging events
* `proxy` for proxy functionality events
* `unspecified` for miscellaneous events

> [!NOTE]
> The Erlang default `logger` configuration silently discards all log messages with a log domain set. SMC provides a custom `logger` configuration that enables the `event` log domain and all subdomains beneath it.

Additionally, the **Group** value can be used with the existing per-module logging facilities as described [below](#per-group-logging).

`ev_logger_formatter` formats the events into `logfmt` messages. You can adjust the fields
included in the message through the `template` configuration field.

Here is an example of the final output:

```
when=2024-05-23T10:56:55.553900+00:00 level=debug pid=<0.4542.0> event_id=92f691f6-80af-5957-be38-9a27d2bef8a8 imsi=111111111111111 imei=3520990017614823 msisdn=440000000000 apn=example.net at=pgw_SUITE:create_session_request_pcf_fail/1:2563 msg=policy_context_denied
```

## Logger formatter

SMC includes a custom `ev_logger_formatter` formatter module. Event metadata can be included in a template, similar to the [logger_formatter](https://www.erlang.org/doc/apps/kernel/logger_formatter#t:template/0).

Some metadata values are added by the Erlang logger and others by SMC.
Most of the fields added by default are only interesting for the developers. However, keeping
them in normal logging helps with post-mortem log analysis.

Erlang logger metadata fields:

| Name | Value |
|---|---|
| pid | Identifier of the Erlang process that logged the event |
| time | Event time stamp |
| mfa | Module, function, and arity of the function logging the event |
| file | Source filename of the logger call |
| line | Line number in the source file of the logger call |

SMC-added metadata is subject to availability.

| Name | Value |
|---|---|
| diameter\_app | Diameter application name (Gx, Gy, Rf, NASREQ) |
| diameter\_peer | Origin/destination host name of the peer |
| gtp\_version | GTP version (1, 2) |
| gtp\_c\_peer | IP address of the GTP-C peer |
| gtp\_u\_peer | IP address of the GTP-U peer |
| imsi | IMSI |
| imei | IMEI |
| apn | APN |
| msisdn | MSISDN |
| upf\_node\_ip | IP of the UPF |
| upf\_node\_name | FQDN of the UPF |

## Special procedures for operations

### Per-group logging

The event domain group, as described in the [Logging events](#logging-events) section above, is used as a pseudo-module for [module-level logging](https://www.erlang.org/doc/apps/kernel/logger.html#set_module_level/2) purposes. For example, the following level enables all session-related event logs:

```Erlang
set_module_level(session, debug).
```
