1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
from __future__ import absolute_import |
17
|
|
|
|
18
|
|
|
import os.path |
19
|
|
|
import logging |
20
|
|
|
|
21
|
|
|
__all__ = [ |
22
|
|
|
'reopen_log_files', |
23
|
|
|
'set_log_level_for_all_handlers', |
24
|
|
|
'set_log_level_for_all_loggers' |
25
|
|
|
] |
26
|
|
|
|
27
|
|
|
LOG = logging.getLogger(__name__) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def reopen_log_files(handlers): |
31
|
|
|
""" |
32
|
|
|
This method iterates through all of the providers handlers looking for the FileHandler types. |
33
|
|
|
|
34
|
|
|
A lock is acquired, the underlying stream closed, reopened, then the lock is released. |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
This method should be called when logs are to be rotated by an external process. The simplest |
38
|
|
|
way to do this is via a signal handler. |
39
|
|
|
""" |
40
|
|
|
for handler in handlers: |
41
|
|
|
if not isinstance(handler, logging.FileHandler): |
42
|
|
|
continue |
43
|
|
|
|
44
|
|
|
LOG.info('Re-opening log file "%s" with mode "%s"\n' % |
|
|
|
|
45
|
|
|
(handler.baseFilename, handler.mode)) |
46
|
|
|
|
47
|
|
|
try: |
48
|
|
|
handler.acquire() |
49
|
|
|
handler.stream.close() |
50
|
|
|
handler.stream = open(handler.baseFilename, handler.mode) |
51
|
|
|
finally: |
52
|
|
|
try: |
53
|
|
|
handler.release() |
54
|
|
|
except RuntimeError as e: |
55
|
|
|
if 'cannot release' in str(e): |
56
|
|
|
# Release failed which most likely indicates that acquire failed |
57
|
|
|
# and lock was never acquired |
58
|
|
|
LOG.warn('Failed to release lock', exc_info=True) |
59
|
|
|
else: |
60
|
|
|
raise e |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def set_log_level_for_all_handlers(logger, level=logging.DEBUG): |
64
|
|
|
""" |
65
|
|
|
Set a log level for all the handlers on the provided logger. |
66
|
|
|
""" |
67
|
|
|
logger.setLevel(level) |
68
|
|
|
|
69
|
|
|
handlers = logger.handlers |
70
|
|
|
for handler in handlers: |
71
|
|
|
handler.setLevel(level) |
72
|
|
|
|
73
|
|
|
return logger |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def set_log_level_for_all_loggers(level=logging.DEBUG): |
77
|
|
|
""" |
78
|
|
|
Set a log level for all the loggers and handlers to the provided level. |
79
|
|
|
""" |
80
|
|
|
root_logger = logging.getLogger() |
81
|
|
|
loggers = logging.Logger.manager.loggerDict.values() |
82
|
|
|
loggers += [root_logger] |
83
|
|
|
|
84
|
|
|
for logger in loggers: |
85
|
|
|
if not isinstance(logger, logging.Logger): |
86
|
|
|
continue |
87
|
|
|
|
88
|
|
|
set_log_level_for_all_handlers(logger=logger) |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
def get_logger_name_for_module(module): |
92
|
|
|
""" |
93
|
|
|
Retrieve fully qualified logger name for current module (e.g. |
94
|
|
|
st2common.cmd.sensormanager) |
95
|
|
|
|
96
|
|
|
:type: ``str`` |
97
|
|
|
""" |
98
|
|
|
module_file = module.__file__ |
99
|
|
|
base_dir = os.path.dirname(os.path.abspath(module_file)) |
100
|
|
|
module_name = os.path.basename(module_file) |
101
|
|
|
module_name = module_name.replace('.pyc', '').replace('.py', '') |
102
|
|
|
|
103
|
|
|
split = base_dir.split(os.path.sep) |
104
|
|
|
split = [component for component in split if component] |
105
|
|
|
|
106
|
|
|
# Find first component which starts with st2 and use that as a starting point |
107
|
|
|
start_index = 0 |
108
|
|
|
for index, component in enumerate(reversed(split)): |
109
|
|
|
if component.startswith('st2'): |
110
|
|
|
start_index = ((len(split) - 1) - index) |
111
|
|
|
break |
112
|
|
|
|
113
|
|
|
split = split[start_index:] |
114
|
|
|
name = '.'.join(split) + '.' + module_name |
115
|
|
|
|
116
|
|
|
return name |
117
|
|
|
|