> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/smc/3.14/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/3.14/config/logging.md).

# Logging

Logging is an important operational aspect. It is required for OPS to get
informed about certain important events. At the same time, too much logging not
only leads to overload of someone who looks at the logs, but it also stresses
the system to create and process these logs.

It is strongly advised to use either `error` or `critical` log level in production
to avoid overloading the system.

| Parameter | Description | Default |
|---|---|---|
| loglevel | Log level. The possible values are `debug`, `info`, `warning`, `error`, and `critical`. | `error` |

## System log levels

 The log level indicates the severity of an event. According to the syslog protocol specified in [RFC 5424](https://datatracker.ietf.org/doc/html/rfc5424), there are eight log levels:

 | Level | Integer | Description |
 | :--- | :--- | :--- |
 | emergency | 0 | System is unusable |
 | alert | 1 | Action must be taken immediately |
 | critical | 2 | Critical conditions |
 | error | 3 | Error conditions |
 | warning | 4 | Warning conditions |
 | notice | 5 | Normal but significant conditions |
 | info | 6 | Informational messages |
 | debug | 7 | Debug-level messages |

 Each log level is mapped to an integer value, and a log event passes the log level check if the integer value of its log level is less than or equal to the currently configured log level.
 That is, the check passes if the event is equally or more severe than the configured level.

### Change the log level in a running erGW node

Follow these steps to change the log level in a running erGW node:

1. Search for the SMC pods in the cluster:

   ```bash
   kubectl get pods --all-namespaces -l app.kubernetes.io/name=smc
   ```

2. Pick the SMC pod for which you want to adjust the log level:

   ```bash
   kubectl exec -it {POD} -n {NAMESPACE} -c smc-ergw /opt/ergw-c-node/bin/ergw-c-node remote_console
   ```

3. Wait a few seconds, access the diagnosis shell, and get the current system log level:

   ```Erlang
   logger:get_primary_config().
   ```

4. Get the current configurations of the `default` logger handler:

   ```Erlang
   logger:get_handler_config(default).
   ```

5. Switch to the other level:

   - First, make the log handler accept the intended log level:

   ```Erlang
   logger:set_handler_config(default, level, <loglevel>).
   ```

   - Set the log level for all modules:

   ```Erlang
   logger:set_primary_config(level, <loglevel>).
   ```

   - Or set it for specific modules only:

   ```Erlang
   logger:set_module_level([mod1, mod2, ...], <loglevel>).
   ```

6. Exit the diagnosis shell using **ctrl+G**, then **q**.

### Add a special DEBUG log sink

You can redirect log messages from the default `stdout` to a file. This is most
useful to write debug logs to a temporary file while keeping the default `stdout` log
free of them to, for example, avoid overloading a log collector. To redirect log messages to a file:

1. Log into a node and access the diagnosis shell as before.

2. Check the current logging handler configuration:

   ```Erlang
   Erlang/OTP 26 [erts-14.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit:ns]

   Eshell V14.2 (press Ctrl+G to abort, type help(). for help)
   1> logger:get_handler_config().
   [#{id => ssl_handler,module => logger_std_h,
      config =>
         #{type => standard_io,burst_limit_enable => true,
            drop_mode_qlen => 200,flush_qlen => 1000,
            sync_mode_qlen => 10,overload_kill_restart_after => 5000,
            burst_limit_max_count => 500,
            burst_limit_window_time => 1000,
            overload_kill_enable => false,
            overload_kill_mem_size => 3000000,
            overload_kill_qlen => 20000,
            filesync_repeat_interval => no_repeat},
      level => debug,
      filters =>
         [{filter_non_ssl,{fun logger_filters:domain/2,
                           {log,sub,[otp,ssl]}}}],
      filter_default => stop,
      formatter => {ssl_logger,#{}}},
   #{id => default,module => logger_std_h,
      config =>
         #{type => standard_io,burst_limit_enable => true,
            drop_mode_qlen => 10000,flush_qlen => 10000,
            sync_mode_qlen => 10000,overload_kill_restart_after => 5000,
            burst_limit_max_count => 500,
            burst_limit_window_time => 1000,
            overload_kill_enable => false,
            overload_kill_mem_size => 3000000,
            overload_kill_qlen => 20000,
            filesync_repeat_interval => no_repeat},
      level => info,
      filters =>
         [{remote_gl,{fun logger_filters:remote_gl/2,stop}},
         {domain,{fun logger_filters:domain/2,
                  {log,super,[otp,sasl]}}},
         {no_domain,{fun logger_filters:domain/2,
                     {log,undefined,[]}}}],
      filter_default => stop,
      formatter =>
         {logger_formatter,#{single_line => true,legacy_header => false,
                              template =>
                                 ["when=",time," level=",level,
                                 {pid,[" pid=",pid],[]},
                                 " at=",mfa,":",line," msg=\"",msg,"\"\n"]}}}]
   ```

   A node with a default logger setup by default has two handlers, one for events
   from Erlang TLS stack (`ssl_handler`) and a default handler (`default`).

3. Add a special debug handler:

   ```Erlang
   logger:add_handler(debug_log_h, logger_std_h,
                     #{config =>
                           #{file => "/tmp/debug.log",
                              filesync_repeat_interval => 1000,
                              sync_mode_qlen => 10000,
                              drop_mode_qlen => 10000,
                              flush_qlen     => 10000},
                        level => debug,
                        formatter =>
                           {logger_formatter,
                           #{
                              single_line => true,
                              legacy_header => false,
                              template =>
                                 ["when=", time, " level=", level,
                                    {pid, [" pid=", pid], ""},
                                    " at=", mfa, ":", line,
                                    " msg=\"", msg, "\"\n"]
                              }
                           }
                     }).
   ```

   This configuration uses the `logger_std_h` handler which appends log
   lines to the given file. You can also use the `logger_disk_log_h` handler which
   supports circular (wrapped) logs:

   ```Erlang
   logger:add_handler(debug_log_h, logger_disk_log_h,
                     #{config =>
                           #{file => "/tmp/debug.log",
                              type => wrap,
                              max_no_files => 4,
                              max_no_bytes => 10000,
                              filesync_repeat_interval => 1000,
                              sync_mode_qlen => 10000,
                              drop_mode_qlen => 10000,
                              flush_qlen     => 10000},
                        level => debug,
                        formatter =>
                           {logger_formatter,
                           #{
                              single_line => true,
                              legacy_header => false,
                              template =>
                                 ["when=", time, " level=", level,
                                    {pid, [" pid=", pid], ""},
                                    " at=", mfa, ":", line,
                                    " msg=\"", msg, "\"\n"]
                              }
                           }
                     }).
   ```

   Adjust `max_no_files` and `max_no_bytes` to your needs.
