log_buffer.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. """
  18. We would like to keep the last X characters
  19. of log message around for us to view in case of emergency.
  20. This log handler lets us do that.
  21. """
  22. import logging, collections
  23. class FixedBuffer(object):
  24. """
  25. The what: a buffer that maintains a fixed-size sliding window on
  26. the log history. As messages come in, old messages get pushed out.
  27. The plan: use a deque to keep a list of messages by reference (so
  28. minimal copying required). If the total size in characters exceeds
  29. some maximum, pop off messages until we get below the max, and then
  30. pad back up with the last maxsize-size characters of the most recently
  31. removed message to bring us back up to the maximum.
  32. Net cost is eventually one string copy per insert and a linear amount of
  33. reference manipulation. Benefit is the ability to save a slice through
  34. the really big messages (although huge messages are rare) rather than
  35. lose them completely when they get popped.
  36. """
  37. def __init__(self, maxsize=50000):
  38. """
  39. maxsize is in characters, not bytes.
  40. """
  41. self.buffer = collections.deque()
  42. self.maxsize = maxsize
  43. self.size = 0
  44. def insert(self, message):
  45. self.size += len(message)
  46. self.buffer.append(message)
  47. if self.size > self.maxsize:
  48. while self.size > self.maxsize:
  49. last = self.buffer.popleft()
  50. self.size -= len(last)
  51. # Prepend only as many characters of the outgoing string
  52. # as we can fit in the buffer
  53. self.buffer.appendleft(last[-(self.maxsize-self.size):])
  54. self.size = self.maxsize
  55. def __str__(self):
  56. return '\n'.join([m for m in self.buffer])
  57. def __iter__(self):
  58. return iter(self.buffer)
  59. class FixedBufferHandler(logging.Handler):
  60. """
  61. Super simple log handler.
  62. """
  63. def __init__(self,buffer_size=50000):
  64. logging.Handler.__init__(self)
  65. self.buf = FixedBuffer(buffer_size)
  66. def emit(self,record):
  67. self.buf.insert(self.format(record))