summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozlog/mozlog/unstructured/loggingmixin.py
blob: 727e8f1a7dbe0cd8152c554926e385d79e657315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from .logger import Logger, getLogger


class LoggingMixin(object):
    """Expose a subset of logging functions to an inheriting class."""

    def set_logger(self, logger_instance=None, name=None):
        """Method for setting the underlying logger instance to be used."""

        if logger_instance and not isinstance(logger_instance, Logger):
            raise ValueError("logger_instance must be an instance of Logger")

        if name is None:
            name = ".".join([self.__module__, self.__class__.__name__])

        self._logger = logger_instance or getLogger(name)

    def _log_msg(self, cmd, *args, **kwargs):
        if not hasattr(self, "_logger"):
            self._logger = getLogger(
                ".".join([self.__module__, self.__class__.__name__])
            )
        getattr(self._logger, cmd)(*args, **kwargs)

    def log(self, *args, **kwargs):
        self._log_msg("log", *args, **kwargs)

    def info(self, *args, **kwargs):
        self._log_msg("info", *args, **kwargs)

    def error(self, *args, **kwargs):
        self._log_msg("error", *args, **kwargs)

    def warn(self, *args, **kwargs):
        self._log_msg("warn", *args, **kwargs)

    def log_structured(self, *args, **kwargs):
        self._log_msg("log_structured", *args, **kwargs)