API reference
tinylogging
AsyncLogger
__init__
__init__(name: str, level: Level = NOTSET, formatter: Formatter = Formatter(), handlers: set[BaseAsyncHandler] = set()) -> None
Initializes an asynchronous logger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the logger. |
required |
level
|
Level
|
The logging level. Defaults to Level.NOTSET. |
NOTSET
|
formatter
|
Formatter
|
The formatter for log messages. Defaults to Formatter(). |
Formatter()
|
handlers
|
set[BaseAsyncHandler]
|
A set of handlers for the logger. Defaults to an empty set. |
set()
|
log
async
log(message: str, level: Level) -> None
Logs a message at the specified level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
level
|
Level
|
The level at which to log the message. |
required |
trace
async
trace(message: str) -> None
Logs a message with TRACE level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
debug
async
debug(message: str) -> None
Logs a message with DEBUG level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
info
async
info(message: str) -> None
Logs a message with INFO level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
notice
async
notice(message: str) -> None
Logs a message with NOTICE level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
warning
async
warning(message: str) -> None
Logs a message with WARNING level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
error
async
error(message: str) -> None
Logs a message with ERROR level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
critical
async
critical(message: str) -> None
Logs a message with CRITICAL level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
enable
enable() -> None
Enables the logger.
disable
disable() -> None
Disables the logger.
AsyncFileHandler
Bases: BaseAsyncHandler
Asynchronous handler for writing log records to a file.
__init__
__init__(file_name: str, level: Level = NOTSET, formatter: Formatter = Formatter(colorize=False)) -> None
Initializes the AsyncFileHandler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
The name of the file to write log records to. |
required |
level
|
Level
|
The logging level threshold for this handler. |
NOTSET
|
formatter
|
Formatter
|
The formatter instance to format log records. |
Formatter(colorize=False)
|
AsyncStreamHandler
Bases: BaseAsyncHandler
Asynchronous handler for streaming log records.
AsyncTelegramHandler
Bases: BaseAsyncHandler
Asynchronous handler for sending log records to a Telegram chat.
__init__
__init__(token: str, chat_id: int | str, message_thread_id: Optional[int] = None, ignore_errors: bool = False, **kwargs: Any) -> None
Initializes the AsyncTelegramHandler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
The Telegram bot token. |
required |
chat_id
|
int | str
|
The chat ID to send log records to. |
required |
message_thread_id
|
Optional[int]
|
The message thread ID (optional). |
None
|
ignore_errors
|
bool
|
Whether to ignore errors during sending. |
False
|
**kwargs
|
Any
|
Additional keyword arguments for the base handler. |
{}
|
BaseAsyncHandler
Bases: ABC
Base class for all async handlers.
Formatter
__init__
__init__(time_format: str = '[%H:%M:%S]', template: str = '{time} | {level} | {relpath}:{line} | {message}', colorize: bool = True) -> None
Initializes the Formatter instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_format
|
str
|
The format for the timestamp in log messages. |
'[%H:%M:%S]'
|
template
|
str
|
The template for formatting log messages. |
'{time} | {level} | {relpath}:{line} | {message}'
|
colorize
|
bool
|
Whether to colorize the log messages. |
True
|
Level
Bases: IntEnum
Enumeration for logging levels.
Attributes:
Name | Type | Description |
---|---|---|
NOTSET |
No level set. |
|
TRACE |
Trace level for detailed debugging. |
|
DEBUG |
Debug level for general debugging. |
|
INFO |
Informational messages. |
|
NOTICE |
Notice level for normal but significant conditions. |
|
WARNING |
Warning level for potentially harmful situations. |
|
ERROR |
Error level for error events. |
|
CRITICAL |
Critical level for severe error events. |
Record
dataclass
Represents a log record.
Attributes:
Name | Type | Description |
---|---|---|
message |
str
|
The log message. |
level |
Level
|
The log level. |
name |
str
|
The name of the logger. |
time |
datetime
|
The time the log record was created. |
filename |
str
|
The name of the file where the log record was created. |
line |
int
|
The line number in the file where the log record was created. |
function |
str
|
The function name where the log record was created. |
basename
property
basename: str
Gets the base name of the file where the log record was created.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The base name of the file. |
relpath
property
relpath: str
Gets the relative path of the file where the log record was created.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The relative path of the file. |
__post_init__
__post_init__() -> None
Initializes additional attributes after the dataclass is created.
Raises:
Type | Description |
---|---|
RuntimeError
|
If the stack frame cannot be retrieved. |
to_dict
to_dict() -> dict[str, Any]
Converts the log record to a dictionary.
Returns:
Name | Type | Description |
---|---|---|
dict |
dict[str, Any]
|
A dictionary representation of the log record. |
Logger
__init__
__init__(name: str, level: Level = NOTSET, formatter: Formatter = Formatter(), handlers: set[BaseHandler] = set()) -> None
Initializes a new Logger instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the logger. |
required |
level
|
Level
|
The logging level. Defaults to Level.NOTSET. |
NOTSET
|
formatter
|
Formatter
|
The formatter for log messages. Defaults to Formatter(). |
Formatter()
|
handlers
|
set[BaseHandler]
|
A set of handlers for the logger. Defaults to an empty set. |
set()
|
log
log(message: str, level: Level) -> None
Logs a message with the specified logging level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
level
|
Level
|
The logging level for the message. |
required |
trace
trace(message: str) -> None
Logs a message with TRACE level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
debug
debug(message: str) -> None
Logs a message with DEBUG level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
info
info(message: str) -> None
Logs a message with INFO level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
notice
notice(message: str) -> None
Logs a message with NOTICE level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
warning
warning(message: str) -> None
Logs a message with WARNING level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
error
error(message: str) -> None
Logs a message with ERROR level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
critical
critical(message: str) -> None
Logs a message with CRITICAL level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
enable
enable() -> None
Enables the logger.
disable
disable() -> None
Disables the logger.
BaseHandler
Bases: ABC
Abstract base class for all handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatter
|
Formatter
|
Formatter instance to format the log records. |
Formatter()
|
level
|
Level
|
Logging level for the handler. |
NOTSET
|
FileHandler
Bases: BaseHandler
Handler for writing log records to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
Name of the file to write log records to. |
required |
level
|
Level
|
Logging level for the handler. |
NOTSET
|
formatter
|
Formatter
|
Formatter instance to format the log records. |
Formatter(colorize=False)
|
LoggingAdapterHandler
Bases: Handler
Adapter handler to integrate with the standard logging module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler
|
BaseHandler
|
Custom handler to delegate log records to. |
required |
emit
emit(record: LogRecord) -> None
Emit a log record using the custom handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record
|
LogRecord
|
The log record to be emitted. |
required |
StreamHandler
Bases: BaseHandler
Handler for streaming log records to a stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatter
|
Formatter
|
Formatter instance to format the log records. |
Formatter()
|
level
|
Level
|
Logging level for the handler. |
NOTSET
|
stream
|
Optional[TextIO]
|
Stream to write log records to. |
None
|
TelegramHandler
Bases: BaseHandler
Handler for sending log records to a Telegram chat.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
Telegram bot token. |
required |
chat_id
|
int | str
|
Chat ID to send messages to. |
required |
ignore_errors
|
bool
|
Whether to ignore errors when sending messages. |
False
|
message_thread_id
|
Optional[int]
|
ID of the message thread. |
None
|
**kwargs
|
Any
|
Additional keyword arguments for the base handler. |
{}
|
aio
AsyncFileHandler
Bases: BaseAsyncHandler
Asynchronous handler for writing log records to a file.
__init__
__init__(file_name: str, level: Level = NOTSET, formatter: Formatter = Formatter(colorize=False)) -> None
Initializes the AsyncFileHandler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
The name of the file to write log records to. |
required |
level
|
Level
|
The logging level threshold for this handler. |
NOTSET
|
formatter
|
Formatter
|
The formatter instance to format log records. |
Formatter(colorize=False)
|
AsyncTelegramHandler
Bases: BaseAsyncHandler
Asynchronous handler for sending log records to a Telegram chat.
__init__
__init__(token: str, chat_id: int | str, message_thread_id: Optional[int] = None, ignore_errors: bool = False, **kwargs: Any) -> None
Initializes the AsyncTelegramHandler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
The Telegram bot token. |
required |
chat_id
|
int | str
|
The chat ID to send log records to. |
required |
message_thread_id
|
Optional[int]
|
The message thread ID (optional). |
None
|
ignore_errors
|
bool
|
Whether to ignore errors during sending. |
False
|
**kwargs
|
Any
|
Additional keyword arguments for the base handler. |
{}
|
BaseAsyncHandler
Bases: ABC
Base class for all async handlers.
AsyncLogger
__init__
__init__(name: str, level: Level = NOTSET, formatter: Formatter = Formatter(), handlers: set[BaseAsyncHandler] = set()) -> None
Initializes an asynchronous logger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the logger. |
required |
level
|
Level
|
The logging level. Defaults to Level.NOTSET. |
NOTSET
|
formatter
|
Formatter
|
The formatter for log messages. Defaults to Formatter(). |
Formatter()
|
handlers
|
set[BaseAsyncHandler]
|
A set of handlers for the logger. Defaults to an empty set. |
set()
|
log
async
log(message: str, level: Level) -> None
Logs a message at the specified level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
level
|
Level
|
The level at which to log the message. |
required |
trace
async
trace(message: str) -> None
Logs a message with TRACE level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
debug
async
debug(message: str) -> None
Logs a message with DEBUG level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
info
async
info(message: str) -> None
Logs a message with INFO level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
notice
async
notice(message: str) -> None
Logs a message with NOTICE level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
warning
async
warning(message: str) -> None
Logs a message with WARNING level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
error
async
error(message: str) -> None
Logs a message with ERROR level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
critical
async
critical(message: str) -> None
Logs a message with CRITICAL level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
enable
enable() -> None
Enables the logger.
disable
disable() -> None
Disables the logger.
handlers
BaseAsyncHandler
Bases: ABC
Base class for all async handlers.
AsyncStreamHandler
Bases: BaseAsyncHandler
Asynchronous handler for streaming log records.
AsyncFileHandler
Bases: BaseAsyncHandler
Asynchronous handler for writing log records to a file.
__init__
__init__(file_name: str, level: Level = NOTSET, formatter: Formatter = Formatter(colorize=False)) -> None
Initializes the AsyncFileHandler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
The name of the file to write log records to. |
required |
level
|
Level
|
The logging level threshold for this handler. |
NOTSET
|
formatter
|
Formatter
|
The formatter instance to format log records. |
Formatter(colorize=False)
|
AsyncTelegramHandler
Bases: BaseAsyncHandler
Asynchronous handler for sending log records to a Telegram chat.
__init__
__init__(token: str, chat_id: int | str, message_thread_id: Optional[int] = None, ignore_errors: bool = False, **kwargs: Any) -> None
Initializes the AsyncTelegramHandler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
The Telegram bot token. |
required |
chat_id
|
int | str
|
The chat ID to send log records to. |
required |
message_thread_id
|
Optional[int]
|
The message thread ID (optional). |
None
|
ignore_errors
|
bool
|
Whether to ignore errors during sending. |
False
|
**kwargs
|
Any
|
Additional keyword arguments for the base handler. |
{}
|
formatter
Formatter
__init__
__init__(time_format: str = '[%H:%M:%S]', template: str = '{time} | {level} | {relpath}:{line} | {message}', colorize: bool = True) -> None
Initializes the Formatter instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_format
|
str
|
The format for the timestamp in log messages. |
'[%H:%M:%S]'
|
template
|
str
|
The template for formatting log messages. |
'{time} | {level} | {relpath}:{line} | {message}'
|
colorize
|
bool
|
Whether to colorize the log messages. |
True
|
helpers
TelegramFormatter
Bases: Formatter
__init__
__init__(time_format: str = '%H:%M:%S')
Initializes the TelegramFormatter with a specific time format and template.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_format
|
str
|
The format string for formatting the time. Defaults to "%H:%M:%S". |
'%H:%M:%S'
|
level
Level
Bases: IntEnum
Enumeration for logging levels.
Attributes:
Name | Type | Description |
---|---|---|
NOTSET |
No level set. |
|
TRACE |
Trace level for detailed debugging. |
|
DEBUG |
Debug level for general debugging. |
|
INFO |
Informational messages. |
|
NOTICE |
Notice level for normal but significant conditions. |
|
WARNING |
Warning level for potentially harmful situations. |
|
ERROR |
Error level for error events. |
|
CRITICAL |
Critical level for severe error events. |
record
Record
dataclass
Represents a log record.
Attributes:
Name | Type | Description |
---|---|---|
message |
str
|
The log message. |
level |
Level
|
The log level. |
name |
str
|
The name of the logger. |
time |
datetime
|
The time the log record was created. |
filename |
str
|
The name of the file where the log record was created. |
line |
int
|
The line number in the file where the log record was created. |
function |
str
|
The function name where the log record was created. |
basename
property
basename: str
Gets the base name of the file where the log record was created.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The base name of the file. |
relpath
property
relpath: str
Gets the relative path of the file where the log record was created.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The relative path of the file. |
__post_init__
__post_init__() -> None
Initializes additional attributes after the dataclass is created.
Raises:
Type | Description |
---|---|
RuntimeError
|
If the stack frame cannot be retrieved. |
to_dict
to_dict() -> dict[str, Any]
Converts the log record to a dictionary.
Returns:
Name | Type | Description |
---|---|---|
dict |
dict[str, Any]
|
A dictionary representation of the log record. |
sync
Formatter
__init__
__init__(time_format: str = '[%H:%M:%S]', template: str = '{time} | {level} | {relpath}:{line} | {message}', colorize: bool = True) -> None
Initializes the Formatter instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_format
|
str
|
The format for the timestamp in log messages. |
'[%H:%M:%S]'
|
template
|
str
|
The template for formatting log messages. |
'{time} | {level} | {relpath}:{line} | {message}'
|
colorize
|
bool
|
Whether to colorize the log messages. |
True
|
BaseHandler
Bases: ABC
Abstract base class for all handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatter
|
Formatter
|
Formatter instance to format the log records. |
Formatter()
|
level
|
Level
|
Logging level for the handler. |
NOTSET
|
FileHandler
Bases: BaseHandler
Handler for writing log records to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
Name of the file to write log records to. |
required |
level
|
Level
|
Logging level for the handler. |
NOTSET
|
formatter
|
Formatter
|
Formatter instance to format the log records. |
Formatter(colorize=False)
|
LoggingAdapterHandler
Bases: Handler
Adapter handler to integrate with the standard logging module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler
|
BaseHandler
|
Custom handler to delegate log records to. |
required |
emit
emit(record: LogRecord) -> None
Emit a log record using the custom handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record
|
LogRecord
|
The log record to be emitted. |
required |
TelegramHandler
Bases: BaseHandler
Handler for sending log records to a Telegram chat.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
Telegram bot token. |
required |
chat_id
|
int | str
|
Chat ID to send messages to. |
required |
ignore_errors
|
bool
|
Whether to ignore errors when sending messages. |
False
|
message_thread_id
|
Optional[int]
|
ID of the message thread. |
None
|
**kwargs
|
Any
|
Additional keyword arguments for the base handler. |
{}
|
Logger
__init__
__init__(name: str, level: Level = NOTSET, formatter: Formatter = Formatter(), handlers: set[BaseHandler] = set()) -> None
Initializes a new Logger instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the logger. |
required |
level
|
Level
|
The logging level. Defaults to Level.NOTSET. |
NOTSET
|
formatter
|
Formatter
|
The formatter for log messages. Defaults to Formatter(). |
Formatter()
|
handlers
|
set[BaseHandler]
|
A set of handlers for the logger. Defaults to an empty set. |
set()
|
log
log(message: str, level: Level) -> None
Logs a message with the specified logging level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
level
|
Level
|
The logging level for the message. |
required |
trace
trace(message: str) -> None
Logs a message with TRACE level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
debug
debug(message: str) -> None
Logs a message with DEBUG level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
info
info(message: str) -> None
Logs a message with INFO level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
notice
notice(message: str) -> None
Logs a message with NOTICE level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
warning
warning(message: str) -> None
Logs a message with WARNING level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
error
error(message: str) -> None
Logs a message with ERROR level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
critical
critical(message: str) -> None
Logs a message with CRITICAL level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to log. |
required |
enable
enable() -> None
Enables the logger.
disable
disable() -> None
Disables the logger.
handlers
BaseHandler
Bases: ABC
Abstract base class for all handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatter
|
Formatter
|
Formatter instance to format the log records. |
Formatter()
|
level
|
Level
|
Logging level for the handler. |
NOTSET
|
StreamHandler
Bases: BaseHandler
Handler for streaming log records to a stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatter
|
Formatter
|
Formatter instance to format the log records. |
Formatter()
|
level
|
Level
|
Logging level for the handler. |
NOTSET
|
stream
|
Optional[TextIO]
|
Stream to write log records to. |
None
|
FileHandler
Bases: BaseHandler
Handler for writing log records to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
Name of the file to write log records to. |
required |
level
|
Level
|
Logging level for the handler. |
NOTSET
|
formatter
|
Formatter
|
Formatter instance to format the log records. |
Formatter(colorize=False)
|
LoggingAdapterHandler
Bases: Handler
Adapter handler to integrate with the standard logging module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler
|
BaseHandler
|
Custom handler to delegate log records to. |
required |
emit
emit(record: LogRecord) -> None
Emit a log record using the custom handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record
|
LogRecord
|
The log record to be emitted. |
required |
TelegramHandler
Bases: BaseHandler
Handler for sending log records to a Telegram chat.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
Telegram bot token. |
required |
chat_id
|
int | str
|
Chat ID to send messages to. |
required |
ignore_errors
|
bool
|
Whether to ignore errors when sending messages. |
False
|
message_thread_id
|
Optional[int]
|
ID of the message thread. |
None
|
**kwargs
|
Any
|
Additional keyword arguments for the base handler. |
{}
|