test_logger.py 20 KB

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