pythonでのloggerの簡単な使用

3681 ワード

pythonでのloggerの簡単な使用
#!/usr/bin/env python # -*- coding: UTF-8 -*- import logging import sys, os import winreg key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') homePath = winreg.QueryValueEx(key, "Desktop")[0] LOGFILE = os.path.join(homePath, "AssimpModel.txt") showLevel = logging.DEBUG #         logger = logging.getLogger("logger") #   Logger      formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s") #     : #      file_handler = logging.FileHandler(LOGFILE) file_handler.setFormatter(formatter) #      consle_handler = logging.StreamHandler(sys.stdout) consle_handler.setFormatter(formatter) #           logger.setLevel(showLevel) #                     logger.addHandler(file_handler) logger.addHandler(consle_handler) 

使用方法
上のファイルをLogとして保存する.py Log.logger.info(…)