_logging.py 2.2 KB

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