MDCLogger — xapp-frame-py master documentation

# MDCLogger

## Usage

The library can be used in as shown below.

    ```python
      from ricappframe.logger.mdclogger import MDCLogger
      my_logger = MDCLogger()
      my_logger.mdclog_format_init(configmap_monitor=True)
      my_logger.error("This is an error log")
    ```

A program can create several logger instances.

mdclog_format_init() Adds the MDC log format with HostName, PodName, ContainerName, ServiceName,PID,CallbackNotifyforLogFieldChange

Pass configmap_monitor = False in mdclog_format_init() function to stop dynamic log level change based on configmap.

## Logging Levels

    """Severity levels of the log messages."""
        DEBUG = 10
        INFO = 20
        WARNING = 30
        ERROR = 40

## mdcLogger API’s

1. Set current logging level

    def set_level(self, level: Level):

           Keyword arguments:
           level -- logging level. Log messages with lower severity will be filtered.

2. Return the current logging level

    def get_level(self) -> Level:

3. Add a logger specific MDC

    def add_mdc(self, key: str, value: Value):

           Keyword arguments:
           key -- MDC key
           value -- MDC value

4. Return logger’s MDC value with the given key or None

    def get_mdc(self, key: str) -> Value:

5. Remove logger’s MDC with the given key

    def remove_mdc(self, key: str):

6. Remove all MDCs of the logger instance.

    def clean_mdc(self):

7. Initialise Logging format:

This api Initialzes mdclog print format using MDC Dictionary by extracting the environment variables in the calling process for “SYSTEM_NAME”, “HOST_NAME”, “SERVICE_NAME”, “CONTAINER_NAME”, “POD_NAME” & “CONFIG_MAP_NAME” mapped to HostName, ServiceName, ContainerName, Podname and Configuration-file-name of the services respectively.

    def mdclog_format_init(configmap_monitor=False):

           Keyword arguments:
           configmap_monitor -- Enables/Disables Dynamic log level change based on configmap
                             -- Boolean values True/False can be passed as per requirement.