test_daiquiri.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import json
  13. import logging
  14. import unittest
  15. import warnings
  16. import six.moves
  17. import daiquiri
  18. class TestDaiquiri(unittest.TestCase):
  19. def tearDown(self):
  20. # Be sure to reset the warning capture
  21. logging.captureWarnings(False)
  22. super(TestDaiquiri, self).tearDown()
  23. def test_setup(self):
  24. daiquiri.setup()
  25. daiquiri.setup(level=logging.DEBUG)
  26. daiquiri.setup(program_name="foobar")
  27. def test_setup_json_formatter(self):
  28. stream = six.moves.StringIO()
  29. daiquiri.setup(outputs=(
  30. daiquiri.output.Stream(
  31. stream, formatter=daiquiri.formatter.JSON_FORMATTER),
  32. ))
  33. daiquiri.getLogger(__name__).warning("foobar")
  34. self.assertEqual({"message": "foobar"},
  35. json.loads(stream.getvalue()))
  36. def test_setup_json_formatter_with_extras(self):
  37. stream = six.moves.StringIO()
  38. daiquiri.setup(outputs=(
  39. daiquiri.output.Stream(
  40. stream, formatter=daiquiri.formatter.JSON_FORMATTER),
  41. ))
  42. daiquiri.getLogger(__name__).warning("foobar", foo="bar")
  43. self.assertEqual({"message": "foobar", "foo": "bar"},
  44. json.loads(stream.getvalue()))
  45. def test_get_logger_set_level(self):
  46. logger = daiquiri.getLogger(__name__)
  47. logger.setLevel(logging.DEBUG)
  48. def test_capture_warnings(self):
  49. stream = six.moves.StringIO()
  50. daiquiri.setup(outputs=(
  51. daiquiri.output.Stream(stream),
  52. ))
  53. warnings.warn("omg!")
  54. line = stream.getvalue()
  55. self.assertIn("WARNING py.warnings: ", line)
  56. self.assertIn("daiquiri/tests/test_daiquiri.py:62: "
  57. "UserWarning: omg!\n warnings.warn(\"omg!\")\n",
  58. line)
  59. def test_no_capture_warnings(self):
  60. stream = six.moves.StringIO()
  61. daiquiri.setup(outputs=(
  62. daiquiri.output.Stream(stream),
  63. ), capture_warnings=False)
  64. warnings.warn("omg!")
  65. self.assertEqual("", stream.getvalue())
  66. def test_set_default_log_levels(self):
  67. daiquiri.set_default_log_levels((("amqp", "debug"),
  68. ("urllib3", "warn")))
  69. def test_parse_and_set_default_log_levels(self):
  70. daiquiri.parse_and_set_default_log_levels(
  71. ("urllib3=warn", "foobar=debug"))
  72. def test_string_as_setup_outputs_arg(self):
  73. daiquiri.setup(outputs=('stderr', 'stdout'))
  74. if daiquiri.handlers.syslog is not None:
  75. daiquiri.setup(outputs=('syslog',))
  76. if daiquiri.handlers.journal is not None:
  77. daiquiri.setup(outputs=('journal',))