log_helper_test_main.py
2.04 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
""" Test log_helpers """
import sys
from tests.common.log_helper import log_helper_test_imported
from oletools.common.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'
RESULT_MESSAGE = 'main: result log'
RESULT_TYPE = 'main: result'
logger = log_helper.get_or_create_silent_logger('test_main')
def init_logging_and_log(args):
"""
Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome:
- Log without enabling: ['<level>']
* logging when being imported - should never print
- Log as JSON without enabling: ['as-json', '<level>']
* logging as JSON when being imported - should never print
- Enable and log: ['enable', '<level>']
* logging when being run as script - should log messages
- Enable and log as JSON: ['as-json', 'enable', '<level>']
* logging as JSON when being run as script - should log messages as JSON
- Enable, log as JSON and throw: ['enable', 'as-json', 'throw', '<level>']
* should produce JSON-compatible output, even after an unhandled exception
"""
# the level should always be the last argument passed
level = args[-1]
use_json = 'as-json' in args
throw = 'throw' in args
percent_autoformat = '%-autoformat' in args
if 'enable' in args:
log_helper.enable_logging(use_json, level, stream=sys.stdout)
_log()
if percent_autoformat:
logger.info('The %s is %d.', 'answer', 47)
if throw:
raise Exception('An exception occurred before ending the logging')
log_helper.end_logging()
def _log():
logger.debug(DEBUG_MESSAGE)
logger.info(INFO_MESSAGE)
logger.warning(WARNING_MESSAGE)
logger.error(ERROR_MESSAGE)
logger.critical(CRITICAL_MESSAGE)
logger.info(RESULT_MESSAGE, type=RESULT_TYPE)
log_helper_test_imported.log()
if __name__ == '__main__':
init_logging_and_log(sys.argv[1:])