_logging.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. try:
  20. from logging import NullHandler
  21. except ImportError:
  22. class NullHandler(logging.Handler):
  23. def emit(self, record):
  24. pass
  25. _logger.addHandler(NullHandler())
  26. _traceEnabled = False
  27. __all__ = ["enableTrace", "dump", "error", "warning", "debug", "trace",
  28. "isEnabledForError", "isEnabledForDebug"]
  29. def enableTrace(traceable, handler = logging.StreamHandler()):
  30. """
  31. turn on/off the traceability.
  32. traceable: boolean value. if set True, traceability is enabled.
  33. """
  34. global _traceEnabled
  35. _traceEnabled = traceable
  36. if traceable:
  37. _logger.addHandler(handler)
  38. _logger.setLevel(logging.DEBUG)
  39. def dump(title, message):
  40. if _traceEnabled:
  41. _logger.debug("--- " + title + " ---")
  42. _logger.debug(message)
  43. _logger.debug("-----------------------")
  44. def error(msg):
  45. _logger.error(msg)
  46. def warning(msg):
  47. _logger.warning(msg)
  48. def debug(msg):
  49. _logger.debug(msg)
  50. def trace(msg):
  51. if _traceEnabled:
  52. _logger.debug(msg)
  53. def isEnabledForError():
  54. return _logger.isEnabledFor(logging.ERROR)
  55. def isEnabledForDebug():
  56. return _logger.isEnabledFor(logging.DEBUG)