1
|
|
|
import os |
|
|
|
|
2
|
|
|
import sys |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
def _gw_logging_cfg(log_file): |
6
|
|
|
return { |
7
|
|
|
'version': 1, |
8
|
|
|
'disable_existing_loggers': False, |
9
|
|
|
'formatters': { |
10
|
|
|
'standard': { |
11
|
|
|
'format': "%(asctime)s - %(levelname)-5s - %(message)s" |
12
|
|
|
}, |
13
|
|
|
'debug': { |
14
|
|
|
'format': "%(asctime)s - %(levelname)-5s - %(name)-40s - %(message)-80s - %(module)s:%(" |
|
|
|
|
15
|
|
|
"funcName)s(%(lineno)s)" |
16
|
|
|
}, |
17
|
|
|
}, |
18
|
|
|
'handlers': { |
19
|
|
|
'console_stdout': { |
20
|
|
|
'formatter': 'standard', |
21
|
|
|
'class': 'logging.StreamHandler', |
22
|
|
|
'stream': sys.stdout, |
23
|
|
|
'level': 'INFO' |
24
|
|
|
}, |
25
|
|
|
'file': { |
26
|
|
|
"class": "logging.handlers.RotatingFileHandler", |
27
|
|
|
"formatter": "debug", |
28
|
|
|
"filename": log_file, |
29
|
|
|
"maxBytes": 1024000, |
30
|
|
|
"backupCount": 3, |
31
|
|
|
'level': 'INFO' |
32
|
|
|
}, |
33
|
|
|
}, |
34
|
|
|
'loggers': { |
35
|
|
|
'': { |
36
|
|
|
'handlers': ['console_stdout'], |
37
|
|
|
'level': 'INFO', |
38
|
|
|
'propagate': False |
39
|
|
|
}, |
40
|
|
|
'groundwork': { |
41
|
|
|
'handlers': ['console_stdout', 'file'], |
42
|
|
|
'level': 'INFO', |
43
|
|
|
'propagate': False |
44
|
|
|
}, |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
View Code Duplication |
def test_logging_file(basicApp, tmpdir): |
|
|
|
|
50
|
|
|
""" |
51
|
|
|
The test case sets up a Groundwork application with logging to file with log level ``INFO``. |
52
|
|
|
The application is then called to log a debug and an info message. |
53
|
|
|
|
54
|
|
|
The generated logfile is then parsed and asserted, that only the info message can be found, |
55
|
|
|
the debug message cannot. |
56
|
|
|
""" |
57
|
|
|
log_file = os.path.join(str(tmpdir), "test.log") |
58
|
|
|
app = basicApp |
59
|
|
|
# set up logging in the config, with log level INFO |
60
|
|
|
app.config.set('GROUNDWORK_LOGGING', _gw_logging_cfg(log_file)) |
61
|
|
|
app._configure_logging(app.config.get("GROUNDWORK_LOGGING")) |
|
|
|
|
62
|
|
|
|
63
|
|
|
debug_message = "This is a test debug message." |
64
|
|
|
info_message = "This is a test info message." |
65
|
|
|
app.log.debug(debug_message) |
66
|
|
|
app.log.info(info_message) |
67
|
|
|
|
68
|
|
|
# verify the contents of the log file |
69
|
|
|
with open(log_file) as lf: |
|
|
|
|
70
|
|
|
log = lf.read() |
71
|
|
|
# at log level INFO, the DEBUG message should not be there |
72
|
|
|
assert log.find(debug_message) == -1 |
73
|
|
|
# the INFO message should be there |
74
|
|
|
assert log.find(info_message) > 0 |
75
|
|
|
|
76
|
|
|
|
77
|
|
View Code Duplication |
def test_logging_console(basicApp, tmpdir, capsys): |
|
|
|
|
78
|
|
|
""" |
79
|
|
|
The test case sets up a Groundwork application with logging to stdout with log level ``INFO``. |
80
|
|
|
The application is then called to log a debug and an info message. |
81
|
|
|
|
82
|
|
|
The captured stdout is then parsed and asserted, that only the info message can be found, |
83
|
|
|
the debug message cannot. |
84
|
|
|
""" |
85
|
|
|
log_file = os.path.join(str(tmpdir), "test.log") |
86
|
|
|
app = basicApp |
87
|
|
|
# set up logging in the config, with log level INFO |
88
|
|
|
app.config.set('GROUNDWORK_LOGGING', _gw_logging_cfg(log_file)) |
89
|
|
|
app._configure_logging(app.config.get("GROUNDWORK_LOGGING")) |
|
|
|
|
90
|
|
|
|
91
|
|
|
debug_message = "This is a test debug message." |
92
|
|
|
info_message = "This is a test info message." |
93
|
|
|
app.log.debug(debug_message) |
94
|
|
|
app.log.info(info_message) |
95
|
|
|
|
96
|
|
|
out, err = capsys.readouterr() |
|
|
|
|
97
|
|
|
# at log level INFO, the DEBUG message should not be there |
98
|
|
|
assert out.find(debug_message) == -1 |
99
|
|
|
# the INFO message should be there |
100
|
|
|
assert out.find(info_message) > 0 |
101
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.