Skip to content

Commit

Permalink
Use Rotating File Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
deldotdr committed Aug 9, 2011
1 parent 73f7b36 commit 6667f2e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 59 deletions.
56 changes: 0 additions & 56 deletions ion/core/ioninit.py
Expand Up @@ -26,62 +26,6 @@
if sys.version_info > (3,0): if sys.version_info > (3,0):
raise RuntimeError("ioncore is not compatible with Python 3.0 or later.") raise RuntimeError("ioncore is not compatible with Python 3.0 or later.")


# Monkey Patch logging to include WatchedFileHandler
if not hasattr(logging.handlers, 'WatchedFileHandler'):
from stat import ST_DEV, ST_INO
class WatchedFileHandler(logging.FileHandler):
"""
A handler for logging to a file, which watches the file
to see if it has changed while in use. This can happen because of
usage of programs such as newsyslog and logrotate which perform
log file rotation. This handler, intended for use under Unix,
watches the file to see if it has changed since the last emit.
(A file has changed if its device or inode have changed.)
If it has changed, the old file stream is closed, and the file
opened to get a new stream.
This handler is not appropriate for use under Windows, because
under Windows open files cannot be moved or renamed - logging
opens the files with exclusive locks - and so there is no need
for such a handler. Furthermore, ST_INO is not supported under
Windows; stat always returns zero for this value.
This handler is based on a suggestion and patch by Chad J.
Schroeder.
"""
def __init__(self, filename, mode='a', encoding=None, delay=0):
logging.FileHandler.__init__(self, filename, mode, encoding)
if not os.path.exists(self.baseFilename):
self.dev, self.ino = -1, -1
else:
stat = os.stat(self.baseFilename)
self.dev, self.ino = stat[ST_DEV], stat[ST_INO]

def emit(self, record):
"""
Emit a record.
First check if the underlying file has changed, and if it
has, close the old stream and reopen the file to get the
current stream.
"""
if not os.path.exists(self.baseFilename):
stat = None
changed = 1
else:
stat = os.stat(self.baseFilename)
changed = (stat[ST_DEV] != self.dev) or (stat[ST_INO] != self.ino)
if changed and self.stream is not None:
self.stream.flush()
self.stream.close()
self.stream = self._open()
if stat is None:
stat = os.stat(self.baseFilename)
self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
logging.FileHandler.emit(self, record)

setattr(logging.handlers, 'WatchedFileHandler', WatchedFileHandler)

# The following code looking for a ION_ALTERNATE_LOGGING_CONF environment # The following code looking for a ION_ALTERNATE_LOGGING_CONF environment
# variable can go away with the new ion environment directories # variable can go away with the new ion environment directories


Expand Down
8 changes: 5 additions & 3 deletions res/logging/ionlogging.conf
Expand Up @@ -34,20 +34,22 @@ formatter=simpleFormatter
args=(sys.stderr,) args=(sys.stderr,)


[handler_tracefileHandler] [handler_tracefileHandler]
class=handlers.WatchedFileHandler class=FileHandler
level=DEBUG level=DEBUG
formatter=fileFormatter formatter=fileFormatter
args=('logs/ioncontainer.log', 'w') args=('logs/ioncontainer.log', 'w')
filename=logs/ioncontainer.log filename=logs/ioncontainer.log
mode=w mode=w


[handler_logfileHandler] [handler_logfileHandler]
class=handlers.WatchedFileHandler class=handlers.RotatingFileHandler
level=DEBUG level=DEBUG
formatter=fileFormatter formatter=fileFormatter
args=('logs/ionsystem.log', 'a') args=('logs/ionsystem.log', 'a', 10*1024*1024, 3)
filename=logs/ionsystem.log filename=logs/ionsystem.log
mode=a mode=a
maxBytes=10*1024*1024
backupCount=3


[handler_msgfileHandler] [handler_msgfileHandler]
class=FileHandler class=FileHandler
Expand Down

1 comment on commit 6667f2e

@sunetos
Copy link
Contributor

@sunetos sunetos commented on 6667f2e Aug 9, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Reviewed] There were some issues found, Dorian will commit a fix next

Please sign in to comment.