test_logger.py 29 KB

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