test_logger.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. flexmock(module.logging.Formatter).should_receive('format').and_return(plain_message)
  121. record = flexmock(levelno=logging.CRITICAL)
  122. colored_message = module.Console_color_formatter().format(record)
  123. assert colored_message != plain_message
  124. assert plain_message in colored_message
  125. def test_color_text_does_not_raise():
  126. flexmock(module).should_receive('ansi_escape_code').and_return('blah')
  127. module.color_text(module.Color.RED, 'hi')
  128. def test_color_text_without_color_does_not_raise():
  129. flexmock(module).should_receive('ansi_escape_code').and_return('blah')
  130. module.color_text(None, 'hi')
  131. def test_add_logging_level_adds_level_name_and_sets_global_attributes_and_methods():
  132. logger = flexmock()
  133. flexmock(module.logging).should_receive('getLoggerClass').and_return(logger)
  134. flexmock(module.logging).should_receive('addLevelName').with_args(99, 'PLAID')
  135. builtins = flexmock(sys.modules['builtins'])
  136. builtins.should_call('setattr')
  137. builtins.should_receive('setattr').with_args(module.logging, 'PLAID', 99).once()
  138. builtins.should_receive('setattr').with_args(logger, 'plaid', object).once()
  139. builtins.should_receive('setattr').with_args(logging, 'plaid', object).once()
  140. module.add_logging_level('PLAID', 99)
  141. def test_add_logging_level_skips_global_setting_if_already_set():
  142. logger = flexmock()
  143. flexmock(module.logging).should_receive('getLoggerClass').and_return(logger)
  144. flexmock(module.logging).PLAID = 99
  145. flexmock(logger).plaid = flexmock()
  146. flexmock(logging).plaid = flexmock()
  147. flexmock(module.logging).should_receive('addLevelName').never()
  148. builtins = flexmock(sys.modules['builtins'])
  149. builtins.should_call('setattr')
  150. builtins.should_receive('setattr').with_args(module.logging, 'PLAID', 99).never()
  151. builtins.should_receive('setattr').with_args(logger, 'plaid', object).never()
  152. builtins.should_receive('setattr').with_args(logging, 'plaid', object).never()
  153. module.add_logging_level('PLAID', 99)
  154. def test_get_log_prefix_gets_prefix_from_first_handler_formatter_with_prefix():
  155. flexmock(module.logging).should_receive('getLogger').and_return(
  156. flexmock(
  157. handlers=[
  158. flexmock(formatter=flexmock()),
  159. flexmock(formatter=flexmock(prefix='myprefix')),
  160. ],
  161. removeHandler=lambda handler: None,
  162. )
  163. )
  164. assert module.get_log_prefix() == 'myprefix'
  165. def test_get_log_prefix_with_no_handlers_does_not_raise():
  166. flexmock(module.logging).should_receive('getLogger').and_return(
  167. flexmock(
  168. handlers=[],
  169. removeHandler=lambda handler: None,
  170. )
  171. )
  172. assert module.get_log_prefix() is None
  173. def test_get_log_prefix_with_no_formatters_does_not_raise():
  174. flexmock(module.logging).should_receive('getLogger').and_return(
  175. flexmock(
  176. handlers=[
  177. flexmock(formatter=None),
  178. flexmock(formatter=None),
  179. ],
  180. removeHandler=lambda handler: None,
  181. )
  182. )
  183. assert module.get_log_prefix() is None
  184. def test_get_log_prefix_with_no_prefix_does_not_raise():
  185. flexmock(module.logging).should_receive('getLogger').and_return(
  186. flexmock(
  187. handlers=[
  188. flexmock(
  189. formatter=flexmock(),
  190. ),
  191. ],
  192. removeHandler=lambda handler: None,
  193. )
  194. )
  195. assert module.get_log_prefix() is None
  196. def test_set_log_prefix_updates_all_handler_formatters():
  197. formatters = (
  198. flexmock(prefix=None),
  199. flexmock(prefix=None),
  200. )
  201. flexmock(module.logging).should_receive('getLogger').and_return(
  202. flexmock(
  203. handlers=[
  204. flexmock(
  205. formatter=formatters[0],
  206. ),
  207. flexmock(
  208. formatter=formatters[1],
  209. ),
  210. ],
  211. removeHandler=lambda handler: None,
  212. )
  213. )
  214. module.set_log_prefix('myprefix')
  215. for formatter in formatters:
  216. assert formatter.prefix == 'myprefix'
  217. def test_set_log_prefix_skips_handlers_without_a_formatter():
  218. formatter = flexmock(prefix=None)
  219. flexmock(module.logging).should_receive('getLogger').and_return(
  220. flexmock(
  221. handlers=[
  222. flexmock(
  223. formatter=None,
  224. ),
  225. flexmock(
  226. formatter=formatter,
  227. ),
  228. ],
  229. removeHandler=lambda handler: None,
  230. )
  231. )
  232. module.set_log_prefix('myprefix')
  233. assert formatter.prefix == 'myprefix'
  234. def test_log_prefix_sets_prefix_and_then_restores_no_prefix_after():
  235. flexmock(module).should_receive('get_log_prefix').and_return(None)
  236. flexmock(module).should_receive('set_log_prefix').with_args('myprefix').once()
  237. flexmock(module).should_receive('set_log_prefix').with_args(None).once()
  238. with module.Log_prefix('myprefix'):
  239. pass
  240. def test_log_prefix_sets_prefix_and_then_restores_original_prefix_after():
  241. flexmock(module).should_receive('get_log_prefix').and_return('original')
  242. flexmock(module).should_receive('set_log_prefix').with_args('myprefix').once()
  243. flexmock(module).should_receive('set_log_prefix').with_args('original').once()
  244. with module.Log_prefix('myprefix'):
  245. pass
  246. def test_delayed_logging_handler_should_flush_without_targets_returns_false():
  247. handler = module.Delayed_logging_handler()
  248. assert handler.shouldFlush(flexmock()) is False
  249. def test_delayed_logging_handler_should_flush_with_targets_returns_true():
  250. handler = module.Delayed_logging_handler()
  251. handler.targets = [flexmock()]
  252. assert handler.shouldFlush(flexmock()) is True
  253. def test_delayed_logging_handler_flush_without_targets_does_not_raise():
  254. handler = module.Delayed_logging_handler()
  255. flexmock(handler).should_receive('acquire')
  256. flexmock(handler).should_receive('release')
  257. handler.flush()
  258. def test_delayed_logging_handler_flush_with_empty_buffer_does_not_raise():
  259. handler = module.Delayed_logging_handler()
  260. flexmock(handler).should_receive('acquire')
  261. flexmock(handler).should_receive('release')
  262. handler.targets = [flexmock()]
  263. handler.flush()
  264. def test_delayed_logging_handler_flush_forwards_each_record_to_each_target():
  265. handler = module.Delayed_logging_handler()
  266. flexmock(handler).should_receive('acquire')
  267. flexmock(handler).should_receive('release')
  268. handler.targets = [flexmock(level=logging.DEBUG), flexmock(level=logging.DEBUG)]
  269. handler.buffer = [flexmock(levelno=logging.DEBUG), flexmock(levelno=logging.DEBUG)]
  270. handler.targets[0].should_receive('handle').with_args(handler.buffer[0]).once()
  271. handler.targets[1].should_receive('handle').with_args(handler.buffer[0]).once()
  272. handler.targets[0].should_receive('handle').with_args(handler.buffer[1]).once()
  273. handler.targets[1].should_receive('handle').with_args(handler.buffer[1]).once()
  274. handler.flush()
  275. assert handler.buffer == []
  276. def test_delayed_logging_handler_flush_skips_forwarding_when_log_record_is_too_low_for_target():
  277. handler = module.Delayed_logging_handler()
  278. flexmock(handler).should_receive('acquire')
  279. flexmock(handler).should_receive('release')
  280. handler.targets = [flexmock(level=logging.INFO), flexmock(level=logging.DEBUG)]
  281. handler.buffer = [flexmock(levelno=logging.DEBUG), flexmock(levelno=logging.INFO)]
  282. handler.targets[0].should_receive('handle').with_args(handler.buffer[0]).never()
  283. handler.targets[1].should_receive('handle').with_args(handler.buffer[0]).once()
  284. handler.targets[0].should_receive('handle').with_args(handler.buffer[1]).once()
  285. handler.targets[1].should_receive('handle').with_args(handler.buffer[1]).once()
  286. handler.flush()
  287. assert handler.buffer == []
  288. def test_flush_delayed_logging_without_handlers_does_not_raise():
  289. root_logger = flexmock(handlers=[])
  290. root_logger.should_receive('removeHandler')
  291. flexmock(module.logging).should_receive('getLogger').and_return(root_logger)
  292. module.flush_delayed_logging([flexmock()])
  293. def test_flush_delayed_logging_without_delayed_logging_handler_does_not_raise():
  294. root_logger = flexmock(handlers=[flexmock()])
  295. root_logger.should_receive('removeHandler')
  296. flexmock(module.logging).should_receive('getLogger').and_return(root_logger)
  297. module.flush_delayed_logging([flexmock()])
  298. def test_flush_delayed_logging_flushes_delayed_logging_handler():
  299. delayed_logging_handler = module.Delayed_logging_handler()
  300. root_logger = flexmock(handlers=[delayed_logging_handler])
  301. flexmock(module.logging).should_receive('getLogger').and_return(root_logger)
  302. flexmock(delayed_logging_handler).should_receive('flush').once()
  303. root_logger.should_receive('removeHandler')
  304. module.flush_delayed_logging([flexmock()])
  305. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_linux():
  306. flexmock(module).should_receive('add_custom_log_levels')
  307. flexmock(module.logging).ANSWER = module.ANSWER
  308. fake_formatter = flexmock()
  309. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  310. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  311. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  312. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  313. flexmock(module).should_receive('interactive_console').and_return(False)
  314. flexmock(module).should_receive('flush_delayed_logging')
  315. flexmock(module.logging).should_receive('basicConfig').with_args(
  316. level=logging.DEBUG, handlers=list
  317. )
  318. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  319. syslog_handler = logging.handlers.SysLogHandler()
  320. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  321. address='/dev/log'
  322. ).and_return(syslog_handler).once()
  323. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  324. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_macos():
  325. flexmock(module).should_receive('add_custom_log_levels')
  326. flexmock(module.logging).ANSWER = module.ANSWER
  327. fake_formatter = flexmock()
  328. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  329. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  330. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  331. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  332. flexmock(module).should_receive('interactive_console').and_return(False)
  333. flexmock(module).should_receive('flush_delayed_logging')
  334. flexmock(module.logging).should_receive('basicConfig').with_args(
  335. level=logging.DEBUG, handlers=list
  336. )
  337. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  338. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  339. syslog_handler = logging.handlers.SysLogHandler()
  340. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  341. address='/var/run/syslog'
  342. ).and_return(syslog_handler).once()
  343. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  344. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_freebsd():
  345. flexmock(module).should_receive('add_custom_log_levels')
  346. flexmock(module.logging).ANSWER = module.ANSWER
  347. fake_formatter = flexmock()
  348. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  349. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  350. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  351. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  352. flexmock(module).should_receive('interactive_console').and_return(False)
  353. flexmock(module).should_receive('flush_delayed_logging')
  354. flexmock(module.logging).should_receive('basicConfig').with_args(
  355. level=logging.DEBUG, handlers=list
  356. )
  357. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  358. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
  359. flexmock(module.os.path).should_receive('exists').with_args('/var/run/log').and_return(True)
  360. syslog_handler = logging.handlers.SysLogHandler()
  361. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  362. address='/var/run/log'
  363. ).and_return(syslog_handler).once()
  364. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  365. def test_configure_logging_without_syslog_log_level_skips_syslog():
  366. flexmock(module).should_receive('add_custom_log_levels')
  367. flexmock(module.logging).ANSWER = module.ANSWER
  368. fake_formatter = flexmock()
  369. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  370. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  371. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  372. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  373. flexmock(module).should_receive('flush_delayed_logging')
  374. flexmock(module.logging).should_receive('basicConfig').with_args(
  375. level=logging.INFO, handlers=list
  376. )
  377. flexmock(module.os.path).should_receive('exists').never()
  378. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  379. module.configure_logging(console_log_level=logging.INFO)
  380. def test_configure_logging_skips_syslog_if_not_found():
  381. flexmock(module).should_receive('add_custom_log_levels')
  382. flexmock(module.logging).ANSWER = module.ANSWER
  383. fake_formatter = flexmock()
  384. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  385. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  386. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  387. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  388. flexmock(module).should_receive('flush_delayed_logging')
  389. flexmock(module.logging).should_receive('basicConfig').with_args(
  390. level=logging.INFO, handlers=list
  391. )
  392. flexmock(module.os.path).should_receive('exists').and_return(False)
  393. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  394. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  395. def test_configure_logging_skips_log_file_if_log_file_logging_is_disabled():
  396. flexmock(module).should_receive('add_custom_log_levels')
  397. flexmock(module.logging).DISABLED = module.DISABLED
  398. fake_formatter = flexmock()
  399. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  400. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  401. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  402. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  403. flexmock(module).should_receive('flush_delayed_logging')
  404. flexmock(module.logging).should_receive('basicConfig').with_args(
  405. level=logging.INFO, handlers=list
  406. )
  407. flexmock(module.os.path).should_receive('exists').never()
  408. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  409. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  410. module.configure_logging(
  411. console_log_level=logging.INFO, log_file_log_level=logging.DISABLED, log_file='/tmp/logfile'
  412. )
  413. def test_configure_logging_to_log_file_instead_of_syslog():
  414. flexmock(module).should_receive('add_custom_log_levels')
  415. flexmock(module.logging).ANSWER = module.ANSWER
  416. fake_formatter = flexmock()
  417. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  418. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  419. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  420. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  421. flexmock(module).should_receive('flush_delayed_logging')
  422. flexmock(module.logging).should_receive('basicConfig').with_args(
  423. level=logging.DEBUG, handlers=list
  424. )
  425. flexmock(module.os.path).should_receive('exists').never()
  426. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  427. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  428. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  429. '/tmp/logfile'
  430. ).and_return(file_handler).once()
  431. module.configure_logging(
  432. console_log_level=logging.INFO,
  433. syslog_log_level=logging.DISABLED,
  434. log_file_log_level=logging.DEBUG,
  435. log_file='/tmp/logfile',
  436. )
  437. def test_configure_logging_to_both_log_file_and_syslog():
  438. flexmock(module).should_receive('add_custom_log_levels')
  439. flexmock(module.logging).ANSWER = module.ANSWER
  440. fake_formatter = flexmock()
  441. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  442. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  443. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  444. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  445. flexmock(module).should_receive('flush_delayed_logging')
  446. flexmock(module.logging).should_receive('basicConfig').with_args(
  447. level=logging.DEBUG, handlers=list
  448. )
  449. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  450. syslog_handler = logging.handlers.SysLogHandler()
  451. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  452. address='/dev/log'
  453. ).and_return(syslog_handler).once()
  454. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  455. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  456. '/tmp/logfile'
  457. ).and_return(file_handler).once()
  458. module.configure_logging(
  459. console_log_level=logging.INFO,
  460. syslog_log_level=logging.DEBUG,
  461. log_file_log_level=logging.DEBUG,
  462. log_file='/tmp/logfile',
  463. )
  464. def test_configure_logging_to_log_file_formats_with_custom_log_format():
  465. flexmock(module).should_receive('add_custom_log_levels')
  466. flexmock(module.logging).ANSWER = module.ANSWER
  467. flexmock(module).should_receive('Log_prefix_formatter').with_args(
  468. '{message}', # noqa: FS003
  469. ).once()
  470. fake_formatter = flexmock()
  471. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  472. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  473. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  474. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  475. flexmock(module).should_receive('interactive_console').and_return(False)
  476. flexmock(module).should_receive('flush_delayed_logging')
  477. flexmock(module.logging).should_receive('basicConfig').with_args(
  478. level=logging.DEBUG, handlers=list
  479. )
  480. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  481. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  482. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  483. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  484. '/tmp/logfile'
  485. ).and_return(file_handler).once()
  486. module.configure_logging(
  487. console_log_level=logging.INFO,
  488. log_file_log_level=logging.DEBUG,
  489. log_file='/tmp/logfile',
  490. log_file_format='{message}', # noqa: FS003
  491. )
  492. def test_configure_logging_skips_log_file_if_argument_is_none():
  493. flexmock(module).should_receive('add_custom_log_levels')
  494. flexmock(module.logging).ANSWER = module.ANSWER
  495. fake_formatter = flexmock()
  496. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  497. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  498. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  499. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  500. flexmock(module).should_receive('flush_delayed_logging')
  501. flexmock(module.logging).should_receive('basicConfig').with_args(
  502. level=logging.INFO, handlers=list
  503. )
  504. flexmock(module.os.path).should_receive('exists').and_return(False)
  505. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  506. module.configure_logging(console_log_level=logging.INFO, log_file=None)
  507. def test_configure_logging_uses_console_no_color_formatter_if_color_disabled():
  508. flexmock(module).should_receive('add_custom_log_levels')
  509. flexmock(module.logging).ANSWER = module.ANSWER
  510. fake_formatter = flexmock()
  511. flexmock(module).should_receive('Console_color_formatter').never()
  512. flexmock(module).should_receive('Log_prefix_formatter').and_return(fake_formatter)
  513. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  514. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  515. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  516. flexmock(module).should_receive('flush_delayed_logging')
  517. flexmock(module.logging).should_receive('basicConfig').with_args(
  518. level=logging.INFO, handlers=list
  519. )
  520. flexmock(module.os.path).should_receive('exists').and_return(False)
  521. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  522. module.configure_logging(console_log_level=logging.INFO, log_file=None, color_enabled=False)