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

Expose logging to user #36

Open
darkdragon-001 opened this issue May 29, 2020 · 2 comments
Open

Expose logging to user #36

darkdragon-001 opened this issue May 29, 2020 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@darkdragon-001
Copy link

darkdragon-001 commented May 29, 2020

I see you use logging library already. It would be nice, if the user could add additional logging.Formatter or logging.Handler (e.g. FileHandler with custom path and level, SMTPHandler).

Config file could consist of

  • import (or can this be determined based from handlerType automatically?)
  • handlerType, handlerArgs for constructor
  • level
  • format string
@darkdragon-001
Copy link
Author

darkdragon-001 commented May 29, 2020

Library:

import logging

def getLevel(level):
    return getattr(logging, level.upper())
def getType(type):  # https://stackoverflow.com/a/452981/3779655
    parts = type.split('.')
    module = ".".join(parts[:-1])
    m = __import__(module)
    for comp in parts[1:]:
        m = getattr(m, comp)
    return m
# TODO allow variables for all handlerArgs/handlerKwargs of type str (configName, ...)
def addLoggerHandler(logger, handlerType, level=None, fmt=None, datefmt=None, *handlerArgs, **handlerKwargs):
    handler = getType(handlerType)(*handlerArgs,**handlerKwargs)
    if 'filename' in handlerKwargs:
        os.makedirs(handlerKwargs['filename'].rpartition('/')[2], exist_ok=True)
    if level is not None:
        handler.setLevel(getLevel(level))
    fmt_kwargs = {}
    if fmt is not None:
        fmt_kwargs['fmt'] = fmt
    if datefmt is not None:
        fmt_kwargs['datefmt'] = datefmt
    formatter = logging.Formatter(**fmt_kwargs)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

An example call could be:

import logging
logger = logging.getLogger(__name__)

for _,handler in config['logger']['handlers'].items():
    addLoggerHandler(logger, **handler)

An example configuration could look like:

[logger.handlers.Default]
type = 'logging.StreamHandler'
level = 'info'
fmt = '%(message)s'

@andreasnuesslein
Copy link
Member

same here: just because code CAN be added, doesnt mean it has to, unless there's a good scenario to do so.

@andreasnuesslein andreasnuesslein added the question Further information is requested label Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants