test_logger.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. import logging
  2. import sys
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic import logger as module
  6. @pytest.mark.parametrize('bool_val', (True, 'yes', 'on', '1', 'true', 'True', 1))
  7. def test_to_bool_parses_true_values(bool_val):
  8. assert module.to_bool(bool_val)
  9. @pytest.mark.parametrize('bool_val', (False, 'no', 'off', '0', 'false', 'False', 0))
  10. def test_to_bool_parses_false_values(bool_val):
  11. assert not module.to_bool(bool_val)
  12. def test_to_bool_passes_none_through():
  13. assert module.to_bool(None) is None
  14. def test_interactive_console_false_when_not_isatty(capsys):
  15. with capsys.disabled():
  16. flexmock(module.sys.stderr).should_receive('isatty').and_return(False)
  17. assert module.interactive_console() is False
  18. def test_interactive_console_false_when_TERM_is_dumb(capsys):
  19. with capsys.disabled():
  20. flexmock(module.sys.stderr).should_receive('isatty').and_return(True)
  21. flexmock(module.os.environ).should_receive('get').with_args('TERM').and_return('dumb')
  22. assert module.interactive_console() is False
  23. def test_interactive_console_true_when_isatty_and_TERM_is_not_dumb(capsys):
  24. with capsys.disabled():
  25. flexmock(module.sys.stderr).should_receive('isatty').and_return(True)
  26. flexmock(module.os.environ).should_receive('get').with_args('TERM').and_return('smart')
  27. assert module.interactive_console() is True
  28. def test_should_do_markup_respects_no_color_value():
  29. flexmock(module.os.environ).should_receive('get').and_return(None)
  30. flexmock(module).should_receive('interactive_console').never()
  31. assert module.should_do_markup(no_color=True, configs={}) is False
  32. def test_should_do_markup_respects_config_value():
  33. flexmock(module.os.environ).should_receive('get').and_return(None)
  34. flexmock(module).should_receive('interactive_console').never()
  35. assert module.should_do_markup(no_color=False, configs={'foo.yaml': {'color': False}}) is False
  36. flexmock(module).should_receive('interactive_console').and_return(True).once()
  37. assert module.should_do_markup(no_color=False, configs={'foo.yaml': {'color': True}}) is True
  38. def test_should_do_markup_prefers_any_false_config_value():
  39. flexmock(module.os.environ).should_receive('get').and_return(None)
  40. flexmock(module).should_receive('interactive_console').never()
  41. assert (
  42. module.should_do_markup(
  43. no_color=False,
  44. configs={
  45. 'foo.yaml': {'color': True},
  46. 'bar.yaml': {'color': False},
  47. },
  48. )
  49. is False
  50. )
  51. def test_should_do_markup_respects_PY_COLORS_environment_variable():
  52. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(
  53. 'True'
  54. )
  55. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return(None)
  56. flexmock(module).should_receive('to_bool').and_return(True)
  57. assert module.should_do_markup(no_color=False, configs={}) is True
  58. def test_should_do_markup_prefers_no_color_value_to_config_value():
  59. flexmock(module.os.environ).should_receive('get').and_return(None)
  60. flexmock(module).should_receive('interactive_console').never()
  61. assert module.should_do_markup(no_color=True, configs={'foo.yaml': {'color': True}}) is False
  62. def test_should_do_markup_prefers_config_value_to_environment_variables():
  63. flexmock(module.os.environ).should_receive('get').and_return('True')
  64. flexmock(module).should_receive('to_bool').and_return(True)
  65. flexmock(module).should_receive('interactive_console').never()
  66. assert module.should_do_markup(no_color=False, configs={'foo.yaml': {'color': False}}) is False
  67. def test_should_do_markup_prefers_no_color_value_to_environment_variables():
  68. flexmock(module.os.environ).should_receive('get').and_return('True')
  69. flexmock(module).should_receive('to_bool').and_return(True)
  70. flexmock(module).should_receive('interactive_console').never()
  71. assert module.should_do_markup(no_color=True, configs={}) is False
  72. def test_should_do_markup_respects_interactive_console_value():
  73. flexmock(module.os.environ).should_receive('get').and_return(None)
  74. flexmock(module).should_receive('interactive_console').and_return(True)
  75. assert module.should_do_markup(no_color=False, configs={}) is True
  76. def test_should_do_markup_prefers_PY_COLORS_to_interactive_console_value():
  77. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(
  78. 'True'
  79. )
  80. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return(None)
  81. flexmock(module).should_receive('to_bool').and_return(True)
  82. flexmock(module).should_receive('interactive_console').never()
  83. assert module.should_do_markup(no_color=False, configs={}) is True
  84. def test_should_do_markup_prefers_NO_COLOR_to_interactive_console_value():
  85. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(None)
  86. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return('True')
  87. flexmock(module).should_receive('interactive_console').never()
  88. assert module.should_do_markup(no_color=False, configs={}) is False
  89. def test_should_do_markup_respects_NO_COLOR_environment_variable():
  90. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return('True')
  91. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(None)
  92. flexmock(module).should_receive('interactive_console').never()
  93. assert module.should_do_markup(no_color=False, configs={}) is False
  94. def test_should_do_markup_ignores_empty_NO_COLOR_environment_variable():
  95. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return('')
  96. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(None)
  97. flexmock(module).should_receive('interactive_console').and_return(True)
  98. assert module.should_do_markup(no_color=False, configs={}) is True
  99. def test_should_do_markup_prefers_NO_COLOR_to_PY_COLORS():
  100. flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(
  101. 'True'
  102. )
  103. flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return(
  104. 'SomeValue'
  105. )
  106. flexmock(module).should_receive('interactive_console').never()
  107. assert module.should_do_markup(no_color=False, configs={}) is False
  108. def test_multi_stream_handler_logs_to_handler_for_log_level():
  109. error_handler = flexmock()
  110. error_handler.should_receive('emit').once()
  111. info_handler = flexmock()
  112. multi_handler = module.Multi_stream_handler(
  113. {module.logging.ERROR: error_handler, module.logging.INFO: info_handler}
  114. )
  115. multi_handler.emit(flexmock(levelno=module.logging.ERROR))
  116. def test_console_color_formatter_format_includes_log_message():
  117. flexmock(module).should_receive('add_custom_log_levels')
  118. flexmock(module.logging).ANSWER = module.ANSWER
  119. plain_message = 'uh oh'
  120. record = flexmock(levelno=logging.CRITICAL, msg=plain_message)
  121. colored_message = module.Console_color_formatter().format(record)
  122. assert colored_message != plain_message
  123. assert plain_message in colored_message
  124. def test_color_text_does_not_raise():
  125. flexmock(module).should_receive('ansi_escape_code').and_return('blah')
  126. module.color_text(module.Color.RED, 'hi')
  127. def test_color_text_without_color_does_not_raise():
  128. flexmock(module).should_receive('ansi_escape_code').and_return('blah')
  129. module.color_text(None, 'hi')
  130. def test_add_logging_level_adds_level_name_and_sets_global_attributes_and_methods():
  131. logger = flexmock()
  132. flexmock(module.logging).should_receive('getLoggerClass').and_return(logger)
  133. flexmock(module.logging).should_receive('addLevelName').with_args(99, 'PLAID')
  134. builtins = flexmock(sys.modules['builtins'])
  135. builtins.should_call('setattr')
  136. builtins.should_receive('setattr').with_args(module.logging, 'PLAID', 99).once()
  137. builtins.should_receive('setattr').with_args(logger, 'plaid', object).once()
  138. builtins.should_receive('setattr').with_args(logging, 'plaid', object).once()
  139. module.add_logging_level('PLAID', 99)
  140. def test_add_logging_level_skips_global_setting_if_already_set():
  141. logger = flexmock()
  142. flexmock(module.logging).should_receive('getLoggerClass').and_return(logger)
  143. flexmock(module.logging).PLAID = 99
  144. flexmock(logger).plaid = flexmock()
  145. flexmock(logging).plaid = flexmock()
  146. flexmock(module.logging).should_receive('addLevelName').never()
  147. builtins = flexmock(sys.modules['builtins'])
  148. builtins.should_call('setattr')
  149. builtins.should_receive('setattr').with_args(module.logging, 'PLAID', 99).never()
  150. builtins.should_receive('setattr').with_args(logger, 'plaid', object).never()
  151. builtins.should_receive('setattr').with_args(logging, 'plaid', object).never()
  152. module.add_logging_level('PLAID', 99)
  153. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_linux():
  154. flexmock(module).should_receive('add_custom_log_levels')
  155. flexmock(module.logging).ANSWER = module.ANSWER
  156. fake_formatter = flexmock()
  157. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  158. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  159. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  160. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  161. flexmock(module).should_receive('interactive_console').and_return(False)
  162. flexmock(module.logging).should_receive('basicConfig').with_args(
  163. level=logging.DEBUG, handlers=list
  164. )
  165. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  166. syslog_handler = logging.handlers.SysLogHandler()
  167. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  168. address='/dev/log'
  169. ).and_return(syslog_handler).once()
  170. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  171. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_macos():
  172. flexmock(module).should_receive('add_custom_log_levels')
  173. flexmock(module.logging).ANSWER = module.ANSWER
  174. fake_formatter = flexmock()
  175. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  176. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  177. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  178. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  179. flexmock(module).should_receive('interactive_console').and_return(False)
  180. flexmock(module.logging).should_receive('basicConfig').with_args(
  181. level=logging.DEBUG, handlers=list
  182. )
  183. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  184. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  185. syslog_handler = logging.handlers.SysLogHandler()
  186. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  187. address='/var/run/syslog'
  188. ).and_return(syslog_handler).once()
  189. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  190. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_freebsd():
  191. flexmock(module).should_receive('add_custom_log_levels')
  192. flexmock(module.logging).ANSWER = module.ANSWER
  193. fake_formatter = flexmock()
  194. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  195. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  196. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  197. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  198. flexmock(module).should_receive('interactive_console').and_return(False)
  199. flexmock(module.logging).should_receive('basicConfig').with_args(
  200. level=logging.DEBUG, handlers=list
  201. )
  202. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  203. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
  204. flexmock(module.os.path).should_receive('exists').with_args('/var/run/log').and_return(True)
  205. syslog_handler = logging.handlers.SysLogHandler()
  206. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  207. address='/var/run/log'
  208. ).and_return(syslog_handler).once()
  209. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  210. def test_configure_logging_without_syslog_log_level_skips_syslog():
  211. flexmock(module).should_receive('add_custom_log_levels')
  212. flexmock(module.logging).ANSWER = module.ANSWER
  213. fake_formatter = flexmock()
  214. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  215. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  216. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  217. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  218. flexmock(module.logging).should_receive('basicConfig').with_args(
  219. level=logging.INFO, handlers=list
  220. )
  221. flexmock(module.os.path).should_receive('exists').never()
  222. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  223. module.configure_logging(console_log_level=logging.INFO)
  224. def test_configure_logging_skips_syslog_if_not_found():
  225. flexmock(module).should_receive('add_custom_log_levels')
  226. flexmock(module.logging).ANSWER = module.ANSWER
  227. fake_formatter = flexmock()
  228. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  229. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  230. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  231. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  232. flexmock(module.logging).should_receive('basicConfig').with_args(
  233. level=logging.INFO, handlers=list
  234. )
  235. flexmock(module.os.path).should_receive('exists').and_return(False)
  236. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  237. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  238. def test_configure_logging_skips_log_file_if_log_file_logging_is_disabled():
  239. flexmock(module).should_receive('add_custom_log_levels')
  240. flexmock(module.logging).DISABLED = module.DISABLED
  241. fake_formatter = flexmock()
  242. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  243. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  244. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  245. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  246. flexmock(module.logging).should_receive('basicConfig').with_args(
  247. level=logging.INFO, handlers=list
  248. )
  249. flexmock(module.os.path).should_receive('exists').never()
  250. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  251. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  252. module.configure_logging(
  253. console_log_level=logging.INFO, log_file_log_level=logging.DISABLED, log_file='/tmp/logfile'
  254. )
  255. def test_configure_logging_to_log_file_instead_of_syslog():
  256. flexmock(module).should_receive('add_custom_log_levels')
  257. flexmock(module.logging).ANSWER = module.ANSWER
  258. fake_formatter = flexmock()
  259. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  260. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  261. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  262. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  263. flexmock(module.logging).should_receive('basicConfig').with_args(
  264. level=logging.DEBUG, handlers=list
  265. )
  266. flexmock(module.os.path).should_receive('exists').never()
  267. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  268. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  269. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  270. '/tmp/logfile'
  271. ).and_return(file_handler).once()
  272. module.configure_logging(
  273. console_log_level=logging.INFO,
  274. syslog_log_level=logging.DISABLED,
  275. log_file_log_level=logging.DEBUG,
  276. log_file='/tmp/logfile',
  277. )
  278. def test_configure_logging_to_both_log_file_and_syslog():
  279. flexmock(module).should_receive('add_custom_log_levels')
  280. flexmock(module.logging).ANSWER = module.ANSWER
  281. fake_formatter = flexmock()
  282. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  283. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  284. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  285. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  286. flexmock(module.logging).should_receive('basicConfig').with_args(
  287. level=logging.DEBUG, handlers=list
  288. )
  289. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  290. syslog_handler = logging.handlers.SysLogHandler()
  291. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  292. address='/dev/log'
  293. ).and_return(syslog_handler).once()
  294. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  295. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  296. '/tmp/logfile'
  297. ).and_return(file_handler).once()
  298. module.configure_logging(
  299. console_log_level=logging.INFO,
  300. syslog_log_level=logging.DEBUG,
  301. log_file_log_level=logging.DEBUG,
  302. log_file='/tmp/logfile',
  303. )
  304. def test_configure_logging_to_log_file_formats_with_custom_log_format():
  305. flexmock(module).should_receive('add_custom_log_levels')
  306. flexmock(module.logging).ANSWER = module.ANSWER
  307. flexmock(module.logging).should_receive('Formatter').with_args(
  308. '{message}', style='{' # noqa: FS003
  309. ).once()
  310. fake_formatter = flexmock()
  311. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  312. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  313. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  314. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  315. flexmock(module).should_receive('interactive_console').and_return(False)
  316. flexmock(module.logging).should_receive('basicConfig').with_args(
  317. level=logging.DEBUG, handlers=list
  318. )
  319. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  320. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  321. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  322. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  323. '/tmp/logfile'
  324. ).and_return(file_handler).once()
  325. module.configure_logging(
  326. console_log_level=logging.INFO,
  327. log_file_log_level=logging.DEBUG,
  328. log_file='/tmp/logfile',
  329. log_file_format='{message}', # noqa: FS003
  330. )
  331. def test_configure_logging_skips_log_file_if_argument_is_none():
  332. flexmock(module).should_receive('add_custom_log_levels')
  333. flexmock(module.logging).ANSWER = module.ANSWER
  334. fake_formatter = flexmock()
  335. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  336. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  337. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  338. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  339. flexmock(module.logging).should_receive('basicConfig').with_args(
  340. level=logging.INFO, handlers=list
  341. )
  342. flexmock(module.os.path).should_receive('exists').and_return(False)
  343. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  344. module.configure_logging(console_log_level=logging.INFO, log_file=None)
  345. def test_configure_logging_uses_console_no_color_formatter_if_color_disabled():
  346. flexmock(module).should_receive('add_custom_log_levels')
  347. flexmock(module.logging).ANSWER = module.ANSWER
  348. fake_formatter = flexmock()
  349. flexmock(module).should_receive('Console_color_formatter').never()
  350. flexmock(module).should_receive('Console_no_color_formatter').and_return(fake_formatter)
  351. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  352. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  353. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  354. flexmock(module.logging).should_receive('basicConfig').with_args(
  355. level=logging.INFO, handlers=list
  356. )
  357. flexmock(module.os.path).should_receive('exists').and_return(False)
  358. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  359. module.configure_logging(console_log_level=logging.INFO, log_file=None, color_enabled=False)