Completed
Push — master ( 2b80fa...6ea077 )
by Nicolas
01:22
created

glances_logger()   B

Complexity

Conditions 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 4
c 3
b 1
f 0
dl 0
loc 24
rs 8.6845
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2017 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
"""Custom logger class."""
21
22
import os
23
import json
24
import getpass
25
import tempfile
26
27
import logging
28
import logging.config
29
30
LOG_FILENAME = os.path.join(tempfile.gettempdir(), 'glances-{}.log'.format(getpass.getuser()))
31
32
# Define the logging configuration
33
LOGGING_CFG = {
34
    "version": 1,
35
    "disable_existing_loggers": "False",
36
    "root": {
37
        "level": "INFO",
38
        "handlers": ["file", "console"]
39
    },
40
    "formatters": {
41
        "standard": {
42
            "format": "%(asctime)s -- %(levelname)s -- %(message)s"
43
        },
44
        "short": {
45
            "format": "%(levelname)s: %(message)s"
46
        },
47
        "free": {
48
            "format": "%(message)s"
49
        }
50
    },
51
    "handlers": {
52
        "file": {
53
            "level": "DEBUG",
54
            "class": "logging.handlers.RotatingFileHandler",
55
            "formatter": "standard",
56
            "filename": LOG_FILENAME
57
        },
58
        "console": {
59
            "level": "CRITICAL",
60
            "class": "logging.StreamHandler",
61
            "formatter": "free"
62
        }
63
    },
64
    "loggers": {
65
        "debug": {
66
            "handlers": ["file", "console"],
67
            "level": "DEBUG"
68
        },
69
        "verbose": {
70
            "handlers": ["file", "console"],
71
            "level": "INFO"
72
        },
73
        "standard": {
74
            "handlers": ["file"],
75
            "level": "INFO"
76
        },
77
        "requests": {
78
            "handlers": ["file", "console"],
79
            "level": "ERROR"
80
        },
81
        "elasticsearch": {
82
            "handlers": ["file", "console"],
83
            "level": "ERROR"
84
        },
85
        "elasticsearch.trace": {
86
            "handlers": ["file", "console"],
87
            "level": "ERROR"
88
        }
89
    }
90
}
91
92
93
def glances_logger(env_key='LOG_CFG'):
94
    """Build and return the logger.
95
96
    env_key define the env var where a path to a specific JSON logger
97
            could be defined
98
99
    :return: logger -- Logger instance
100
    """
101
    _logger = logging.getLogger()
102
103
    # By default, use the LOGGING_CFG logger configuration
104
    config = LOGGING_CFG
105
106
    # Check if a specific configuration is available
107
    user_file = os.getenv(env_key, None)
108
    if user_file and os.path.exists(user_file):
109
        # A user file as been defined. Use it...
110
        with open(user_file, 'rt') as f:
111
            config = json.load(f)
112
113
    # Load the configuration
114
    logging.config.dictConfig(config)
115
116
    return _logger
117
118
119
logger = glances_logger()
120