def logSet(logPath, level='info', reNew=True, recDate=True, recLevel=True, haNew=False): """ Set up logger. Input logPath - log path level - level, {'info'} | reNew - flag of creating a new log, {True} | False recDate - flag of recording time, {True} | False recLevel - flag of recording time, {True} | False haNew - flag of creating all new handler, True | {False} """ import io # create the log folder if necessary logFold = os.path.dirname(logPath) io.mkDir(logFold) # delete old log if reNew: io.rmFile(logPath) # remove onld handler logger = logging.getLogger() for handler in logger.handlers[:]: logger.removeHandler(handler) # level if level == 'info': level = logging.INFO else: raise Exception('unknown level: {}'.format(level)) # format format = '%(message)s' if recLevel: format = '%(levelname)s; ' + format if recDate: format = '%(asctime)s; ' + format # set logging.basicConfig(level=level, filename=logPath, format=format, datefmt="%Y-%m-%d %H:%M:%S")
def logSet(logPath, level='info', reNew=True, recDate=True, recLevel=True): """ Set up logger. The log file can be found at 'work/save/$logProj/log/$logNm' Input logPath - log path level - level, {'info'} | reNew - flag of creating a new log, {True} | False recDate - flag of recording time, {True} | False recLevel - flag of recording time, {True} | False """ import io # create the log folder if necessary logFold = os.path.dirname(logPath) io.mkDir(logFold) # delete old log if reNew: io.rmFile(logPath) # level if level == 'info': level = logging.INFO else: raise Exception('unknown level: {}'.format(level)) # format format = '%(message)s' if recLevel: format = '%(levelname)s; ' + format if recDate: format = '%(asctime)s; ' + format # set logging.basicConfig(level=level, filename=logPath, format=format, datefmt="%Y-%m-%d %H:%M:%S")