test_logger.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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,
  325. handlers=list,
  326. )
  327. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  328. syslog_handler = logging.handlers.SysLogHandler()
  329. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  330. address='/dev/log',
  331. ).and_return(syslog_handler).once()
  332. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  333. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_macos():
  334. flexmock(module).should_receive('add_custom_log_levels')
  335. flexmock(module.logging).ANSWER = module.ANSWER
  336. fake_formatter = flexmock()
  337. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  338. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  339. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  340. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  341. flexmock(module).should_receive('interactive_console').and_return(False)
  342. flexmock(module).should_receive('flush_delayed_logging')
  343. flexmock(module.logging).should_receive('basicConfig').with_args(
  344. level=logging.DEBUG,
  345. handlers=list,
  346. )
  347. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  348. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  349. syslog_handler = logging.handlers.SysLogHandler()
  350. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  351. address='/var/run/syslog',
  352. ).and_return(syslog_handler).once()
  353. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  354. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_freebsd():
  355. flexmock(module).should_receive('add_custom_log_levels')
  356. flexmock(module.logging).ANSWER = module.ANSWER
  357. fake_formatter = flexmock()
  358. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  359. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  360. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  361. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  362. flexmock(module).should_receive('interactive_console').and_return(False)
  363. flexmock(module).should_receive('flush_delayed_logging')
  364. flexmock(module.logging).should_receive('basicConfig').with_args(
  365. level=logging.DEBUG,
  366. handlers=list,
  367. )
  368. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  369. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
  370. flexmock(module.os.path).should_receive('exists').with_args('/var/run/log').and_return(True)
  371. syslog_handler = logging.handlers.SysLogHandler()
  372. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  373. address='/var/run/log',
  374. ).and_return(syslog_handler).once()
  375. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  376. def test_configure_logging_without_syslog_log_level_skips_syslog():
  377. flexmock(module).should_receive('add_custom_log_levels')
  378. flexmock(module.logging).ANSWER = module.ANSWER
  379. fake_formatter = flexmock()
  380. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  381. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  382. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  383. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  384. flexmock(module).should_receive('flush_delayed_logging')
  385. flexmock(module.logging).should_receive('basicConfig').with_args(
  386. level=logging.INFO,
  387. handlers=list,
  388. )
  389. flexmock(module.os.path).should_receive('exists').never()
  390. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  391. module.configure_logging(console_log_level=logging.INFO)
  392. def test_configure_logging_skips_syslog_if_not_found():
  393. flexmock(module).should_receive('add_custom_log_levels')
  394. flexmock(module.logging).ANSWER = module.ANSWER
  395. fake_formatter = flexmock()
  396. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  397. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  398. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  399. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  400. flexmock(module).should_receive('flush_delayed_logging')
  401. flexmock(module.logging).should_receive('basicConfig').with_args(
  402. level=logging.INFO,
  403. handlers=list,
  404. )
  405. flexmock(module.os.path).should_receive('exists').and_return(False)
  406. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  407. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  408. def test_configure_logging_skips_log_file_if_log_file_logging_is_disabled():
  409. flexmock(module).should_receive('add_custom_log_levels')
  410. flexmock(module.logging).DISABLED = module.DISABLED
  411. fake_formatter = flexmock()
  412. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  413. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  414. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  415. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  416. flexmock(module).should_receive('flush_delayed_logging')
  417. flexmock(module.logging).should_receive('basicConfig').with_args(
  418. level=logging.INFO,
  419. handlers=list,
  420. )
  421. flexmock(module.os.path).should_receive('exists').never()
  422. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  423. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  424. module.configure_logging(
  425. console_log_level=logging.INFO,
  426. log_file_log_level=logging.DISABLED,
  427. log_file='/tmp/logfile',
  428. )
  429. def test_configure_logging_to_log_file_instead_of_syslog():
  430. flexmock(module).should_receive('add_custom_log_levels')
  431. flexmock(module.logging).ANSWER = module.ANSWER
  432. fake_formatter = flexmock()
  433. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  434. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  435. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  436. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  437. flexmock(module).should_receive('flush_delayed_logging')
  438. flexmock(module.logging).should_receive('basicConfig').with_args(
  439. level=logging.DEBUG,
  440. handlers=list,
  441. )
  442. flexmock(module.os.path).should_receive('exists').never()
  443. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  444. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  445. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  446. '/tmp/logfile',
  447. ).and_return(file_handler).once()
  448. module.configure_logging(
  449. console_log_level=logging.INFO,
  450. syslog_log_level=logging.DISABLED,
  451. log_file_log_level=logging.DEBUG,
  452. log_file='/tmp/logfile',
  453. )
  454. def test_configure_logging_to_both_log_file_and_syslog():
  455. flexmock(module).should_receive('add_custom_log_levels')
  456. flexmock(module.logging).ANSWER = module.ANSWER
  457. fake_formatter = flexmock()
  458. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  459. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  460. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  461. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  462. flexmock(module).should_receive('flush_delayed_logging')
  463. flexmock(module.logging).should_receive('basicConfig').with_args(
  464. level=logging.DEBUG,
  465. handlers=list,
  466. )
  467. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  468. syslog_handler = logging.handlers.SysLogHandler()
  469. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  470. address='/dev/log',
  471. ).and_return(syslog_handler).once()
  472. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  473. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  474. '/tmp/logfile',
  475. ).and_return(file_handler).once()
  476. module.configure_logging(
  477. console_log_level=logging.INFO,
  478. syslog_log_level=logging.DEBUG,
  479. log_file_log_level=logging.DEBUG,
  480. log_file='/tmp/logfile',
  481. )
  482. def test_configure_logging_to_log_file_formats_with_custom_log_format():
  483. flexmock(module).should_receive('add_custom_log_levels')
  484. flexmock(module.logging).ANSWER = module.ANSWER
  485. flexmock(module).should_receive('Log_prefix_formatter').with_args(
  486. '{message}',
  487. ).once()
  488. fake_formatter = flexmock()
  489. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  490. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  491. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  492. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  493. flexmock(module).should_receive('interactive_console').and_return(False)
  494. flexmock(module).should_receive('flush_delayed_logging')
  495. flexmock(module.logging).should_receive('basicConfig').with_args(
  496. level=logging.DEBUG,
  497. handlers=list,
  498. )
  499. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  500. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  501. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  502. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  503. '/tmp/logfile',
  504. ).and_return(file_handler).once()
  505. module.configure_logging(
  506. console_log_level=logging.INFO,
  507. log_file_log_level=logging.DEBUG,
  508. log_file='/tmp/logfile',
  509. log_file_format='{message}',
  510. )
  511. def test_configure_logging_skips_log_file_if_argument_is_none():
  512. flexmock(module).should_receive('add_custom_log_levels')
  513. flexmock(module.logging).ANSWER = module.ANSWER
  514. fake_formatter = flexmock()
  515. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  516. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  517. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  518. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  519. flexmock(module).should_receive('flush_delayed_logging')
  520. flexmock(module.logging).should_receive('basicConfig').with_args(
  521. level=logging.INFO,
  522. handlers=list,
  523. )
  524. flexmock(module.os.path).should_receive('exists').and_return(False)
  525. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  526. module.configure_logging(console_log_level=logging.INFO, log_file=None)
  527. def test_configure_logging_uses_console_no_color_formatter_if_color_disabled():
  528. flexmock(module).should_receive('add_custom_log_levels')
  529. flexmock(module.logging).ANSWER = module.ANSWER
  530. fake_formatter = flexmock()
  531. flexmock(module).should_receive('Console_color_formatter').never()
  532. flexmock(module).should_receive('Log_prefix_formatter').and_return(fake_formatter)
  533. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  534. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  535. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  536. flexmock(module).should_receive('flush_delayed_logging')
  537. flexmock(module.logging).should_receive('basicConfig').with_args(
  538. level=logging.INFO,
  539. handlers=list,
  540. )
  541. flexmock(module.os.path).should_receive('exists').and_return(False)
  542. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  543. module.configure_logging(console_log_level=logging.INFO, log_file=None, color_enabled=False)