0

Hello Stack Overflow users,

i'm writing a simple code and i got a "AttributeError" exception

log_level = 'INFO' # DEBUG - INFO - WARNING - ERROR - CRITICAL

logging.basicConfig(
        format=('%(asctime)s > [%(levelname)s] in %(funcName)s on line '
                '%(lineno)d > %(message)s'), level = logging.log_level, \
                filename='logs.log', filemode='w', encoding='utf-8')

Sorry for asking a suck stupid question.

Thank you in advance.

3
  • level = logging.log_level should be just log_level maybe ? It will probably be easier to help you if you share the full error stack trace. Commented Apr 14, 2021 at 14:34
  • Thank you it worked :) ! Commented Apr 14, 2021 at 14:45
  • My pleasure : ) Commented Apr 14, 2021 at 14:47

1 Answer 1

2

Not very clear on your requirement, but the below approach can be a neat solution

def get_logger(cls, logger_name, create_file=False):

    # create logger
    log = logging.getLogger(logger_name)
    log.setLevel(level=logging.INFO)

    # create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    if create_file:
        # create file handler for logger.
        fh = logging.FileHandler('my_log.log')
        fh.setLevel(level=logging.DEBUG)
        fh.setFormatter(formatter)
    # reate console handler for logger.
    ch = logging.StreamHandler()
    ch.setLevel(level=logging.DEBUG)
    ch.setFormatter(formatter)

    # add handlers to logger.
    if create_file:
        log.addHandler(fh)

    log.addHandler(ch)
    return log 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.