Skip to content

quantpylib.logger.handlers

BufferedFileHandler

Bases: FileHandler

A logging handler that buffers log messages and writes them to a file when the buffer is full or on exit.

This handler extends logging.FileHandler to provide buffered writing. Log messages are stored in a buffer and are written to the file only when the buffer reaches a specified size or the program exits.

Parameters:

Name Type Description Default
filename str

The name of the file to write log messages to.

required
mode str

The mode in which the file is opened. Defaults to 'a' (append).

'a'
encoding str

The encoding to use for the file. Defaults to None.

None
delay bool

If True, file opening is deferred until the first call to emit(). Defaults to False.

False
errors str

How encoding errors are to be handled. Defaults to None.

None
buffer_size int

The number of log messages to buffer before writing to the file. Defaults to 64.

64

__init__(filename, mode='a', encoding=None, delay=False, errors=None, buffer_size=64)

Initializes the BufferedFileHandler with the specified parameters.

Registers an atexit function to flush the buffer when the program exits.

Parameters:

Name Type Description Default
filename str

The name of the file to write log messages to.

required
mode str

The mode in which the file is opened. Defaults to 'a' (append).

'a'
encoding str

The encoding to use for the file. Defaults to None.

None
delay bool

If True, file opening is deferred until the first call to emit(). Defaults to False.

False
errors str

How encoding errors are to be handled. Defaults to None.

None
buffer_size int

The number of log messages to buffer before writing to the file. Defaults to 64.

64

emit(record)

Adds a log record to the buffer and writes to the file if the buffer is full.

Parameters:

Name Type Description Default
record LogRecord

The log record to add to the buffer.

required

flush_buffer()

Writes the contents of the buffer to the file and clears the buffer.

This method is thread-safe.

BufferedRotatingFileHandler

Bases: RotatingFileHandler

A rotating file handler with buffered writing.

This handler extends RotatingFileHandler to buffer log messages and write them to the file only when the buffer reaches a specified size. The file is rotated when it reaches a certain size.

Parameters:

Name Type Description Default
filename str

The name of the file to write log messages to.

required
mode str

The mode in which the file is opened. Defaults to 'a' (append).

'a'
maxBytes int

The maximum size of the file before it is rotated. Defaults to 1GB.

1 * 1024 * 1024 * 1024
backupCount int

The number of backup files to keep. Defaults to 5.

5
encoding str

The encoding to use for the file. Defaults to None.

None
delay bool

If True, file opening is deferred until the first call to emit(). Defaults to False.

False
errors str

How encoding errors are to be handled. Defaults to None.

None
buffer_size int

The number of log messages to buffer before writing to the file. Defaults to 64.

64

__init__(filename, mode='a', maxBytes=1 * 1024 * 1024 * 1024, backupCount=5, encoding=None, delay=False, errors=None, buffer_size=64)

Initializes the BufferedRotatingFileHandler with the specified parameters.

Registers an atexit function to flush the buffer when the program exits.

Parameters:

Name Type Description Default
filename str

The name of the file to write log messages to.

required
mode str

The mode in which the file is opened. Defaults to 'a' (append).

'a'
maxBytes int

The maximum size of the file before it is rotated. Defaults to 1GB.

1 * 1024 * 1024 * 1024
backupCount int

The number of backup files to keep. Defaults to 5.

5
encoding str

The encoding to use for the file. Defaults to None.

None
delay bool

If True, file opening is deferred until the first call to emit(). Defaults to False.

False
errors str

How encoding errors are to be handled. Defaults to None.

None
buffer_size int

The number of log messages to buffer before writing to the file. Defaults to 64.

64

emit(record)

Adds a log record to the buffer and writes to the file if the buffer is full.

Parameters:

Name Type Description Default
record LogRecord

The log record to add to the buffer.

required

flush_buffer()

Writes the contents of the buffer to the file and clears the buffer.

This method is thread-safe. Only the last log record in the buffer state is used to check for rotate condition.

Raises:

Type Description
IOError

If the file cannot be written to.