1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import
6
7from .logger import (
8    Logger,
9    getLogger,
10)
11
12
13class LoggingMixin(object):
14    """Expose a subset of logging functions to an inheriting class."""
15
16    def set_logger(self, logger_instance=None, name=None):
17        """Method for setting the underlying logger instance to be used."""
18
19        if logger_instance and not isinstance(logger_instance, Logger):
20            raise ValueError("logger_instance must be an instance of Logger")
21
22        if name is None:
23            name = ".".join([self.__module__, self.__class__.__name__])
24
25        self._logger = logger_instance or getLogger(name)
26
27    def _log_msg(self, cmd, *args, **kwargs):
28        if not hasattr(self, "_logger"):
29            self._logger = getLogger(
30                ".".join([self.__module__, self.__class__.__name__])
31            )
32        getattr(self._logger, cmd)(*args, **kwargs)
33
34    def log(self, *args, **kwargs):
35        self._log_msg("log", *args, **kwargs)
36
37    def info(self, *args, **kwargs):
38        self._log_msg("info", *args, **kwargs)
39
40    def error(self, *args, **kwargs):
41        self._log_msg("error", *args, **kwargs)
42
43    def warn(self, *args, **kwargs):
44        self._log_msg("warn", *args, **kwargs)
45
46    def log_structured(self, *args, **kwargs):
47        self._log_msg("log_structured", *args, **kwargs)
48