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

Support multiple loggers #1

Open
psychemedia opened this issue May 30, 2024 · 2 comments
Open

Support multiple loggers #1

psychemedia opened this issue May 30, 2024 · 2 comments

Comments

@psychemedia
Copy link
Contributor

psychemedia commented May 30, 2024

It would be useful if users could create multiple loggers in a very simple way.

So maybe an interaction like:

from ou_logger import newLogger

logger1 = newLogger()

Or maybe something along the lines of:

from ou_logger import groupLoggers

glog = groupLoggers(["logger1", "logger2"])

glog.error("An error message for logger2", "logger2")

Need to check the docs - eg logging.getLogger(), logging filters, child / parent loggers, etc.

@psychemedia
Copy link
Contributor Author

psychemedia commented May 30, 2024

For the glog thing, ChatGPT suggests:

# File: logger.py

# Then in __init__.py:
#from .logger import GroupLoggers

import logging

class GroupLoggers:
    def __init__(self, logger_names):
        self.loggers = {}
        for name in logger_names:
            logger = logging.getLogger(name)
            logger.setLevel(logging.DEBUG)
            handler = logging.StreamHandler()
            handler.setLevel(logging.DEBUG)
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            handler.setFormatter(formatter)
            logger.addHandler(handler)
            self.loggers[name] = logger

    def set_level(self, logger_name, level):
        if logger_name in self.loggers:
            self.loggers[logger_name].remove()  # Remove existing handlers to reconfigure level
            self.loggers[logger_name].add(level=level)
        else:
            raise ValueError(f"Logger '{logger_name}' not found.")

    def log(self, level, message, logger_name):
        if logger_name in self.loggers:
            logger = self.loggers[logger_name]
            if level == 'debug':
                logger.debug(message)
            elif level == 'info':
                logger.info(message)
            elif level == 'warning':
                logger.warning(message)
            elif level == 'error':
                logger.error(message)
            elif level == 'critical':
                logger.critical(message)
            else:
                raise ValueError(f"Unsupported log level: {level}")
        else:
            raise ValueError(f"Logger '{logger_name}' not found")

    def debug(self, message, logger_name):
        self.log('debug', message, logger_name)

    def info(self, message, logger_name):
        self.log('info', message, logger_name)

    def warning(self, message, logger_name):
        self.log('warning', message, logger_name)

    def error(self, message, logger_name):
        self.log('error', message, logger_name)

    def critical(self, message, logger_name):
        self.log('critical', message, logger_name)

with usage:

from ou_logger import GroupLoggers

# Define loggers
glog = GroupLoggers(["logger1", "logger2"])

# Set logging levels
glog.set_level("logger1", "DEBUG")
glog.set_level("logger2", "ERROR")

# Log message
glog.error("An error message for logger2", "logger2")
glog.info("An info message for logger1", "logger1")

@psychemedia
Copy link
Contributor Author

We'd also need to be able to set the handler, eg to text, tts, both, etc.

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

No branches or pull requests

1 participant