Skip to content

Commit

Permalink
Add default logging config example
Browse files Browse the repository at this point in the history
As requested in review, this begins by documenting how to replicate
NAV's default logging config, and proceeds to document Humio only after
that.
  • Loading branch information
lunkwill42 committed Sep 7, 2023
1 parent ad3b47e commit 43cabfa
Showing 1 changed file with 51 additions and 22 deletions.
73 changes: 51 additions & 22 deletions doc/howto/setting-up-logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,50 @@ extensive knowledge of how the standard Python logging framework works.
and parsed. This means that :file:`logging.yml` must adhere to the
configuration dictionary schema laid out in the Python docs.

This can allow you to do such things as sending all NAV log records to a Falcon
LogScale (previously known as Humio) ingestor using something like the
`humiologging <https://pypi.org/project/humiologging/>`_ library. Instead of
shipping the file-based logs to LogScale and having them parsed there, each log
record can be shipped with structured attributes/tags.
Be aware that by adding configuration to :file:`logging.yml`, you are altering
NAV's default logging configuration at a very low level, and you may also be
altering NAV's default behavior of storing logs in files. A :file:`logging.yml`
that replicates a default NAV setup may look something like this:

.. code-block:: yaml
version: 1
loggers:
nav:
level: INFO
root:
handlers: [console]
formatters:
default:
format: '%(asctime)s [%(levelname)s] [%(name)s] %(message)s'
handlers:
console:
class: logging.StreamHandler
formatter: default
This replicates a setup that logs only **INFO**-level messages and above from
NAV to ``stderr``, using NAV's default log message format. Individual NAV
daemons will redirect their ``stderr`` streams to their respective log files as
they fork off background processes, so there is no need to redefine these.

Leaving out the :class:`logging.StreamHandler` will still cause the log files
to be created, but they will be empty (save for any outpout to ``stderr`` that
did not come from the :mod:`logging` library).

.. tip:: As with :file:`logging.conf`, processes can be directed to read a
bespoke :file:`logging.yml` file, but by setting the
:envvar:`NAV_LOGGING_YML` environment variable instead.

Example: Directing logs to Falcon LogScale (Humio)
--------------------------------------------------

The following example shows how you can make all NAV programs ship their log
messages to a Falcon LogScale (previously known as Humio) ingestor using
something like the `humiologging <https://pypi.org/project/humiologging/>`_
library. Instead of shipping the file-based logs to LogScale and having them
parsed there, each log record can be shipped with structured attributes/tags.

To achieve something like this, you need to first install the
:mod:`humiologging` library into your NAV installation's Python environment
Expand All @@ -158,6 +197,10 @@ similar to this:
root:
handlers: [humio, console]
formatters:
default:
format: '%(asctime)s [%(levelname)s] [%(name)s] %(message)s'
handlers:
humio:
class: humiologging.handlers.HumioJSONHandler
Expand All @@ -169,25 +212,11 @@ similar to this:
formatter: default
formatters:
default:
format: '%(asctime)s [%(levelname)s] [%(name)s] %(message)s'
This configuration attaches a :class:`HumioJSONHandler` to the ``root`` logger
and sets the global NAV log level to **DEBUG**. Unfortunately, as this
configuration manipulates the ``root`` logger, it removes the handler(s) that
NAV has by default installed on it, so if you want NAV to also keep logging to
files in addition to Humio, you need to add an extra handler that logs to a
stream (``stderr`` by default), and you need to specify a format for it. This
example just redefines the log line format NAV uses by default.
files in addition to Humio, you need to replicate parts of NAV's default setup,
as mentioned in the previous section. Add an extra handler named ``console``
that logs to a stream (``stderr`` by default), and specify a format for it.

As long as you add a :class:`logging.StreamHandler` to ``root``, you shouldn't
need to redefine which files the logs go to, as most NAV daemons achieve this
by redirecting their ``stderr`` to a file as they fork off a background
process. Leaving out the :class:`logging.StreamHandler` will still cause the
log files to be created, but they will be empty (save for any outpout to
``stderr`` that did not come from the :mod:`logging` library).

.. tip:: As with :file:`logging.conf`, processes can be directed to read a
bespoke :file:`logging.yml` file, but by setting the
:envvar:`NAV_LOGGING_YML` environment variable instead.

0 comments on commit 43cabfa

Please sign in to comment.