test_logger.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. <<<<<<< HEAD
  285. =======
  286. def test_delayed_logging_handler_flush_skips_forwarding_when_log_record_is_too_low_for_target():
  287. handler = module.Delayed_logging_handler()
  288. flexmock(handler).should_receive('acquire')
  289. flexmock(handler).should_receive('release')
  290. handler.targets = [flexmock(level=logging.INFO), flexmock(level=logging.DEBUG)]
  291. handler.buffer = [flexmock(levelno=logging.DEBUG), flexmock(levelno=logging.INFO)]
  292. handler.targets[0].should_receive('handle').with_args(handler.buffer[0]).never()
  293. handler.targets[1].should_receive('handle').with_args(handler.buffer[0]).once()
  294. handler.targets[0].should_receive('handle').with_args(handler.buffer[1]).once()
  295. handler.targets[1].should_receive('handle').with_args(handler.buffer[1]).once()
  296. handler.flush()
  297. assert handler.buffer == []
  298. >>>>>>> main
  299. def test_flush_delayed_logging_without_handlers_does_not_raise():
  300. root_logger = flexmock(handlers=[])
  301. root_logger.should_receive('removeHandler')
  302. flexmock(module.logging).should_receive('getLogger').and_return(root_logger)
  303. module.flush_delayed_logging([flexmock()])
  304. def test_flush_delayed_logging_without_delayed_logging_handler_does_not_raise():
  305. root_logger = flexmock(handlers=[flexmock()])
  306. root_logger.should_receive('removeHandler')
  307. flexmock(module.logging).should_receive('getLogger').and_return(root_logger)
  308. module.flush_delayed_logging([flexmock()])
  309. def test_flush_delayed_logging_flushes_delayed_logging_handler():
  310. delayed_logging_handler = module.Delayed_logging_handler()
  311. root_logger = flexmock(handlers=[delayed_logging_handler])
  312. flexmock(module.logging).should_receive('getLogger').and_return(root_logger)
  313. flexmock(delayed_logging_handler).should_receive('flush').once()
  314. root_logger.should_receive('removeHandler')
  315. module.flush_delayed_logging([flexmock()])
  316. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_linux():
  317. flexmock(module).should_receive('add_custom_log_levels')
  318. flexmock(module.logging).ANSWER = module.ANSWER
  319. fake_formatter = flexmock()
  320. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  321. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  322. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  323. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  324. flexmock(module).should_receive('interactive_console').and_return(False)
  325. flexmock(module).should_receive('flush_delayed_logging')
  326. flexmock(module.logging).should_receive('basicConfig').with_args(
  327. level=logging.DEBUG, handlers=list
  328. )
  329. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  330. syslog_handler = logging.handlers.SysLogHandler()
  331. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  332. address='/dev/log'
  333. ).and_return(syslog_handler).once()
  334. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  335. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_macos():
  336. flexmock(module).should_receive('add_custom_log_levels')
  337. flexmock(module.logging).ANSWER = module.ANSWER
  338. fake_formatter = flexmock()
  339. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  340. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  341. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  342. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  343. flexmock(module).should_receive('interactive_console').and_return(False)
  344. flexmock(module).should_receive('flush_delayed_logging')
  345. flexmock(module.logging).should_receive('basicConfig').with_args(
  346. level=logging.DEBUG, handlers=list
  347. )
  348. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  349. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  350. syslog_handler = logging.handlers.SysLogHandler()
  351. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  352. address='/var/run/syslog'
  353. ).and_return(syslog_handler).once()
  354. module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
  355. def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_freebsd():
  356. flexmock(module).should_receive('add_custom_log_levels')
  357. flexmock(module.logging).ANSWER = module.ANSWER
  358. fake_formatter = flexmock()
  359. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  360. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  361. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  362. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  363. flexmock(module).should_receive('interactive_console').and_return(False)
  364. flexmock(module).should_receive('flush_delayed_logging')
  365. flexmock(module.logging).should_receive('basicConfig').with_args(
  366. level=logging.DEBUG, 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, handlers=list
  387. )
  388. flexmock(module.os.path).should_receive('exists').never()
  389. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  390. module.configure_logging(console_log_level=logging.INFO)
  391. def test_configure_logging_skips_syslog_if_not_found():
  392. flexmock(module).should_receive('add_custom_log_levels')
  393. flexmock(module.logging).ANSWER = module.ANSWER
  394. fake_formatter = flexmock()
  395. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  396. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  397. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  398. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  399. flexmock(module).should_receive('flush_delayed_logging')
  400. flexmock(module.logging).should_receive('basicConfig').with_args(
  401. level=logging.INFO, handlers=list
  402. )
  403. flexmock(module.os.path).should_receive('exists').and_return(False)
  404. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  405. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  406. def test_configure_logging_skips_log_file_if_log_file_logging_is_disabled():
  407. flexmock(module).should_receive('add_custom_log_levels')
  408. flexmock(module.logging).DISABLED = module.DISABLED
  409. fake_formatter = flexmock()
  410. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  411. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  412. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  413. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  414. flexmock(module).should_receive('flush_delayed_logging')
  415. flexmock(module.logging).should_receive('basicConfig').with_args(
  416. level=logging.INFO, handlers=list
  417. )
  418. flexmock(module.os.path).should_receive('exists').never()
  419. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  420. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  421. module.configure_logging(
  422. console_log_level=logging.INFO, log_file_log_level=logging.DISABLED, log_file='/tmp/logfile'
  423. )
  424. def test_configure_logging_to_log_file_instead_of_syslog():
  425. flexmock(module).should_receive('add_custom_log_levels')
  426. flexmock(module.logging).ANSWER = module.ANSWER
  427. fake_formatter = flexmock()
  428. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  429. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  430. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  431. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  432. flexmock(module).should_receive('flush_delayed_logging')
  433. flexmock(module.logging).should_receive('basicConfig').with_args(
  434. level=logging.DEBUG, handlers=list
  435. )
  436. flexmock(module.os.path).should_receive('exists').never()
  437. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  438. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  439. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  440. '/tmp/logfile'
  441. ).and_return(file_handler).once()
  442. module.configure_logging(
  443. console_log_level=logging.INFO,
  444. syslog_log_level=logging.DISABLED,
  445. log_file_log_level=logging.DEBUG,
  446. log_file='/tmp/logfile',
  447. )
  448. def test_configure_logging_to_both_log_file_and_syslog():
  449. flexmock(module).should_receive('add_custom_log_levels')
  450. flexmock(module.logging).ANSWER = module.ANSWER
  451. fake_formatter = flexmock()
  452. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  453. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  454. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  455. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  456. flexmock(module).should_receive('flush_delayed_logging')
  457. flexmock(module.logging).should_receive('basicConfig').with_args(
  458. level=logging.DEBUG, handlers=list
  459. )
  460. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  461. syslog_handler = logging.handlers.SysLogHandler()
  462. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  463. address='/dev/log'
  464. ).and_return(syslog_handler).once()
  465. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  466. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  467. '/tmp/logfile'
  468. ).and_return(file_handler).once()
  469. module.configure_logging(
  470. console_log_level=logging.INFO,
  471. syslog_log_level=logging.DEBUG,
  472. log_file_log_level=logging.DEBUG,
  473. log_file='/tmp/logfile',
  474. )
  475. def test_configure_logging_to_log_file_formats_with_custom_log_format():
  476. flexmock(module).should_receive('add_custom_log_levels')
  477. flexmock(module.logging).ANSWER = module.ANSWER
  478. flexmock(module).should_receive('Log_prefix_formatter').with_args(
  479. '{message}', # noqa: FS003
  480. ).once()
  481. fake_formatter = flexmock()
  482. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  483. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  484. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  485. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  486. flexmock(module).should_receive('interactive_console').and_return(False)
  487. flexmock(module).should_receive('flush_delayed_logging')
  488. flexmock(module.logging).should_receive('basicConfig').with_args(
  489. level=logging.DEBUG, handlers=list
  490. )
  491. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  492. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  493. file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
  494. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
  495. '/tmp/logfile'
  496. ).and_return(file_handler).once()
  497. module.configure_logging(
  498. console_log_level=logging.INFO,
  499. log_file_log_level=logging.DEBUG,
  500. log_file='/tmp/logfile',
  501. log_file_format='{message}', # noqa: FS003
  502. )
  503. def test_configure_logging_skips_log_file_if_argument_is_none():
  504. flexmock(module).should_receive('add_custom_log_levels')
  505. flexmock(module.logging).ANSWER = module.ANSWER
  506. fake_formatter = flexmock()
  507. flexmock(module).should_receive('Console_color_formatter').and_return(fake_formatter)
  508. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  509. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  510. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  511. flexmock(module).should_receive('flush_delayed_logging')
  512. flexmock(module.logging).should_receive('basicConfig').with_args(
  513. level=logging.INFO, handlers=list
  514. )
  515. flexmock(module.os.path).should_receive('exists').and_return(False)
  516. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  517. module.configure_logging(console_log_level=logging.INFO, log_file=None)
  518. def test_configure_logging_uses_console_no_color_formatter_if_color_disabled():
  519. flexmock(module).should_receive('add_custom_log_levels')
  520. flexmock(module.logging).ANSWER = module.ANSWER
  521. fake_formatter = flexmock()
  522. flexmock(module).should_receive('Console_color_formatter').never()
  523. flexmock(module).should_receive('Log_prefix_formatter').and_return(fake_formatter)
  524. multi_stream_handler = flexmock(setLevel=lambda level: None, level=logging.INFO)
  525. multi_stream_handler.should_receive('setFormatter').with_args(fake_formatter).once()
  526. flexmock(module).should_receive('Multi_stream_handler').and_return(multi_stream_handler)
  527. flexmock(module).should_receive('flush_delayed_logging')
  528. flexmock(module.logging).should_receive('basicConfig').with_args(
  529. level=logging.INFO, handlers=list
  530. )
  531. flexmock(module.os.path).should_receive('exists').and_return(False)
  532. flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
  533. module.configure_logging(console_log_level=logging.INFO, log_file=None, color_enabled=False)