Re: [Tutor] Create Logging module
Thank you Alan, - from previous mail > 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! -- end I am thinking about a module where user can change the logging mode and doing so without modifying code, I am thinking to extend it with parameter module. I seen many software that if user set logging Level 1,2,3,4 or support mode, then application will create more information in the log file accordingly. The method I am using most likely is wrong. I dont know where should I trigger the log Level to achieve parameterize logging mode. I have one question I have following set stream_handler.setLevel('INFO')<--- handler to console file_handler.setLevel('INFO') < handler to file both are added to logger logger.addHandler(file_handler) logger.addHandler(stream_handler) If I did not set the logger.setLevel like following logger.setLevel('INFO') the logger unable to process logger.info('Current log level is : {}'.format(logger.level)) > return 10 if logger.setLevel('DEBUG') if not My question is what is the different between stream_handler.setLevel('INFO') and logger.setLevel('INFO') Why do we need logger.setLevel('INFO') if we stated following stream_handler.setLevel('INFO')<--- handler to console file_handler.setLevel('INFO') < handler to file --- complete code 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(orig_func): #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): #as per advised from Alan Gauld @python.org if logleveltocheck.upper() 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('DEBUG') 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(file_handler) logger.addHandler(stream_handler) #this wraps is to make sure we are returning orig_func instead of wrapper @wraps(orig_func) def wrapper(*args, **kwargs): logger.info('Ran with args: {}, and kwargs: {}'.format(args, kwargs)) logger.info('Current log level is : {}'.format(logger.level)) return orig_func(*args, **kwargs) return wrapper def timer(orig_func): import time #this wraps is to make sure we are returning orig_func instead of wrapper @wraps(orig_func) def wrapper(*args, **kwargs): t1 = time.time() result = orig_func(*args, **kwargs) t2 = time.time() - t1 print('{} ran in {} sec'.format(orig_func.__name__, t2)) return result @logme def hello(name, age): print('I am {}, and I am {} years old'.format(name, age)) hello('Peter Parker', 20) On Fri, Aug 2, 2019 at 12:14 AM Alan Gauld via Tutor 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 understa
Re: [Tutor] Create Logging module
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 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@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Create Logging module
Hi I solve my problem, the decorator need to be above the function not when executed. :) Thanks for reading. :) On Thu, Aug 1, 2019 at 8:42 PM Steven D'Aprano wrote: > On Thu, Aug 01, 2019 at 05:11:04PM +0800, Sinardy Xing wrote: > > > I have error look like in the wrapper. > > > > Can someone point to me where is the issue > > No, but you can. > > When the error occurs, Python will print a traceback containing a list > of the lines of code being executed, and the final error. You should > read that error, especially the last line, and it will tell you the line > of code that failed and give you a clue why it failed. > > We can help you if you copy and paste the full traceback, starting with > the line "Traceback..." to the end. > > Don't take a screen shot or photo and send that, instead copy and paste > the text into your email. > > (P.S. please reply to the mailing list, not to me personally.) > > > -- > Steven > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Create Logging module
Hi Steven, Thanks for your reply, I was copy and paste the code in the email as a text. I dont know why it becoming photo or screen shot when you view it ? When I run the module individually it is no error only when I use as decorator I have error. $ cat mainapp.py from loggingme import logme def say_hello(name, age): print('Hello {}, I am {}'.format(name, age)) @logme say_hello('Tonny', 8) $ python3 mainapp.py File "mainapp.py", line 8 say_hello('Tonny', 8) ^ SyntaxError: invalid syntax On Thu, Aug 1, 2019 at 8:42 PM Steven D'Aprano wrote: > On Thu, Aug 01, 2019 at 05:11:04PM +0800, Sinardy Xing wrote: > > > I have error look like in the wrapper. > > > > Can someone point to me where is the issue > > No, but you can. > > When the error occurs, Python will print a traceback containing a list > of the lines of code being executed, and the final error. You should > read that error, especially the last line, and it will tell you the line > of code that failed and give you a clue why it failed. > > We can help you if you copy and paste the full traceback, starting with > the line "Traceback..." to the end. > > Don't take a screen shot or photo and send that, instead copy and paste > the text into your email. > > (P.S. please reply to the mailing list, not to me personally.) > > > -- > Steven > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Create Logging module
Sinardy Xing wrote: > 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) Isn't this a SyntaxError? You can decorate functions, not function calls: When Python finds a syntax error in your main script it won't proceed to run it and thus the bugs in modules that would be imported if the code were executed don't matter at this point. Try @logme def say_hello(name, age): print('Hello {}, I am {}'.format(name, age)) say_hello('Tonny', 8) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Create Logging module
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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Create Logging module
On Thu, Aug 01, 2019 at 05:11:04PM +0800, Sinardy Xing wrote: > I have error look like in the wrapper. > > Can someone point to me where is the issue No, but you can. When the error occurs, Python will print a traceback containing a list of the lines of code being executed, and the final error. You should read that error, especially the last line, and it will tell you the line of code that failed and give you a clue why it failed. We can help you if you copy and paste the full traceback, starting with the line "Traceback..." to the end. Don't take a screen shot or photo and send that, instead copy and paste the text into your email. (P.S. please reply to the mailing list, not to me personally.) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor