Skip to content

HamletSargsyan/tinylogging

Repository files navigation

tinylogging

GitHub License GitHub commit activity PyPI - Downloads PyPI - Version PyPI - Python Version Checks Documentation

Installation

pip install tinylogging

Usage

New logger

from tinylogging import Logger, Level

logger = Logger(name="my_logger", level=Level.DEBUG)

Logging messages

logger.info("This is an info message.")
logger.error("This is an error message.")
logger.debug("This is a debug message.")

Logging to a file

from tinylogging import FileHandler

file_handler = FileHandler(file_name="app.log", level=Level.WARNING)
logger.handlers.add(file_handler)

logger.warning("This warning will be logged to both console and file.")

Custom formatting

from tinylogging import Formatter

formatter = Formatter(template="{time} - {name} - {level} - {message}", colorize=False)
logger = Logger(name="custom_logger", formatter=formatter)
logger.info("This log message uses a custom format.")

Disabling logging

logger.disable()
logger.info("This message will not be logged.")
logger.enable()

Async support

import asyncio
from tinylogging import AsyncLogger, AsyncFileHandler


async def main():
    logger = AsyncLogger(name="async_logger")

    file_handler = AsyncFileHandler(file_name="app.log")
    logger.handlers.add(file_handler)

    await logger.info("This is an info message.")
    await logger.error("This is an error message.")
    await logger.debug("This is a debug message.")


if __name__ == "__main__":
    asyncio.run(main)

License

This project is licensed under the MIT License.