write logs in python with an inline message

You can implement your own logger class in a way that behaves similarly to the default logger, but using *args to take an arbitrarily long number of parameters

See example:

import logging


class Logger:
    def __init__(self, name, level):
        logging.basicConfig(level=level)
        self._logger = logging.getLogger(name)

    def debug(self, *args):
        for arg in args:
            self._logger.debug(arg)

    def info(self, *args):
        for arg in args:
            self._logger.info(arg)

    def warning(self, *args):
        for arg in args:
            self._logger.warning(arg)

    def error(self, *args):
        for arg in args:
            self._logger.error(arg)


logger = Logger("test_logger", level=logging.INFO)
logger.info("logline1", "logline2", "logline3")

The output will be:

INFO:test_logger:logline1
INFO:test_logger:logline2
INFO:test_logger:logline3

Which can be adjusted by passing a format to the logging.basicConfig

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top