File size: 2,262 Bytes
1f53a4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pathlib import Path
import logging


class BaseLogger:
    """
    Class for defining logger.
    """
    _unexecuted_configure = True

    @classmethod
    def get_logger(cls, name: str) -> logging.Logger:
        """
        Set logger.
        Args:
            name (str): If needed, potentially hierarchical name is desired, eg. lib.net, lib.dataloader, etc.
                        For the details, see https://docs.python.org/3/library/logging.html?highlight=logging#module-logging.
        Returns:
            logging.Logger: logger
        """
        if cls._unexecuted_configure:
            cls._init_logger()

        return logging.getLogger('nervus.{}'.format(name))

    @classmethod
    def _init_logger(cls) -> None:
        """
        Configure logger.
        """
        _root_logger = logging.getLogger('nervus')
        _root_logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

        log_dir = Path('logs')
        log_dir.mkdir(parents=True, exist_ok=True)
        log_path = Path(log_dir, 'log.log')

        # file handler
        ## upper warning
        fh_err = logging.FileHandler(log_path)
        fh_err.setLevel(logging.WARNING)
        fh_err.setFormatter(formatter)
        fh_err.addFilter(lambda log_record: not ('BdbQuit' in str(log_record.exc_info)) and (log_record.levelno >= logging.WARNING))
        _root_logger.addHandler(fh_err)

        ## lower warning
        fh = logging.FileHandler(log_path)
        fh.setLevel(logging.DEBUG)
        fh.addFilter(lambda log_record: log_record.levelno < logging.WARNING)
        _root_logger.addHandler(fh)

        # stream handler
        ## upper warning
        ch_err = logging.StreamHandler()
        ch_err.setLevel(logging.WARNING)
        ch_err.setFormatter(formatter)
        ch_err.addFilter(lambda log_record: log_record.levelno >= logging.WARNING)
        _root_logger.addHandler(ch_err)

        ## lower warning
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.addFilter(lambda log_record: log_record.levelno < logging.WARNING)
        _root_logger.addHandler(ch)

        cls._unexecuted_configure = False