Skip to content

Python based 5GTANGO JSON logging

Manuel Peuster edited this page Nov 23, 2018 · 2 revisions

Library

This library implements a custom Python logger to produce logs in the 5GTANGO JSON format as described here.

Its implementation was discussed in issue #128 and issue #69.

You can find the code in this file: https://github.com/sonata-nfv/tng-sdk-package/blob/master/src/tngsdk/package/logger.py

Usage

Initialize logger:

import logging
from logger import TangoLogger

LOG = TangoLogger.getLogger("logger_name", log_level=logging.INFO, log_json=True)
  • name: Name of the logger, e.g., __name__
  • log_level: The loglevel to be used (Python default logging levels)
  • log_json: Boolean indicating if you want to have normal log outputs or the 5GTANGO JSON format logs.

Use logger:

The beauty of this implementation is that the logger can be used in the same way the normal Python logging is used.

Example:

LOG.warning("this is a test message")

If JSON logging is enabled, the custom log handler automatically produces something like this:

{
        "type":"W",
        "time_elapsed":"",
        "operation":"setup_logging",
        "status":"",
        "message":"this is a test message",
        "component":"tango.tngsdk.package",
        "processName":"MainProcess",
        "threadName":"MainThread",
        "start_stop":"",
        "lineno":73,
        "timestamp":"2018-11-15 19:25:49.348161 UTC"
    }

On top of this you can specify extra fields to add/overwrite the fields of the JSON output format by using the logger like this:

LOG.warning("this is a test message", extra={"start_stop": "START", "status": "201"})