logger.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env/python
  2. # -*- coding: utf-8 -*-
  3. ###
  4. ### Author: Chris Iatrou (ichrispa@core-vector.net)
  5. ### Version: rev 13
  6. ###
  7. ### This program was created for educational purposes and is released into the
  8. ### public domain under the General Public Licence. A copy of the GNU GPL is
  9. ### available under http://www.gnu.org/licenses/gpl-3.0.html.
  10. ###
  11. ### This program is not meant to be used in a production environment. The
  12. ### author is not liable for any complications arising due to the use of
  13. ### this program.
  14. ###
  15. import inspect
  16. ###
  17. ### Tidy logging helpers
  18. ###
  19. LOG_LEVEL_DEBUG = 4
  20. LOG_LEVEL_INFO = 2
  21. LOG_LEVEL_WARN = 1
  22. LOG_LEVEL_ERROR = 0
  23. LOG_LEVEL_SILENT = -1
  24. # Change the following to filter logging output
  25. GLOBAL_LOG_LEVEL = LOG_LEVEL_SILENT
  26. def log(callee, logstr, level=LOG_LEVEL_DEBUG):
  27. prefixes = { LOG_LEVEL_DEBUG : "DBG: ",
  28. LOG_LEVEL_INFO : "INF: ",
  29. LOG_LEVEL_WARN : "WRN: ",
  30. LOG_LEVEL_ERROR : "ERR: ",
  31. LOG_LEVEL_SILENT: ""
  32. }
  33. if level <= GLOBAL_LOG_LEVEL:
  34. if prefixes.has_key(level):
  35. print str(prefixes[level]) + callee.__class__.__name__ + "." + inspect.stack()[1][3] + "(): " + logstr
  36. else:
  37. print callee.__class__.__name__ + "." + inspect.stack()[1][3] + "(): " + logstr