MCPcopy Create free account
hub / github.com/StackStorm/st2 / reopen_log_files

Function reopen_log_files

st2common/st2common/logging/misc.py:48–80  ·  view source on GitHub ↗

This method iterates through all of the providers handlers looking for the FileHandler types. A lock is acquired, the underlying stream closed, reopened, then the lock is released. This method should be called when logs are to be rotated by an external process. The simplest way t

(handlers)

Source from the content-addressed store, hash-verified

46
47
48def reopen_log_files(handlers):
49 """
50 This method iterates through all of the providers handlers looking for the FileHandler types.
51
52 A lock is acquired, the underlying stream closed, reopened, then the lock is released.
53
54
55 This method should be called when logs are to be rotated by an external process. The simplest
56 way to do this is via a signal handler.
57 """
58 for handler in handlers:
59 if not isinstance(handler, logging.FileHandler):
60 continue
61
62 LOG.info(
63 'Re-opening log file "%s" with mode "%s"\n'
64 % (handler.baseFilename, handler.mode)
65 )
66
67 try:
68 handler.acquire()
69 handler.stream.close()
70 handler.stream = open(handler.baseFilename, handler.mode)
71 finally:
72 try:
73 handler.release()
74 except RuntimeError as e:
75 if "cannot release" in six.text_type(e):
76 # Release failed which most likely indicates that acquire failed
77 # and lock was never acquired
78 LOG.warning("Failed to release lock", exc_info=True)
79 else:
80 raise e
81
82
83def set_log_level_for_all_handlers(logger, level=logging.DEBUG):

Callers 1

handle_sigusr1Function · 0.90

Calls 3

acquireMethod · 0.80
releaseMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected