Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logging for multiple scanners in a script #651

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rumpelsepp
Copy link
Member

@rumpelsepp rumpelsepp commented Dec 17, 2024

import gallia
import gallia.log
import gallia.command

logger = gallia.log.get_logger("gallia")

class Scanner1(gallia.command.Script):
    def main(self) -> None:
        logger.info(f"hi {self.__class__.__name__}")
        logger.error("error")
        logger.warning("warning")
        logger.notice("notice")
        logger.info("info")
        logger.debug("debug")
        logger.trace("trace")

class Scanner2(gallia.command.Script):
    def main(self) -> None:
        logger.info(f"hi {self.__class__.__name__}")


if __name__ == "__main__":
    # Each scanner sets up its own logging setup
    # with a logger called `gallia`. Logging setup
    # via the config class, in this case all default.
    Scanner1().entry_point()
    Scanner2().entry_point()

    # Alternatively, a context manager can be used
    # for more fine grained control.
    with gallia.log.setup_logging(
        logger_name="gallia",
        stderr_level=gallia.log.Loglevel.DEBUG,
        logfile="test.log.zst",
    ) as h:
        Scanner1(logging_handler=h).entry_point()
        Scanner2(logging_handler=h).entry_point()

The following setup is now possible when scanners are run in own
scripts:

        import gallia
        import gallia.log
        import gallia.command

        logger = gallia.log.get_logger("gallia")

        class Scanner1(gallia.command.Script):
            def main(self) -> None:
                logger.info(f"hi {self.__class__.__name__}")
                logger.error("error")
                logger.warning("warning")
                logger.notice("notice")
                logger.info("info")
                logger.debug("debug")
                logger.trace("trace")

        class Scanner2(gallia.command.Script):
            def main(self) -> None:
                logger.info(f"hi {self.__class__.__name__}")

        if __name__ == "__main__":

            # Each scanner sets up its own logging setup
            # with a logger called `gallia`.
            Scanner1().entry_point()
            Scanner2().entry_point()

            # Alternatively, a context manager can be used
            # for more fine grained control.
            with gallia.log.setup_logging(
                logger_name="gallia",
                stderr_level=gallia.log.Loglevel.DEBUG,
                logfile="test.log.zst",
            ) as h:
                Scanner1(logging_handler=h).entry_point()
                Scanner2(logging_handler=h).entry_point()
@rumpelsepp rumpelsepp added the bug Something isn't working label Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant