Logging Strategy for a GUI Program

I think it might be useful to sprinkle some debug logs in my application (a program like drawing) and write this information to a file. My current debugging strategy is to hook up a custom exception listener (sys.excepthook) and allow the user to email me a copy of the stack trace that caused the crash.

It was very convenient when viewing what the user did to cause the program to crash, but I feel that the log file will certainly help. I am wondering what is the best way to do this. I am thinking of enabling logging through the command line switch and creating a log to “run” the program and email a copy of the log when it fails. However, the log will not help if the application is not in debug mode!

I'm a little worried that the log is filling up too quickly - if I put the log in some mouse motion event handlers, then it will create a lot of entries. In addition, the log file can grow quite large and simply fill up with unnecessary information for me when analyzing the error report.

How do you guys handle this? I am interested in the frequency of logging - since my application responds to many events (for example, mouse movement, depending on user input), I do not want to create redundant entries.

+3
source share
2 answers

, . (, ) , . , , ( stdout). , (, ).

:

import logging
import logging.handlers as handlers

logger = logging.getLogger('myapp')
hdlr = logging.FileHandler('/tmp/myapp.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)

logger.debug('debug!')
logger.info('info!')
logger.warning('warning!')
logger.error('error!')
logger.critical('critical!')

myapp.log:

magun@~: cat /tmp/myapp.log
2010-11-05 12:27:25,359 WARNING: warning!
2010-11-05 12:27:25,362 ERROR: error!
2010-11-05 12:27:26,071 CRITICAL: critical!
magun@~:

, , , :

import logging
import logging.handlers as handlers

logger = logging.getLogger('myapp')
hdlr = handlers.RotatingFileHandler('/tmp/log/myapp.log', maxBytes=100, backupCount=5)
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)

for i in range(20):
    logger.debug('debug%i!'%i)
    logger.info('info%i!'%i)
    logger.warning('warning%i!'%i)
    logger.error('error%i!'%i)
    logger.critical('critical%i!'%i)

100 ( , ) 5. , 100 , "": ( ) myapp.log , myapp.log.1. myapp.log.1 myapp.log.2, myapp.log myapp.log.1. , myapp.log, myapp.log.1, myapp.log.2,... myapp.log.n( myapp.log.5). , , myapp.log.5 . , , 5 * 100 .

, :

magun@~: ls /tmp/log/
myapp.log  myapp.log.1  myapp.log.2  myapp.log.3  myapp.log.4  myapp.log.5
magun@~: cat /tmp/log/myapp.log
2010-11-05 12:33:52,369 ERROR: error19!
2010-11-05 12:33:52,376 CRITICAL: critical19!
magun@~: cat /tmp/log/myapp.log.1
2010-11-05 12:33:52,362 CRITICAL: critical18!
2010-11-05 12:33:52,369 WARNING: warning19!
magun@~: cat /tmp/log/myapp.log.2
2010-11-05 12:33:52,355 WARNING: warning18!
2010-11-05 12:33:52,362 ERROR: error18!
magun@~: cat /tmp/log/myapp.log.3
2010-11-05 12:33:52,348 ERROR: error17!
2010-11-05 12:33:52,355 CRITICAL: critical17!
magun@~: cat /tmp/log/myapp.log.4
2010-11-05 12:33:52,340 CRITICAL: critical16!
2010-11-05 12:33:52,348 WARNING: warning17!
magun@~: cat /tmp/log/myapp.log.5
2010-11-05 12:33:52,333 WARNING: warning16!
2010-11-05 12:33:52,340 ERROR: error16!
magun@~:

, (0-15), , . :)

+5

Source: https://habr.com/ru/post/1771076/


All Articles