logger.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  2. # McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. Logger for Syslog
  17. """
  18. logger = None
  19. location = None
  20. queue = []
  21. import threading
  22. loglock = threading.Lock()
  23. def setLogger(loc, address, loglevel):
  24. """
  25. Sets the logger object
  26. :param loc: location of the server, to prepend to every logged message
  27. :param address: the address of the syslog server in the form of (ip-address, port)
  28. :param loglevel: the level of logging to perform, should be one specified in the logging module
  29. """
  30. if loglevel is None:
  31. return
  32. global logger
  33. if logger is not None:
  34. # A logger is already set, so ignore this one
  35. return
  36. import logging
  37. import logging.handlers
  38. handler = logging.handlers.SysLogHandler(address, facility=19)
  39. local_logger = logging.getLogger('PyPDEVS-logging')
  40. local_logger.addHandler(handler)
  41. local_logger.setLevel(loglevel)
  42. global location
  43. location = loc
  44. # Now make the logger 'public'
  45. logger = local_logger
  46. def log(level, msg, logger):
  47. """
  48. Do the actual logging at the specified level, but save it in case no logger exists yet
  49. :param level: string representation of the function to call on the logger
  50. :param msg: the message to log
  51. :returns: True -- to allow it as an #assert statement
  52. """
  53. with loglock:
  54. global location
  55. global queue
  56. if len(msg) > 80:
  57. msg = msg[:79]
  58. if logger is not None:
  59. # Flush the queue first
  60. for level1, msg1 in queue:
  61. getattr(logger, level1)("%s -- %s" % (location, msg1))
  62. queue = []
  63. getattr(logger, level)("%s -- %s" % (location, msg))
  64. else:
  65. queue.append((level, msg))
  66. return True
  67. def debug(msg):
  68. """
  69. Debug logging statement
  70. :param msg: the message to print
  71. :returns: True -- to allow it as an #assert statement
  72. """
  73. return log("debug", msg, logger)
  74. def info(msg):
  75. """
  76. Informational logging statement
  77. :param msg: the message to print
  78. :returns: True -- to allow it as an #assert statement
  79. """
  80. return log("info", msg, logger)
  81. def warn(msg):
  82. """
  83. Warning logging statement
  84. :param msg: the message to print
  85. :returns: True -- to allow it as an #assert statement
  86. """
  87. return log("warn", msg, logger)
  88. def error(msg):
  89. """
  90. Error logging statement
  91. :param msg: the message to print
  92. :returns: True -- to allow it as an #assert statement
  93. """
  94. return log("error", msg, logger)