Create Logging module

Sinardy Xing sinardyxing at gmail.com
Thu Aug 1 05:08:51 EDT 2019


Hi,

I am learning to create python logging.

My goal is to create a logging module where I can use as decorator in my
main app

following is the logging code

---- start here---

import logging

#DEBUG    : Detailed information, typically of interest only when
diagnosing problems.
#INFO     : Confirmation that things are working as expected.
#WARNING  (default): An indication that something unexpected happened, or
indicative of some problem in the near future
#         (e.g. 'disk space low'). The software is still working as
expected.
#ERROR    : Due to a more serious problem, the software has not been able
to perform some function.
#CRITICAL :A serious error, indicating that the program itself may be
unable to continue running.

from functools import wraps

def logme(func_to_log):
    import logging

    #Without getLogger name it will log all in root
    logger = logging.getLogger(__name__)

    #Check log level within understanable parameter, set to INFO if is not
 permitable value
        def check_log_level(logleveltocheck):
            if any(logleveltocheck.upper() in lf for lf in ['DEBUG',
'INFO', 'WARNING', 'ERROR', 'CRITICAL']):
                return logleveltocheck.upper()
            else:
                return 'INFO'

    log_file_level='INFO' #check_log_level('info')
    log_console_level='INFO' #check_log_level('info')

    #root level
    logger.setLevel('INFO')

    formatter = logging.Formatter('%(asctime)s :: %(name)s :: %(levelname)s
:: %(message)s')

    #Read log file from parameter
    logfile='mylogfile.log'
    file_handler = logging.FileHandler(logfile)
    file_handler.setLevel(log_file_level)
    file_handler.setFormatter(formatter)

    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(log_console_level)
    stream_handler.setFormatter(formatter)

    logger.addHandler()
    logger.addHandler(stream_handler)

    #this wraps is to make sure we are returning func_to_log instead of
wrapper
    @wraps(func_to_log)
    def wrapper(*args, **kwargs):
        logger.info('Ran with args: {}, and kwargs: {}'.format(args,
kwargs))
        return func_to_log(*args, **kwargs)

    return wrapper


def timer(func_to_log):
    import time

    #this wraps is to make sure we are returning func_to_log instead of
wrapper
    @wraps(func_to_log)
    def wrapper(*args, **kwargs):
        t1 = time.time()
        result = func_to_log(*args, **kwargs)
        t2 = time.time() - t1
        print('{} ran in {} sec'.format(func_to_log.__name__, t2))
        return result

--- end here---


following is my main app

-- start here--
from loggingme import logme

def say_hello(name, age):
        print('Hello {}, I am {}'.format(name, age))

#say_hello=logme(say_hello('Sinardy'))
@logme
say_hello('Tonny', 8)

--- end here---


I have error look like in the wrapper.

Can someone point to me where is the issue or is this the correct way to
create logging module?

PS: above code with python 3.7.4

Thank you.

regards,
C



More information about the Python-list mailing list