logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.

This commit is contained in:
Vinay Sajip 2012-02-23 19:37:18 +00:00
parent 62cc44db0c
commit d23845e270
2 changed files with 29 additions and 22 deletions

View file

@ -1,4 +1,4 @@
# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved. # Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
# #
# Permission to use, copy, modify, and distribute this software and its # Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, # documentation for any purpose and without fee is hereby granted,
@ -828,6 +828,7 @@ class StreamHandler(Handler):
""" """
Flushes the stream. Flushes the stream.
""" """
with self.lock:
if self.stream and hasattr(self.stream, "flush"): if self.stream and hasattr(self.stream, "flush"):
self.stream.flush() self.stream.flush()
@ -900,6 +901,7 @@ class FileHandler(StreamHandler):
""" """
Closes the stream. Closes the stream.
""" """
with self.lock:
if self.stream: if self.stream:
self.flush() self.flush()
if hasattr(self.stream, "close"): if hasattr(self.stream, "close"):

View file

@ -1,4 +1,4 @@
# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved. # Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
# #
# Permission to use, copy, modify, and distribute this software and its # Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, # documentation for any purpose and without fee is hereby granted,
@ -563,6 +563,7 @@ class SocketHandler(logging.Handler):
""" """
Closes the socket. Closes the socket.
""" """
with self.lock:
if self.sock: if self.sock:
self.sock.close() self.sock.close()
self.sock = None self.sock = None
@ -767,6 +768,7 @@ class SysLogHandler(logging.Handler):
""" """
Closes the socket. Closes the socket.
""" """
with self.lock:
if self.unixsocket: if self.unixsocket:
self.socket.close() self.socket.close()
logging.Handler.close(self) logging.Handler.close(self)
@ -1096,6 +1098,7 @@ class BufferingHandler(logging.Handler):
This version just zaps the buffer to empty. This version just zaps the buffer to empty.
""" """
with self.lock:
self.buffer = [] self.buffer = []
def close(self): def close(self):
@ -1144,6 +1147,7 @@ class MemoryHandler(BufferingHandler):
records to the target, if there is one. Override if you want records to the target, if there is one. Override if you want
different behaviour. different behaviour.
""" """
with self.lock:
if self.target: if self.target:
for record in self.buffer: for record in self.buffer:
self.target.handle(record) self.target.handle(record)
@ -1154,5 +1158,6 @@ class MemoryHandler(BufferingHandler):
Flush, set the target to None and lose the buffer. Flush, set the target to None and lose the buffer.
""" """
self.flush() self.flush()
with self.lock:
self.target = None self.target = None
BufferingHandler.close(self) BufferingHandler.close(self)