本文整理汇总了Python中formatter.Formatter.format_message方法的典型用法代码示例。如果您正苦于以下问题:Python Formatter.format_message方法的具体用法?Python Formatter.format_message怎么用?Python Formatter.format_message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类formatter.Formatter
的用法示例。
在下文中一共展示了Formatter.format_message方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Logger
# 需要导入模块: from formatter import Formatter [as 别名]
# 或者: from formatter.Formatter import format_message [as 别名]
class Logger(object):
def __init__(self, config_file):
if path.exists(config_file) and path.isfile(config_file):
conf = ConfigReader(config_file)
self.output = conf.get("output")
self.output_file = conf.get("output_file")
self.level = conf.get("level")
self.__log_levels = {
"debug": 10,
"info": 20,
"warning": 30,
"error": 40,
"critical": 50,
}
self.message_format = conf.get("message_format")
self.date_format = conf.get("date_format")
self.formatter = Formatter(
self.level, self.message_format, self.date_format)
else:
raise IOError("Can't find configuration file.")
@property
def log_levels(self):
return self.__log_levels
def set_level(self, level):
self.level = level
def log_message(self, message):
if self.output == "file":
with open(self.output_file, "a") as f:
f.write(message + "\n")
elif self.output == "stdout":
print message
else:
raise LoggerException("Can't output log information. Set file or "
"stdout in logger configuration file.")
def should_log(self, level):
return self.log_levels[level] >= self.log_levels[self.level]
def debug(self, message):
if self.should_log("debug"):
self.log_message(self.formatter.format_message(message, "debug"))
def info(self, message):
if self.should_log("info"):
self.log_message(self.formatter.format_message(message, "info"))
def warning(self, message):
if self.should_log("warning"):
self.log_message(self.formatter.format_message(message, "warning"))
def error(self, message):
if self.should_log("error"):
self.log_message(self.formatter.format_message(message, "error"))
def critical(self, message):
if self.should_log("critical"):
self.log_message(self.formatter.format_message(message,
"critical"))