|
Rotary Logger
1.0.2
The middleware rotary logger
|

Public Member Functions | |
| None | __init__ (self, Optional[Union[str, Path, CONST.FileInfo]] file_path, Optional[bool] override=None, Optional[bool] merged=None, Optional[str] encoding=None, Optional[CONST.Prefix] prefix=None, *, Optional[int] max_size_mb=None, Optional[int] flush_size_kb=None, Optional[CONST.StdMode] folder_prefix=None, bool log_to_file=True, Optional[bool] merge_stdin=None) |
| None | __del__ (self) |
| None | set_log_to_file (self, bool log_to_file=True, *, bool lock=True) |
| None | set_max_size (self, int max_size_mb, *, bool lock=True) |
| None | set_folder_prefix (self, Optional[CONST.StdMode] folder_prefix, *, bool lock=True) |
| None | set_flush_size (self, int flush_size, *, bool lock=True) |
| None | set_merged (self, bool merged, *, bool lock=True) |
| None | set_merge_stdin (self, bool merge_stdin, *, bool lock=True) |
| None | set_encoding (self, str encoding, *, bool lock=True) |
| None | set_prefix (self, Optional[CONST.Prefix] prefix, *, bool lock=True) |
| None | set_override (self, bool override=False, *, bool lock=True) |
| None | set_filepath (self, Optional[Union[str, Path, CONST.FileInfo]] file_path, *, bool lock=True) |
| bool | get_log_to_file (self, *, bool lock=True) |
| str | get_mode (self, *, bool lock=True) |
| bool | get_merged (self, *, bool lock=True) |
| bool | get_merge_stdin (self, *, bool lock=True) |
| str | get_encoding (self, *, bool lock=True) |
| Optional[CONST.Prefix] | get_prefix (self, *, bool lock=True) |
| bool | get_override (self, *, bool lock=True) |
| Optional[CONST.FileInfo] | get_filepath (self, *, bool lock=True) |
| int | get_flush_size (self, *, bool lock=True) |
| int | get_max_size (self, *, bool lock=True) |
| Optional[CONST.StdMode] | get_folder_prefix (self, *, bool lock=True) |
| None | update (self, Optional['FileInstance'] file_data, *, bool lock=True) |
| "FileInstance" | copy (self, *, bool lock=True) |
| None | write (self, str message) |
| flush (self) | |
Data Fields | |
| Rogger | rogger = RI |
| Optional[CONST.FileInfo] | file = None |
| bool | override = False |
| bool | merged = True |
| bool | merge_stdin = False |
| str | encoding = CONST.DEFAULT_ENCODING |
| Optional[CONST.Prefix] | prefix = None |
| int | max_size = CONST.DEFAULT_LOG_MAX_FILE_SIZE |
| int | flush_size = CONST.BUFFER_FLUSH_SIZE |
| Optional[CONST.StdMode] | folder_prefix = None |
| bool | _file_lock = False) |
| CONST.FileInfo | file = CONST.RAW_STDOUT |
| str | _file_lock = self.get_mode(lock=False).lower() |
Protected Member Functions | |
| None | _set_prefix (self, Optional[CONST.Prefix] prefix) |
| None | _set_folder_prefix (self, Optional[CONST.StdMode] folder_prefix) |
| None | _set_mode (self, str mode, *, bool lock=True) |
| None | _set_max_size (self, int max_size_mb) |
| None | _set_flush_size (self, int flush_size_kb) |
| None | _set_filepath_child (self, Union[str, Path, CONST.FileInfo] file_path) |
| None | _update (self, Optional['FileInstance'] file_data) |
| "FileInstance" | _copy (self) |
| datetime | _get_current_date (self) |
| str | _get_filename (self) |
| bool | _should_flush (self) |
| None | _refresh_written_bytes (self) |
| bool | _should_rotate (self) |
| None | _rotate_file (self) |
| Path | _create_log_path (self, Optional[Path] base_override=None) |
| bool | _looks_like_directory (self, Path dir_path) |
| CONST.FileInfo | _open_file (self, Path file_path) |
| bool | _close_file_inner (self) |
| bool | _close_file (self, *, Optional[bool] lock=True) |
| None | _flush_buffer (self) |
Protected Attributes | |
| RLock | _file_lock = RLock() |
| str | _mode = "a" |
| bool | _log_to_file = log_to_file |
| list | _buffer = [] |
Manage buffered writes, file descriptors, and log rotation. Public methods are thread-safe and documented below. Writes are appended to an in-memory buffer and flushed to disk when the configured flush size is reached. Rotation is performed when the underlying file grows beyond `max_size`.
Definition at line 51 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.__init__ | ( | self, | |
| Optional[Union[str, Path, CONST.FileInfo]] | file_path, | ||
| Optional[bool] | override = None, | ||
| Optional[bool] | merged = None, | ||
| Optional[str] | encoding = None, | ||
| Optional[CONST.Prefix] | prefix = None, | ||
| * | , | ||
| Optional[int] | max_size_mb = None, | ||
| Optional[int] | flush_size_kb = None, | ||
| Optional[CONST.StdMode] | folder_prefix = None, | ||
| bool | log_to_file = True, | ||
| Optional[bool] | merge_stdin = None ) |
Create a FileInstance wrapper.
Arguments:
file_path (Optional[Union[str, Path, CONST.FileInfo]]): Initial file path, Path or FileInfo, or None to defer opening.
Keyword Arguments:
override (Optional[bool]): When True open files for write ('w') instead of append ('a'). Default: None
merged (Optional[bool]): Whether multiple streams should share the same file. Default: None
encoding (Optional[str]): Text encoding to use for file I/O. Default: None
prefix (Optional[CONST.Prefix]): Optional Prefix configuration to use when teeing. Default: None
max_size_mb (Optional[int]): Maximum logfile size in MB before rotation. Default: None
flush_size_kb (Optional[int]): Buffer flush threshold in KB. Default: None
folder_prefix (Optional[CONST.StdMode]): StdMode used to segregate per-stream subfolders. Default: None
log_to_file (bool): Whether file logging is enabled. Default: True
merge_stdin (Optional[bool]): Whether stdin is merged into the shared log file. Default: None
Definition at line 60 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.__del__ | ( | self | ) |
Best-effort cleanup on object deletion. Attempt to close the current file descriptor if it exists and is open. This method must never raise during interpreter shutdown (where `__del__` may be called), so IO-related errors are swallowed. After attempting to close the descriptor the internal `file` reference is cleared.
Definition at line 133 of file file_instance.py.
|
protected |
Close the current file descriptor, optionally acquiring a lock. When `lock` is True the instance lock `self._file_lock` is acquired before closing the file. Returns True on completion.
Definition at line 1090 of file file_instance.py.
|
protected |
Close the underlying file descriptor without acquiring locks. This inner helper is used by `_close_file()`; it performs the actual descriptor close and clears the reference. It is safe to call when already closed. Errors during close are suppressed because this is a best-effort operation.
Definition at line 1062 of file file_instance.py.
|
protected |
Return a shallow copy of this FileInstance configuration. The returned `FileInstance` will share the same `FileInfo` reference but will otherwise have the same configuration values (mode, encoding, prefix, sizes). The copy is useful for creating per-stream views of shared configuration.
Definition at line 821 of file file_instance.py.
|
protected |
Build the timestamped log file path and create the parent directories.
The path is organised as `<root>/logs/<year>/<month>/<day>/[<stream>/]<timestamp>.log`.
If `base_override` is provided it is used as the root; otherwise the instance's
current file path or the package directory is used as the fallback.
Keyword Arguments:
base_override (Optional[Path]): Override root directory for the log path. Default: None
Returns:
The resolved Path for the new log file.
Definition at line 908 of file file_instance.py.
|
protected |
Internal: detach pending buffer and write to disk. Implements the swap-buffer pattern: capture and clear the in-memory buffer while holding the lock, perform I/O outside the lock, then update counters and rotate under lock.
Definition at line 1119 of file file_instance.py.
|
protected |
Return the current UTC datetime used for naming files. Centralising the time provider makes it easier to test filename generation and ensures all timestamps use UTC.
Definition at line 842 of file file_instance.py.
|
protected |
Construct a timestamped log filename. The filename format is driven by `CONST.FILE_LOG_DATE_FORMAT` and uses the current UTC time returned by `_get_current_date()`.
Definition at line 850 of file file_instance.py.
|
protected |
Heuristic check to decide whether a path refers to a directory.
Returns True when the path has no file extension, already exists as a
directory, or ends with a path separator. Used to determine whether a
caller-supplied path should be treated as a folder (triggering
automatic log-file naming) or as an explicit file path.
Arguments:
dir_path (Path): Path to evaluate.
Returns:
True if the path appears to represent a directory, False otherwise.
Definition at line 969 of file file_instance.py.
|
protected |
Open or create the log file at `file_path` and return a populated `FileInfo`.
If `file_path` looks like a directory (as determined by `_looks_like_directory`)
a timestamped filename is generated via `_create_log_path`. The parent directory
is created if it does not exist. The file descriptor is opened outside the
instance lock; the resulting `FileInfo` is populated atomically under the lock.
Arguments:
file_path (Path): Destination path for the log file, or a directory.
Returns:
A `CONST.FileInfo` with `path`, `descriptor`, and `written_bytes` populated.
Definition at line 1001 of file file_instance.py.
|
protected |
Add the sizes of buffered lines to `file.written_bytes`. This method is called after a successful write to update the persisted byte counter. It encodes lines using the configured encoding and falls back to 'utf-8' on lookup errors. The in- memory buffer is cleared after accounting.
Definition at line 869 of file file_instance.py.
|
protected |
Rotate the current file if the bytes threshold is exceeded. When rotation is needed the current descriptor is closed and a fresh `FileInfo` is opened at a newly-created path returned by `_create_log_path()`.
Definition at line 896 of file file_instance.py.
|
protected |
Internal routine to set the instance's file reference. This method closes any previously-open file, clears internal state, and opens the provided `file_path`. The `file_path` may be a string/Path (in which case a new `FileInfo` is created) or an existing `CONST.FileInfo` instance which may be re-opened if necessary.
Definition at line 779 of file file_instance.py.
|
protected |
Configure the flush threshold for buffered writes. `flush_size_kb` is interpreted as kilobytes when reasonable; the function coerces input to int, normalises negative values and ensures the internal `self.flush_size` is stored in bytes.
Definition at line 755 of file file_instance.py.
|
protected |
Configure the per-stream folder prefix. If `folder_prefix` is a valid `CONST.StdMode` value present in `CONST.CORRECT_FOLDER`, it is stored; otherwise the stored `folder_prefix` is cleared (set to None).
Definition at line 699 of file file_instance.py.
|
protected |
Configure the maximum file size used for rotation. `max_size_mb` is interpreted as megabytes when reasonable; the function attempts to coerce the input to an integer. Negative or too-small values are corrected with warnings. The resulting internal `self.max_size` is stored in bytes.
Definition at line 730 of file file_instance.py.
|
protected |
Set the file mode to 'w' (overwrite) or 'a' (append). The `lock` parameter indicates whether this function should acquire `self._file_lock` before updating the internal mode. Invalid input is ignored.
Definition at line 715 of file file_instance.py.
|
protected |
Set the internal `Prefix` object from an external one. This internal setter copies boolean flag values from `prefix` into a fresh `CONST.Prefix()` instance. The caller is responsible for holding any required locks; this routine does not perform locking itself.
Definition at line 670 of file file_instance.py.
|
protected |
Check whether the in-memory buffer has reached the flush threshold.
Returns:
True if the total encoded size of buffered lines meets or exceeds `flush_size`, False otherwise.
Definition at line 858 of file file_instance.py.
|
protected |
Check whether the current log file has exceeded the maximum size threshold.
Returns:
True if the file's `written_bytes` exceeds `max_size` or no file is open, False otherwise.
Definition at line 886 of file file_instance.py.
|
protected |
Copy configuration values from another FileInstance. This helper updates the receiver to match the provided `file_data`. The method is intended to be called while holding the caller's lock; it delegates to public setters with the `lock=False` flag to avoid deadlocks.
Definition at line 800 of file file_instance.py.
| "FileInstance" rotary_logger.file_instance.FileInstance.copy | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return a shallow copy of this instance's configuration. The returned `FileInstance` is a new object populated from the current instance. By default the instance lock is acquired to provide a consistent snapshot.
Definition at line 616 of file file_instance.py.
| rotary_logger.file_instance.FileInstance.flush | ( | self | ) |
Flush any buffered log lines to disk immediately. This is a blocking call that performs disk I/O; callers should avoid calling it too frequently. Errors raised by the underlying I/O are propagated as OSError or ValueError when appropriate.
Definition at line 654 of file file_instance.py.
| str rotary_logger.file_instance.FileInstance.get_encoding | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return the configured text encoding for file writes. When `lock` is True the instance lock is held while the value is read.
Definition at line 506 of file file_instance.py.
| Optional[CONST.FileInfo] rotary_logger.file_instance.FileInstance.get_filepath | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return the internal `FileInfo` reference (may be None). Note: the returned object may be shared; callers that need to mutate it should take care to hold the instance lock or use `copy()` to obtain an independent view.
Definition at line 570 of file file_instance.py.
| int rotary_logger.file_instance.FileInstance.get_flush_size | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return the configured buffer flush threshold in bytes.
Definition at line 582 of file file_instance.py.
| Optional[CONST.StdMode] rotary_logger.file_instance.FileInstance.get_folder_prefix | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return the configured folder prefix (StdMode) or None.
Definition at line 596 of file file_instance.py.
| bool rotary_logger.file_instance.FileInstance.get_log_to_file | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return True when file logging is enabled.
Keyword Arguments:
lock (bool): When True the instance lock is acquired before reading the flag. Default: True
Returns:
True if writes to the log file are enabled, False otherwise.
Definition at line 456 of file file_instance.py.
| int rotary_logger.file_instance.FileInstance.get_max_size | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return the configured maximum file size in bytes.
Definition at line 589 of file file_instance.py.
| bool rotary_logger.file_instance.FileInstance.get_merge_stdin | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return the merge_stdin flag (True when stdin is merged into the shared log file).
Keyword Arguments:
lock (bool): When True the instance lock is held while reading the value. Default: True
Returns:
True if stdin is merged into the shared log file, False otherwise.
Definition at line 492 of file file_instance.py.
| bool rotary_logger.file_instance.FileInstance.get_merged | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return the merged flag (True when streams share a file). The optional `lock` parameter controls whether the instance lock is held while reading the value.
Definition at line 481 of file file_instance.py.
| str rotary_logger.file_instance.FileInstance.get_mode | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return the current file open mode ('w' or 'a').
When `lock` is True the instance lock is acquired prior to
reading the value.
Definition at line 470 of file file_instance.py.
| bool rotary_logger.file_instance.FileInstance.get_override | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return True when override mode ('w') is active.
This convenience maps internal mode strings to a boolean.
When `lock` is True the instance lock is held for the check.
Definition at line 540 of file file_instance.py.
| Optional[CONST.Prefix] rotary_logger.file_instance.FileInstance.get_prefix | ( | self, | |
| * | , | ||
| bool | lock = True ) |
Return a safe copy of the current `Prefix` configuration. The returned `CONST.Prefix` is a fresh object to avoid exposing internal references. If no prefix is configured None is returned. When `lock` is True the instance lock is held while copying the object.
Definition at line 517 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_encoding | ( | self, | |
| str | encoding, | ||
| * | , | ||
| bool | lock = True ) |
Set the text encoding used for file I/O.
Arguments:
encoding (str): A codec name such as 'utf-8'.
Keyword Arguments:
lock (bool): When True the change is performed while holding the instance lock. Default: True
Definition at line 316 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_filepath | ( | self, | |
| Optional[Union[str, Path, CONST.FileInfo]] | file_path, | ||
| * | , | ||
| bool | lock = True ) |
Set or clear the active file/file path for this instance.
Arguments:
file_path (Optional[Union[str, Path, CONST.FileInfo]]): A path-like object, a `CONST.FileInfo` instance describing an already-open file, or None to clear the current file.
Keyword Arguments:
lock (bool): When True the instance lock is held while the change is applied. Default: True
Definition at line 404 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_flush_size | ( | self, | |
| int | flush_size, | ||
| * | , | ||
| bool | lock = True ) |
Public wrapper to configure the buffer flush threshold. `flush_size` is provided as a KB-like value and normalised by `_set_flush_size`. If `lock` is True the operation is performed while holding the instance lock.
Definition at line 228 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_folder_prefix | ( | self, | |
| Optional[CONST.StdMode] | folder_prefix, | ||
| * | , | ||
| bool | lock = True ) |
Public setter for `folder_prefix`. When `lock` is True the instance lock is held while the value is updated. The internal method `_set_folder_prefix` performs the validation and assignment.
Definition at line 215 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_log_to_file | ( | self, | |
| bool | log_to_file = True, | ||
| * | , | ||
| bool | lock = True ) |
Enable or disable file logging.
When `log_to_file` is False no data will be written to disk even
if a file descriptor is open.
Arguments:
log_to_file (bool): Whether writes to the log file are enabled. Default: True
Keyword Arguments:
lock (bool): When True the instance lock is acquired while updating the flag. Default: True
Definition at line 151 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_max_size | ( | self, | |
| int | max_size_mb, | ||
| * | , | ||
| bool | lock = True ) |
Public wrapper to set the maximum logfile size.
Delegates to `_set_max_size` which normalises the value to bytes
and applies range checks.
Arguments:
max_size_mb (int): Maximum size in megabytes. The value is normalised by `_set_max_size` and stored in `self.max_size` as bytes.
Keyword Arguments:
lock (bool): When True the instance lock is acquired while updating the configuration. Default: True
Definition at line 183 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_merge_stdin | ( | self, | |
| bool | merge_stdin, | ||
| * | , | ||
| bool | lock = True ) |
Enable or disable stdin merging into the shared log file.
When `merge_stdin` is True stdin data is written to the same file
as stdout/stderr; when False stdin is kept separate or not logged.
The `lock` parameter controls whether the instance lock is acquired.
Arguments:
merge_stdin (bool): Whether stdin should be merged into the shared log file.
Keyword Arguments:
lock (bool): When True the instance lock is acquired while updating the flag. Default: True
Definition at line 283 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_merged | ( | self, | |
| bool | merged, | ||
| * | , | ||
| bool | lock = True ) |
Enable or disable stream merging. When `merged` is True multiple streams may share a single log file; when False separate per-stream files are used. The `lock` parameter controls whether the instance lock is acquired.
Definition at line 255 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_override | ( | self, | |
| bool | override = False, | ||
| * | , | ||
| bool | lock = True ) |
Public setter for the file open mode. When `override` is True files will be opened with mode 'w' (overwrite); otherwise 'a' (append) is used. The lock behaviour is controlled by `lock`.
Definition at line 373 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.set_prefix | ( | self, | |
| Optional[CONST.Prefix] | prefix, | ||
| * | , | ||
| bool | lock = True ) |
Public setter for `Prefix` configuration. Copies the provided `prefix` into an internal `CONST.Prefix` object. Use `get_prefix()` to obtain a safe copy of the current configuration.
Definition at line 345 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.update | ( | self, | |
| Optional['FileInstance'] | file_data, | ||
| * | , | ||
| bool | lock = True ) |
Public method to copy configuration from another instance. This method acquires the lock by default and delegates to the `_update` implementation which performs the actual field assignments.
Definition at line 603 of file file_instance.py.
| None rotary_logger.file_instance.FileInstance.write | ( | self, | |
| str | message ) |
Append `message` to the internal buffer (thread-safe). The message is encoded and counted toward `flush_size`. When the buffer reaches the configured threshold a background flush is triggered (performed synchronously inside `_flush_buffer()` but with I/O outside the main lock to minimize blocking).
Definition at line 628 of file file_instance.py.
|
protected |
Definition at line 106 of file file_instance.py.
|
protected |
Definition at line 93 of file file_instance.py.
| bool rotary_logger.file_instance.FileInstance._file_lock = False) |
Definition at line 385 of file file_instance.py.
| str rotary_logger.file_instance.FileInstance._file_lock = self.get_mode(lock=False).lower() |
Definition at line 551 of file file_instance.py.
|
protected |
Definition at line 95 of file file_instance.py.
|
protected |
Definition at line 94 of file file_instance.py.
| str rotary_logger.file_instance.FileInstance.encoding = CONST.DEFAULT_ENCODING |
Definition at line 100 of file file_instance.py.
| CONST FileInfo rotary_logger.file_instance.FileInstance.file = None |
Definition at line 96 of file file_instance.py.
| CONST.FileInfo rotary_logger.file_instance.FileInstance.file = CONST.RAW_STDOUT |
Definition at line 417 of file file_instance.py.
| int rotary_logger.file_instance.FileInstance.flush_size = CONST.BUFFER_FLUSH_SIZE |
Definition at line 103 of file file_instance.py.
| Optional[CONST.StdMode] rotary_logger.file_instance.FileInstance.folder_prefix = None |
Definition at line 104 of file file_instance.py.
| int rotary_logger.file_instance.FileInstance.max_size = CONST.DEFAULT_LOG_MAX_FILE_SIZE |
Definition at line 102 of file file_instance.py.
| rotary_logger.file_instance.FileInstance.merge_stdin = False |
Definition at line 99 of file file_instance.py.
| rotary_logger.file_instance.FileInstance.merged = True |
Definition at line 98 of file file_instance.py.
| bool rotary_logger.file_instance.FileInstance.override = False |
Definition at line 97 of file file_instance.py.
| Optional[CONST.Prefix] rotary_logger.file_instance.FileInstance.prefix = None |
Definition at line 101 of file file_instance.py.
| Rogger rotary_logger.file_instance.FileInstance.rogger = RI |
Definition at line 92 of file file_instance.py.