stouputils.ctx.log_to_file module#

class LogToFile(
path: str,
mode: str = 'w',
encoding: str = 'utf-8',
tee_stdout: bool = True,
tee_stderr: bool = True,
strip_colors: bool = False,
ignore_lineup: bool = True,
restore_on_exit: bool = False,
)[source]#

Bases: AbstractBothContextManager[LogToFile]

Context manager to log to a file.

This context manager allows you to temporarily log output to a file while still printing normally. The file will receive log messages without ANSI color codes.

Parameters:
  • path (str) – Path to the log file

  • mode (str) – Mode to open the file in (default: “w”)

  • encoding (str) – Encoding to use for the file (default: “utf-8”)

  • tee_stdout (bool) – Whether to redirect stdout to the file (default: True)

  • tee_stderr (bool) – Whether to redirect stderr to the file (default: True)

  • ignore_lineup (bool) – Whether to ignore lines containing LINE_UP escape sequence in files (default: False)

  • restore_on_exit (bool) – Whether to restore original stdout/stderr on exit (default: False) This ctx uses TeeMultiOutput which handles closed files gracefully, so restoring is not mandatory.

Examples

> import stouputils as stp
> with stp.LogToFile("output.log"):
>     stp.info("This will be logged to output.log and printed normally")
>     print("This will also be logged")

> with stp.LogToFile("output.log") as log_ctx:
>     stp.warning("This will be logged to output.log and printed normally")
>     log_ctx.change_file("new_file.log")
>     print("This will be logged to new_file.log")
path: str#

Attribute remembering path to the log file

mode: str#

Attribute remembering mode to open the file in

encoding: str#

Attribute remembering encoding to use for the file

tee_stdout: bool#

Whether to redirect stdout to the file

tee_stderr: bool#

Whether to redirect stderr to the file

strip_colors: bool#

Whether to strip ANSI color codes from output sent to non-stdout/stderr files

ignore_lineup: bool#

Whether to ignore lines containing LINE_UP escape sequence in files

restore_on_exit: bool#

Whether to restore original stdout/stderr on exit. This ctx uses TeeMultiOutput which handles closed files gracefully, so restoring is not mandatory.

_abc_impl = <_abc._abc_data object>#
file: IO[Any]#

Attribute remembering opened file

original_stdout: TextIO#

Original stdout before redirection

original_stderr: TextIO#

Original stderr before redirection

change_file(new_path: str) None[source]#

Change the log file to a new path.

Parameters:

new_path (str) – New path to the log file

static common(
logs_folder: str,
filepath: str,
func: CallableAny,
init_kwargs: dict[str, Any] | None = None,
*args: Any,
**kwargs: Any,
) Any[source]#

Common code used at the beginning of a program to launch main function

Parameters:
  • logs_folder (str) – Folder to store logs in

  • filepath (str) – Path to the main function

  • func (CallableAny) – Main function to launch

  • init_kwargs (dict[str, Any] | None) – Keyword arguments to pass to LogToFile constructor

  • *args (tuple[Any, ...]) – Arguments to pass to the main function

  • **kwargs (dict[str, Any]) – Keyword arguments to pass to the main function

Returns:

Return value of the main function

Return type:

Any

Examples

>>> if __name__ == "__main__":
...     LogToFile.common(f"{ROOT}/logs", __file__, main, init_kwargs={"strip_colors": True})