Logging¶
logging
¶
Logging utilities for consistent script output.
This module provides functionality for creating consistent outputs from CLI tools provided by this package.
ClickHandler(level=0, file=None, *, nl=True, err=False, color=None, punctuate=True)
¶
Bases: Handler
Custom logging handler specifically for click based CLI tools.
Initialize an instance of the click handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int | str
|
The logging level to use for this handler. |
0
|
file
|
IO[Any] | None
|
The file to write to. Defaults to stdout. |
None
|
nl
|
bool
|
Print a newline after the message. Enabled by default. |
True
|
err
|
bool
|
Write to stderr instead of stdout. |
False
|
color
|
bool | None
|
Force showing or hiding colors and other styles. By default click will remove color if the output does not look like an interactive terminal. |
None
|
punctuate
|
bool
|
A boolean indicating if punctuation should be added to the end of a log message provided if missing. |
True
|
Notes
For more details on the file, nl, err, and color args please refer
to click.echo.
Source code in src/flepimop2/logging.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
emit(record)
¶
Emit a given log record via click.echo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to output. |
required |
See Also
Source code in src/flepimop2/logging.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
LoggingLevel
¶
Bases: IntEnum
An enumeration of the logging levels used in this package.
Attributes:
| Name | Type | Description |
|---|---|---|
DEBUG |
Detailed information, typically of interest only when diagnosing problems. |
|
INFO |
Confirmation that things are working as expected. |
|
WARNING |
An indication that something unexpected happened, or indicative of some problem in the near future (e.g., 'ode unstable'). The software is still working as expected. |
|
ERROR |
Due to a more serious problem, the software has not been able to perform some function. |
|
CRITICAL |
A serious error, indicating that the program itself may be unable to continue running. |
Examples:
>>> from flepimop2.logging import LoggingLevel
>>> LoggingLevel.DEBUG
<LoggingLevel.DEBUG: 10>
>>> LoggingLevel.from_verbosity(2)
<LoggingLevel.INFO: 20>
>>> LoggingLevel.from_verbosity(LoggingLevel.ERROR)
<LoggingLevel.ERROR: 40>
from_verbosity(verbosity)
classmethod
¶
Convert a verbosity level to a LoggingLevel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbosity
|
int | LoggingLevel
|
The verbosity level to convert. |
required |
Returns:
| Type | Description |
|---|---|
LoggingLevel
|
The corresponding |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/flepimop2/logging.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
get_script_logger(name, verbosity, handler=None, log_format=DEFAULT_LOG_FORMAT)
¶
Create a logger for use in scripts/CLI tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name to display in the log message, useful for locating the source
of logging messages. Almost always |
required |
verbosity
|
int
|
A non-negative integer for the verbosity level. |
required |
handler
|
Handler | None
|
An optional logging handler to use in creating the logger returned, or
|
None
|
log_format
|
str
|
The format to use for logged messages. Passed directly to the |
DEFAULT_LOG_FORMAT
|
Returns:
| Type | Description |
|---|---|
Logger
|
An instance of |
Logger
|
|
Examples:
>>> from flepimop2.logging import get_script_logger
>>> logger = get_script_logger(__name__, 3)
>>> logger.info("This is a log info message")
2024-10-29 16:07:20,272:INFO> This is a log info message.
Source code in src/flepimop2/logging.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |