output.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import datetime
  13. import inspect
  14. import logging
  15. import logging.handlers
  16. import numbers
  17. import os
  18. import sys
  19. try:
  20. import syslog
  21. except ImportError:
  22. syslog = None
  23. from daiquiri import formatter
  24. from daiquiri import handlers
  25. def get_program_name():
  26. return os.path.basename(inspect.stack()[-1][1])
  27. class Output(object):
  28. """Generic log output."""
  29. def __init__(self, handler, formatter=formatter.TEXT_FORMATTER,
  30. level=None):
  31. self.handler = handler
  32. self.handler.setFormatter(formatter)
  33. if level is not None:
  34. self.handler.setLevel(level)
  35. def add_to_logger(self, logger):
  36. """Add this output to a logger."""
  37. logger.addHandler(self.handler)
  38. def _get_log_file_path(logfile=None, logdir=None, program_name=None,
  39. logfile_suffix=".log"):
  40. ret_path = None
  41. if not logdir:
  42. ret_path = logfile
  43. if not ret_path and logfile and logdir:
  44. ret_path = os.path.join(logdir, logfile)
  45. if not ret_path and logdir:
  46. program_name = program_name or get_program_name()
  47. ret_path = os.path.join(logdir, program_name) + logfile_suffix
  48. if not ret_path:
  49. raise ValueError("Unable to determine log file destination")
  50. return ret_path
  51. class File(Output):
  52. """Ouput to a file."""
  53. def __init__(self, filename=None, directory=None, suffix=".log",
  54. program_name=None, formatter=formatter.TEXT_FORMATTER,
  55. level=None):
  56. """Log file output.
  57. :param filename: The log file path to write to.
  58. If directory is also specified, both will be combined.
  59. :param directory: The log directory to write to.
  60. If no filename is specified, the program name and suffix will be used
  61. to contruct the full path relative to the directory.
  62. :param suffix: The log file name suffix.
  63. This will be only used if no filename has been provided.
  64. :param program_name: Program name. Autodetected by default.
  65. """
  66. logpath = _get_log_file_path(filename, directory,
  67. program_name, suffix)
  68. handler = logging.handlers.WatchedFileHandler(logpath)
  69. super(File, self).__init__(handler, formatter, level)
  70. class RotatingFile(Output):
  71. """Output to a file, rotating after a certain size."""
  72. def __init__(self, filename=None, directory=None, suffix='.log',
  73. program_name=None, formatter=formatter.TEXT_FORMATTER,
  74. level=None, max_size_bytes=0, backup_count=0):
  75. """Rotating log file output.
  76. :param filename: The log file path to write to.
  77. If directory is also specified, both will be combined.
  78. :param directory: The log directory to write to.
  79. If no filename is specified, the program name and suffix will be used
  80. to contruct the full path relative to the directory.
  81. :param suffix: The log file name suffix.
  82. This will be only used if no filename has been provided.
  83. :param program_name: Program name. Autodetected by default.
  84. :param max_size_bytes: allow the file to rollover at a
  85. predetermined size.
  86. :param backup_count: the maximum number of files to rotate
  87. logging output between.
  88. """
  89. logpath = _get_log_file_path(filename, directory,
  90. program_name, suffix)
  91. handler = logging.handlers.RotatingFileHandler(
  92. logpath, maxBytes=max_size_bytes, backupCount=backup_count)
  93. super(RotatingFile, self).__init__(handler, formatter, level)
  94. def do_rollover(self):
  95. """Manually forces a log file rotation."""
  96. return self.handler.doRollover()
  97. class TimedRotatingFile(Output):
  98. """Rotating log file output, triggered by a fixed interval."""
  99. def __init__(self, filename=None, directory=None, suffix='.log',
  100. program_name=None, formatter=formatter.TEXT_FORMATTER,
  101. level=None, interval=datetime.timedelta(hours=24),
  102. backup_count=0):
  103. """Rotating log file output, triggered by a fixed interval.
  104. :param filename: The log file path to write to.
  105. If directory is also specified, both will be combined.
  106. :param directory: The log directory to write to.
  107. If no filename is specified, the program name and suffix will be used
  108. to contruct the full path relative to the directory.
  109. :param suffix: The log file name suffix.
  110. This will be only used if no filename has been provided.
  111. :param program_name: Program name. Autodetected by default.
  112. :param interval: datetime.timedelta instance representing
  113. how often a new log file should be created.
  114. :param backup_count: the maximum number of files to rotate
  115. logging output between.
  116. """
  117. logpath = _get_log_file_path(filename, directory,
  118. program_name, suffix)
  119. handler = logging.handlers.TimedRotatingFileHandler(
  120. logpath,
  121. when='S',
  122. interval=self._timedelta_to_seconds(interval),
  123. backupCount=backup_count)
  124. super(TimedRotatingFile, self).__init__(handler, formatter, level)
  125. def do_rollover(self):
  126. """Manually forces a log file rotation."""
  127. return self.handler.doRollover()
  128. @staticmethod
  129. def _timedelta_to_seconds(td):
  130. """Convert a datetime.timedelta object into a seconds interval for
  131. rotating file ouput.
  132. :param td: datetime.timedelta
  133. :return: time in seconds
  134. :rtype: int
  135. """
  136. if isinstance(td, numbers.Real):
  137. td = datetime.timedelta(seconds=td)
  138. return td.total_seconds()
  139. class Stream(Output):
  140. """Generic stream output."""
  141. def __init__(self, stream=sys.stderr, formatter=formatter.TEXT_FORMATTER,
  142. level=None):
  143. super(Stream, self).__init__(handlers.TTYDetectorStreamHandler(stream),
  144. formatter, level)
  145. STDERR = Stream()
  146. STDOUT = Stream(sys.stdout)
  147. class Journal(Output):
  148. def __init__(self, program_name=None,
  149. formatter=formatter.TEXT_FORMATTER, level=None):
  150. program_name = program_name or get_program_name
  151. super(Journal, self).__init__(handlers.JournalHandler(program_name),
  152. formatter, level)
  153. class Syslog(Output):
  154. def __init__(self, program_name=None, facility="user",
  155. formatter=formatter.TEXT_FORMATTER, level=None):
  156. if syslog is None:
  157. # FIXME(jd) raise something more specific
  158. raise RuntimeError("syslog is not available on this platform")
  159. super(Syslog, self).__init__(
  160. handlers.SyslogHandler(
  161. program_name=program_name or get_program_name(),
  162. facility=self._find_facility(facility)),
  163. formatter, level)
  164. @staticmethod
  165. def _find_facility(facility):
  166. # NOTE(jd): Check the validity of facilities at run time as they differ
  167. # depending on the OS and Python version being used.
  168. valid_facilities = [f for f in
  169. ["LOG_KERN", "LOG_USER", "LOG_MAIL",
  170. "LOG_DAEMON", "LOG_AUTH", "LOG_SYSLOG",
  171. "LOG_LPR", "LOG_NEWS", "LOG_UUCP",
  172. "LOG_CRON", "LOG_AUTHPRIV", "LOG_FTP",
  173. "LOG_LOCAL0", "LOG_LOCAL1", "LOG_LOCAL2",
  174. "LOG_LOCAL3", "LOG_LOCAL4", "LOG_LOCAL5",
  175. "LOG_LOCAL6", "LOG_LOCAL7"]
  176. if getattr(syslog, f, None)]
  177. facility = facility.upper()
  178. if not facility.startswith("LOG_"):
  179. facility = "LOG_" + facility
  180. if facility not in valid_facilities:
  181. raise TypeError('syslog facility must be one of: %s' %
  182. ', '.join("'%s'" % fac
  183. for fac in valid_facilities))
  184. return getattr(syslog, facility)
  185. preconfigured = {
  186. 'stderr': STDERR,
  187. 'stdout': STDOUT,
  188. }
  189. if syslog is not None:
  190. preconfigured['syslog'] = Syslog()
  191. if handlers.journal is not None:
  192. preconfigured['journal'] = Journal()