[Tutor] Create Logging module
Sinardy Xing
sinardyxing at gmail.com
Fri Aug 2 00:46:16 EDT 2019
Thanks Alan,
I learn alot.
logger.setLevel('INFO') <----- If I did not include this in the code it
not generating any log I am confuse because I have setLevel to file_handler
and to stream_handler
file_handler.setLevel('DEBUG')
stream_handler.setLevel('DEBUG')
On Fri, Aug 2, 2019 at 12:14 AM Alan Gauld via Tutor <tutor at python.org>
wrote:
> On 01/08/2019 10:11, Sinardy Xing wrote:
>
> > ---- start here---
> >
> > import logging
> >
> > ..snip...
>
>
> > from functools import wraps
> >
> > def logme(func_to_log):
> > import logging
>
> You don't need the import, it's already done in the first line.
>
>
> > #Check log level within understanable parameter, set to INFO if is
> not
> > permitable value
> > def check_log_level(logleveltocheck):
>
> This looks like an indentation error?
> It should be at the same level as the import statement.
>
> > if any(logleveltocheck.upper() in lf for lf in ['DEBUG',
> > 'INFO', 'WARNING', 'ERROR', 'CRITICAL']):
> > return logleveltocheck.upper()
>
> Are you sure that is what you want? It seems very complicated unless you
> are allowing the user to supply an abbreviated form. Otherwise
>
> if logleveltocheck.upper() in ['DEBUG', 'INFO', 'WARNING',
> 'ERROR', 'CRITICAL']:
> return logleveltocheck.upper()
>
> might be easier?
>
> > else
> > return 'INFO'
> >
> > log_file_level='INFO' #check_log_level('info')
> > log_console_level='INFO' #check_log_level('info')
> >
> > #root level
> > logger.setLevel('INFO')
>
> I'm not sure what this is supposed to be doing!
>
> > 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
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list