_logging.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. websocket - WebSocket client library for Python
  3. Copyright (C) 2010 Hiroki Ohtani(liris)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1335 USA
  16. """
  17. import logging
  18. _logger = logging.getLogger('websocket')
  19. _logger.addHandler(logging.NullHandler())
  20. _traceEnabled = False
  21. __all__ = ["enableTrace", "dump", "error", "warning", "debug", "trace",
  22. "isEnabledForError", "isEnabledForDebug"]
  23. def enableTrace(traceable):
  24. """
  25. turn on/off the traceability.
  26. traceable: boolean value. if set True, traceability is enabled.
  27. """
  28. global _traceEnabled
  29. _traceEnabled = traceable
  30. if traceable:
  31. if not _logger.handlers:
  32. _logger.addHandler(logging.StreamHandler())
  33. _logger.setLevel(logging.DEBUG)
  34. def dump(title, message):
  35. if _traceEnabled:
  36. _logger.debug("--- " + title + " ---")
  37. _logger.debug(message)
  38. _logger.debug("-----------------------")
  39. def error(msg):
  40. _logger.error(msg)
  41. def warning(msg):
  42. _logger.warning(msg)
  43. def debug(msg):
  44. _logger.debug(msg)
  45. def trace(msg):
  46. if _traceEnabled:
  47. _logger.debug(msg)
  48. def isEnabledForError():
  49. return _logger.isEnabledFor(logging.ERROR)
  50. def isEnabledForDebug():
  51. return _logger.isEnabledFor(logging.DEBUG)