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

Implement loggers #47

Closed
barneydobson opened this issue Feb 20, 2024 · 2 comments · Fixed by #72
Closed

Implement loggers #47

barneydobson opened this issue Feb 20, 2024 · 2 comments · Fixed by #72
Assignees

Comments

@barneydobson
Copy link
Collaborator

          Probably you should implement a proper logger at some point and do somthing like this:
        if ix > 0:
            LOGGER.warning('''a node has multiple successors, 
                not sure how that can happen if using shortest path
                to derive topology''')

Maybe open an issue about it, if not already there.

Originally posted by @dalonsoa in #31 (comment)

@barneydobson barneydobson mentioned this issue Feb 20, 2024
13 tasks
@cheginit
Copy link
Collaborator

For logging, I highly recommend loguru. I've been using it for many of my packages, since it doesn't have any additional dependency.

I have this function, usually in a utils module:

import os
import sys
import loguru

def get_logger(verbose: bool = False) -> Logger:
    """Get a logger."""
    logger = loguru.logger
    logger.configure(
        handlers=[
            {
                "sink": sys.stdout,
                "colorize": True,
                "format": " | ".join(
                    [
                        "<cyan>{time:YYYY/MM/DD HH:mm:ss}</>",
                        "{message}",
                    ]
                ),
            }
        ]
    )
    if os.getenv("PACKAGE_NAME_VERBOSE", str(verbose)).lower() == "true":
        logger.enable("package_name")
    else:
        logger.disable("package_name")
    return logger

This way, I can easily control the verbosity of the package with an env variable (PACKAGE_NAME_VERBOSE). In the code, whenever I want to use the logger, I just do:

logger = get_logger()
logger.info("This is a log")

By default, the logger is disabled and needs to be enabled explicitly using the env variable like so:

os.environ["PACKAGE_NAME_VERBOSE"] = "true"

It's a simple and clean solution and doesn't interfere with the logging level of other packages.

@barneydobson barneydobson mentioned this issue Mar 1, 2024
4 tasks
@dalonsoa
Copy link
Collaborator

dalonsoa commented Mar 4, 2024

I would personally use just the standard logger that comes with Python. I've never had the need of another, third party tool, very often just syntactic sugar for the standard logger. It has its drawbacks, of course, but still it is really powerful and, to be honest, it is WIDELY use, so learning to use it properly will help you understand other tools.

Here you have an interesting video on pros, cons and how to use it right: https://www.youtube.com/watch?v=9L77QExPmI0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants