Package: concurrent-log-handler
concurrent-log-handlerThis package provides an additional log handler for Python's standard logging
package (PEP 282). This handler will write log events to a log file which is
rotated when the log file reaches a certain size. Multiple processes can
safely write to the same log file concurrently. Rotated logs can be gzipped
if desired. Both Windows and POSIX systems are supported. An optional threaded
queue logging handler is provided to perform logging in the background.This is a fork of Lowell Alleman's ConcurrentLogHandler 0.9.1 which fixes
a hanging/deadlocking problem.See this.Summary of other changes:New: requires Python 3.6 or higher.If you require support for Python 2.7, use version0.9.22.Renamed package toconcurrent_log_handler(abbreviated CLH in this file.)Provideuse_gzipoption to compress rotated logsSupport for WindowsUses file locking to ensure exclusive write access
Note: file locking is advisory, not a hard lock against external processesMore secure generation of random numbers for temporary filenamesChange the name of the lockfile to have .__ in front of it.Provide a class for time-based rotation:ConcurrentTimedRotatingFileHandlerProvide an optional QueueListener / QueueHandler implementation for
handling log events in a background thread.Allow setting owner and mode permissions of rollover file on UnixDepends onportalockerpackage, which (on Windows only) depends onPyWin32Linksconcurrent-log-handler on Githubconcurrent-log-handler on the Python Package Index (PyPI)Primary use casesThe main use case this is designed to support is when you have a Python application that runs in
multiple processes, potentially on multiple hosts connected with a shared network drive, and you want
to write all log events to a central log file and have those files rotated based on size and/or time,
e.g. daily or hourly.However, this is not the only way to achieve shared logging from multiple processes. You can also
centralize logging by using cloud logging services like Azure Log Monitor, Logstash, etc. Or you can
implement your own remote logging server as shown here:Logging cookbook: networkConcurrent-Log-Handler includes a QueueHandler and QueueListener implementation that can be used to
perform logging in the background asynchronously, so the thread or process making the log statement doesn't have
to wait for its completion. Seethis section. Using that example code, each process still
locks and writes the file separately, so there is no centralized writer. You could also write code to use
QueueHandler and QueueListener to queue up log events within each process to be sent to a central server,
instead of CLH's model where each process locks and writes to the log file.Time-based rotationThe mainConcurrentRotatingFileHandlerclass supports size-based rotation only.
In addition, aConcurrentTimedRotatingFileHandlerclass is provided that supports both time-based
and size-based rotation. By default, it does hourly time-based rotation and no size rotation.
Seethis sectionfor more details.Instructions and UsageInstallationYou can download and install the package withpipusing the following command:pip install concurrent-log-handlerThis will also install the portalocker module, which on Windows in turn depends on pywin32.If installing from source, use the following command:python setup.py installDeveloper setupIf you plan to modify the code, you should follow this procedure:Clone the repositoryCreate a virtual environment (venv) and activate it.Install the package in editable mode with the [dev] option:pip install -e .[dev]Run the tests:toxor runpytestdirectly.Or manually run a single pass of the stress test with specific options:pythontests/stresstest.py--help
pythontests/stresstest.py--gzip--num-processes12--log-calls=5000To build a Python "wheel" for distribution, use the following:pythonsetup.pyclean--allbuildsdistbdist_wheel# Copy the .whl file from under the "dist" folder# or upload with twine:pipinstalltwine
twineuploaddist/concurrent-log-handler-0.9.23.tar.gzdist/concurrent_log_handler-0.9.23-py3-none-any.whlImportant RequirementsConcurrent Log Handler (CLH) is designed to allow multiple processes to write to the same
logfile in a concurrent manner. It is important that each process involved MUST follow
these requirements:You can't serialize a handler instance and reuse it in another process. This means you cannot, for
example, pass a CLH handler instance from parent process to child process using
themultiprocessingpackage in spawn mode (or similar techniques that use serialized objects).
Each child process must initialize its own CLH instance.When using themultiprocessingmodule in "spawn" (non-fork) mode, each child process must create
its OWN instance of the handler (ConcurrentRotatingFileHandler). The child target function
should call code that initializes a new CLH instance.This requirement does not apply to threads within a given process. Different threads within a
process can use the same CLH instance. Thread locking is handled automatically.This also does not apply tofork()based child processes such as gunicorn --preload.
Child processes of a fork() call should be able to inherit the CLH object instance.This limitation exists because the CLH object can't be serialized, passed over a network or
pipe, and reconstituted at the other end.It is important that every process or thread writing to a given logfile must all use the same
settings, especially related to file rotation. Also do not attempt to mix different handler
classes writing to the same file, e.g. do not also use aRotatingFileHandleron the same file.Special attention may need to be paid when the log file being written to resides on a network
shared drive or a cloud synced folder (Dropbox, Google Drive, etc.). Whether the multiprocess
advisory lock technique (via portalocker) works in these folders may depend on the details of
your configuration.Note that alock_file_directorysetting (kwarg) now exists (as of v0.9.21) which lets you
place the lockfile at a different location from the main logfile. This might solve problems
related to trying to lock files in network shares or cloud folders (Dropbox, Google Drive, etc.)
However, if multiple hosts are writing to the same shared logfile, they must also have access
to the same lock file.Alternatively, you may be able to set your cloud sync software to ignore all.lockfiles.A separate handler instance is needed for each individual log file. For instance, if your app
writes to two different log files you will need to set up two CLH instances per process.Simple ExampleHere is a simple direct usage example:fromloggingimportgetLogger,INFOfromconcurrent_log_handlerimportConcurrentRotatingFileHandlerimportoslog=getLogger(__name__)# Use an absolute path to prevent file rotation trouble.logfile=os.path.abspath("mylogfile.log")# Rotate log after reaching 512K, keep 5 old copies.rotateHandler=ConcurrentRotatingFileHandler(logfile,"a",512*1024,5)log.addHandler(rotateHandler)log.setLevel(INFO)log.info("Here is a very exciting log message, just for you")See also the filesrc/example.pyfor a configuration and usage example.
This shows both the standard non-threaded non-async usage, and the use of theasynciobackground logging feature. Under that option, when your program makes a logging statement,
it is added to a background queue and may not be written immediately and synchronously. This
queue can span multiple processes usingmultiprocessingorconcurrent.futures, and spanning
multiple hosts works due to the use of file locking on the log file. Note that with this async
logging feature, currently there is no way for the caller to know when the logging statement
completed (no "Promise" or "Future" object is returned).QueueHandlerConfigurationTo use this module from a logging config file, use a handler entry like this:[handler_hand01]class=handlers.ConcurrentRotatingFileHandlerlevel=NOTSETformatter=form01args=("rotating.log", "a")kwargs={'backupCount': 5, 'maxBytes': 1048576, 'use_gzip': True}That sets the files to be rotated at about 10 MB, and to keep the last 5 rotations.
It also turns on gzip compression for rotated files.Please note that Python 3.7 and higher accepts keyword arguments (kwargs) in a logging
config file, but earlier versions of Python only accept positional args.Note: you must have animport concurrent_log_handlerbefore you call fileConfig(). For
more information see Python docs onlog file formatsLimitationsThe size-based rotation limit (maxBytes) is not strict. The files may become slightly
larger thanmaxBytes. How much larger depends on the size of the log message being
written when the rollover occurs.By contrast, the baseRotatingLogHandlerclass tries to ensure that the log file is
always kept undermaxBytestaking into account the size of the current log message being
written. This limitation may be changed in the future.Recommended SettingsFor best performance, avoid setting thebackupCount(number of rollover files to keep) too
high. What counts as "too high" is situational, but a good rule of thumb might be to keep
around a maximum of 20 rollover files. If necessary, increase themaxBytesso that each
file can hold more. Too many rollover files can slow down the rollover process due to the
mass file renames, and the rollover occurs while the file lock is held for the main logfile.How big to allow each file to grow (maxBytes) is up to your needs, but generally a value of
10 MB (1048576) to 100 MB (1048576) is reasonable.Gzip compression is turned off by default. If enabled it will reduce the storage needed for rotated
files, at the cost of some minimal CPU overhead. Use of the background logging queue shown below
can help offload the cost of logging to another thread.Sometimes you may need to place the lock file at a different location from the main log
file. Alock_file_directorysetting (kwarg) now exists (as of v0.9.21) which lets you
place the lockfile at a different location. This can often solve problems related to trying
to lock files in cloud folders (Dropbox, Google Drive, OneDrive, etc.) However, in
order for this to work, each process writing to the log must have access to the same
lock file location, even if they are running on different hosts.You can set thenamerattribute of the handler to customize the naming of the rotated files,
in line with theBaseRotatingHandlerclass. See the Python docs formore details.Line EndingsBy default, the logfile will have line endings appropriate to the platform. On Windows
the line endings will be CRLF ('\r\n') and on Unix/Mac they will be LF ('\n').It is possible to force another line ending format by using the newline and terminator
arguments.The following would force Windows-style CRLF line endings on Unix:kwargs={'newline': '', 'terminator': '\r\n'}The following would force Unix-style LF line endings on Windows:kwargs={'newline': '', 'terminator': '\n'}Time-based rotation settingsAn alternative classConcurrentTimedRotatingFileHandleris also provided which supports
time-based rotation, defaulting to hourly. Like the main class, it uses advisory file
locking to both ensure that only one process/thread is writing to the log file at a time,
and to coordinate the rollover time between processes.By default, it hasmaxBytesset to 0, which means that it will not rotate based on file size,
but it is possible to setmaxBytesto a value to limit the size of each file in addition
to the time-based rotation. When files are rotated based on size, they may have an additional
numeric suffix like.1added to the filename. Note that like with the main CLH class,
the file size limits arenotstrictly adhered to.All the same settings are available for this class as for the main class, includingmaxBytes,use_gzip,lock_file_directory,newline, andterminator. However,
the ordering of the arguments is different, so it's recommended to use keyword arguments
when using or configuring this class. The arguments shared withTimedRotatingFileHandlerare in the same order as the base class, and the extra CLH arguments come after that,
although not in the exact same order due to some overlap.For configuration, see theconfiguration sectionabove, but substitute inclass=handlers.ConcurrentTimedRotatingFileHandlerand other appropriate settings
likewhenandinterval. See the Python docs forTimedRotatingFileHandlerformore details.Background logging queueTo use the background logging queue, you must call this code at some point in your
app after it sets up logging configuration. Please read the doc string in the
fileconcurrent_log_handler/queue.pyfor more details. This requires Python 3.
See alsosrc/example.py.fromconcurrent_log_handler.queueimportsetup_logging_queues# convert all configured loggers to use a background threadsetup_logging_queues()This module is designed to function well in a multi-threaded or multi-processes
concurrent environment. However, all writers to a given log file should be using
the same class and thesame settingsat the same time, otherwise unexpected
behavior may result during file rotation.This may mean that if you change the logging settings at any point you may need to
restart your app service so that all processes are using the same settings at the same time.Other Usage DetailsTheConcurrentRotatingFileHandlerclass is a drop-in replacement for
Python's standard log handlerRotatingFileHandler. This module uses file
locking so that multiple processes can concurrently log to a single file without
dropping or clobbering log events. This module provides a file rotation scheme
like withRotatingFileHandler. Extra care is taken to ensure that logs
can be safely rotated before the rotation process is started. (This module works
around the file rename issue withRotatingFileHandleron Windows, where a
rotation failure means that all subsequent log events are dropped).This module attempts to preserve log records at all cost. This means that log
files will grow larger than the specified maximum (rotation) size. So if disk
space is tight, you may want to stick withRotatingFileHandler, which will
strictly adhere to the maximum file size.Important:If you have multiple instances of a script (or multiple scripts) all running at
the same time and writing to the same log file, thenallof the scripts should
be usingConcurrentRotatingFileHandler. You should not attempt to mix
and matchRotatingFileHandlerandConcurrentRotatingFileHandler.
The file locking is advisory only - it is respected by other Concurrent Log Handler
instances, but does not protect against outside processes (or different Python logging
file handlers) from writing to a log file in use.ChangelogSeeCHANGELOG.mdContributorsThe original version was written by Lowell Alleman.Other contributors are listed inCONTRIBUTORS.md.LicenseSee theLICENSE file