Apr-24-2022, 09:19 PM
I have problem with loggers. One function of my program is initiazing logger in specified directory. Second function is closing this logger in order to change name of direcotry to "directory_old" and creating new "directory", then logging should be initiaze again. It must be done this way because active logger locks directory. But I don't know how to close it or disable from another function, I know about ".close()" in loggers but in my case I have no idea how to use it in clear(self) function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import logging import os class self : def logger( self ): self .log = logging.getLogger() self .log.handlers = [] self .log.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s | %(levelname)s | %(message)s' ) loggingFilehandler = logging.FileHandler( 'directory\\logs.log' ) loggingFilehandler.setLevel(logging.DEBUG) loggingFilehandler.setFormatter(formatter) self .log.addHandler(loggingFilehandler) def clear( self ): # self .log.addHandler.close() os.rename( "directory" , "directory_old" , ) # create new directory os.mkdir( "directory" ) self .logger( self ) self .clear( self ) |