logger.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 has been
  8. ### contributed to the open62541 project by the author. All licensing
  9. ### terms for this source is inherited by the terms and conditions
  10. ### specified for by the open62541 project (see the projects readme
  11. ### file for more information on the LGPL terms and restrictions).
  12. ###
  13. ### This program is not meant to be used in a production environment. The
  14. ### author is not liable for any complications arising due to the use of
  15. ### this program.
  16. ###
  17. import inspect
  18. ###
  19. ### Tidy logging helpers
  20. ###
  21. LOG_LEVEL_DEBUG = 4
  22. LOG_LEVEL_INFO = 2
  23. LOG_LEVEL_WARN = 1
  24. LOG_LEVEL_ERROR = 0
  25. LOG_LEVEL_SILENT = -1
  26. # Change the following to filter logging output
  27. GLOBAL_LOG_LEVEL = LOG_LEVEL_SILENT
  28. def log(callee, logstr, level=LOG_LEVEL_DEBUG):
  29. prefixes = { LOG_LEVEL_DEBUG : "DBG: ",
  30. LOG_LEVEL_INFO : "INF: ",
  31. LOG_LEVEL_WARN : "WRN: ",
  32. LOG_LEVEL_ERROR : "ERR: ",
  33. LOG_LEVEL_SILENT: ""
  34. }
  35. if level <= GLOBAL_LOG_LEVEL:
  36. if prefixes.has_key(level):
  37. print str(prefixes[level]) + callee.__class__.__name__ + "." + inspect.stack()[1][3] + "(): " + logstr
  38. else:
  39. print callee.__class__.__name__ + "." + inspect.stack()[1][3] + "(): " + logstr