我想要做的就是对我正在编写的程序使用记录器,并且它在日志中什么也没有写,这是相关的代码片段:
#!/usr/bin/python3.6
import logging
class Excel2csv():
def __init__(self, input=None, output=None ):
self.process_options(input, output)
self.create_log_directory()
self.config_logger()
def config_logger(self):
logger = os.path.basename(__file__).split(".")[0]
global logger_location
logger_location = os.getcwd() + '/log/' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + '.log'
print("logger_location:" + logger_location)
logging.basicConfig(filename=logger_location, format='%(asctime)s %(levelname)s %(message)s')
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.INFO)
def run(self):
start_time = time.time()
self.extract2csv()
stop_time = datetime.datetime.now()
elapsed_time = stop_time - start_time
self.logger.info('=============================================================================================')
self.logger.info('Report for the session')
self.logger.info('Log location for this session: ' + logger_location)
self.logger.info('Job/report processed by: ' + getpass.getuser() + ' on host: ' + socket.gethostname())
self.logger.info('=============================================================================================')
print('\nDone!')
def run_from_cmd():
Excel2csv().run()
if __name__ == "__main__":
Excel2csv().run()
已生成日志文件,但未写入任何内容。
非常感谢您能帮助我解决问题,
答案 0 :(得分:0)
定义输出级别:
import logging
logging.basicConfig(level=logging.INFO,
format="[%(levelname)s] [%(asctime)s] %(message)s",
datefmt="%y-%m-%d %H:%M:%S")
使用文件时,您可以添加:
formatter = logging.Formatter('[%(levelname)s] [%(asctime)s] %(message)s',"%y-%m-%d %H:%M:%S", "%")
fh = logging.FileHandler("log")
fh.setFormatter(formatter)
logger.addHandler(fh)