-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathlogger.py
More file actions
142 lines (129 loc) · 4.14 KB
/
logger.py
File metadata and controls
142 lines (129 loc) · 4.14 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright (c) 2017 Red Hat, Inc.
#
# This file is part of ARA: Ansible Run Analysis.
#
# ARA is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ARA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
# Note: This file tries to import itself if it's named logging, thus, logger.
import logging
import logging.config
import os
import yaml
from ara.config.compat import ara_config
DEFAULT_LOG_CONFIG = """
---
version: 1
formatters:
normal:
format: '%(asctime)s %(levelname)s %(name)s: %(message)s'
console:
format: '%(asctime)s %(levelname)s %(name)s: %(message)s'
handlers:
console:
class: logging.StreamHandler
formatter: console
level: INFO
stream: ext://sys.stdout
normal:
class: logging.handlers.TimedRotatingFileHandler
formatter: normal
level: DEBUG
filename: '{dir}/{file}'
when: 'midnight'
interval: 1
backupCount: 30
loggers:
ara:
handlers:
- console
- normal
level: {level}
propagate: 0
alembic:
handlers:
- console
- normal
level: WARN
propagate: 0
sqlalchemy.engine:
handlers:
- console
- normal
level: WARN
propagate: 0
werkzeug:
handlers:
- console
- normal
level: INFO
propagate: 0
root:
handlers:
- normal
level: {level}
"""
class LogConfig(object):
def __init__(self):
default_dir = ara_config('dir', 'ARA_DIR',
os.path.expanduser('~/.ara'))
self.ARA_LOG_CONFIG = ara_config(
'logconfig', 'ARA_LOG_CONFIG', os.path.join(default_dir,
'logging.yml')
)
self.ARA_LOG_DIR = ara_config('logdir', 'ARA_LOG_DIR', default_dir)
self.ARA_LOG_FILE = ara_config('logfile', 'ARA_LOG_FILE', 'ara.log')
self.ARA_LOG_LEVEL = ara_config('loglevel', 'ARA_LOG_LEVEL', 'INFO')
if self.ARA_LOG_LEVEL == 'DEBUG':
self.SQLALCHEMY_ECHO = True
self.ARA_ENABLE_DEBUG_VIEW = True
else:
self.SQLALCHEMY_ECHO = False
self.ARA_ENABLE_DEBUG_VIEW = False
@property
def config(self):
""" Returns a dictionary for the loaded configuration """
return {
key: self.__dict__[key]
for key in dir(self)
if key.isupper()
}
def setup_logging(config=None):
if config is None:
config = LogConfig().config
if not os.path.isdir(config['ARA_LOG_DIR']):
os.makedirs(config['ARA_LOG_DIR'], mode=0o750)
if not os.path.exists(config['ARA_LOG_CONFIG']):
default_config = DEFAULT_LOG_CONFIG.format(
dir=config['ARA_LOG_DIR'],
file=config['ARA_LOG_FILE'],
level=config['ARA_LOG_LEVEL']
)
with open(config['ARA_LOG_CONFIG'], 'w') as log_config:
log_config.write(default_config.lstrip())
ext = os.path.splitext(config['ARA_LOG_CONFIG'])[1]
if ext in ('.yml', '.yaml', '.json'):
# yaml.safe_load can load json as well as yaml
logging.config.dictConfig(yaml.safe_load(
open(config['ARA_LOG_CONFIG'], 'r')
))
else:
logging.config.fileConfig(config['ARA_LOG_CONFIG'])
logger = logging.getLogger('ara.logging')
msg = 'Logging: Level {level} from {config}, logging to {dir}/{file}'
msg = msg.format(
level=config['ARA_LOG_LEVEL'],
config=config['ARA_LOG_CONFIG'],
dir=config['ARA_LOG_DIR'],
file=config['ARA_LOG_FILE'],
)
logger.debug(msg)