log_helper_test_main.py
2.43 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
""" Test log_helpers """
import sys
import logging
from tests.util.log_helper import log_helper_test_imported
from oletools.util.log_helper import log_helper
DEBUG_MESSAGE = 'main: debug log'
INFO_MESSAGE = 'main: info log'
WARNING_MESSAGE = 'main: warning log'
ERROR_MESSAGE = 'main: error log'
CRITICAL_MESSAGE = 'main: critical log'
logger = log_helper.get_or_create_logger('test_main')
def init_logging_and_log(cmd_line_args=None):
args = cmd_line_args if cmd_line_args else sys.argv
if 'silent' in args:
return _log_silently()
elif 'dictionary' in args:
return _log_dictionary(args)
elif 'current_level' in args:
_enable_logging()
return _log_at_current_level()
elif 'default' in args:
_enable_logging()
return _log(logger)
use_json = '-j' in args
throw_exception = 'throw' in args
level = _parse_log_level(args)
_enable_logging(use_json, level)
_log(logger)
log_helper_test_imported.log()
if throw_exception:
raise Exception('An exception occurred before ending the logging')
_end_logging()
def _parse_log_level(args):
if 'debug' in args:
return 'debug'
elif 'info' in args:
return 'info'
elif 'warning' in args:
return 'warning'
elif 'error' in args:
return 'error'
else:
return 'critical'
def _log_dictionary(args):
level = _parse_log_level(args)
_enable_logging(True, level)
logger.log_at_current_level({
'msg': DEBUG_MESSAGE
})
log_helper.end_logging()
def _enable_logging(use_json=False, level='warning'):
log_helper.enable_logging(use_json, level, stream=sys.stdout)
def _log_at_current_level():
logger.log_at_current_level(DEBUG_MESSAGE)
def _log_silently():
silent_logger = log_helper.get_or_create_silent_logger('test_main_silent', logging.DEBUG - 1)
_log(silent_logger)
def _log(current_logger):
current_logger.debug(DEBUG_MESSAGE)
current_logger.info(INFO_MESSAGE)
current_logger.warning(WARNING_MESSAGE)
current_logger.error(ERROR_MESSAGE)
current_logger.critical(CRITICAL_MESSAGE)
def _end_logging():
log_helper.end_logging()
if __name__ == '__main__':
# since we are using subprocess, add delimiters so we can easily extract
# the output that matters (when debugging tests we get extra output,
# so we need to ignore it)
print('<#')
init_logging_and_log(sys.argv)
print('#>')