当前位置: 首页>>代码示例>>Python>>正文


Python Formatter.format方法代码示例

本文整理汇总了Python中logging.Formatter.format方法的典型用法代码示例。如果您正苦于以下问题:Python Formatter.format方法的具体用法?Python Formatter.format怎么用?Python Formatter.format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在logging.Formatter的用法示例。


在下文中一共展示了Formatter.format方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
    def format(self, record):
        if not getattr(record, 'session', None):
            record.session =  ''
            record.sessionLS = ' %s' % record.session
            record.sessionRS = '%s ' % record.session
        else:
            record.sessionLS = ' %s' % record.session
            record.sessionRS = '%s ' % record.session

        if not getattr(record, 'classname', None):
            record.classname = ''
        if not getattr(record, 'method', None):
            record.method = record.funcName
        if not getattr(record, 'classAndmethod', None):
            if record.method and record.classname:
                record.classAndMethod = '%s::%s' % (record.classname, record.method)
            else:
                record.classAndMethod = ''

        created = localtime(record.created)
        record.date = strftime('%Y-%m-%d', created)
        record.time = strftime('%H:%M:%S', created)
        record.asctimeshort = '%s %s' % (record.date, record.time)

        #return Formatter.format(self, record)

        ## splits the record and formats every line
        msg = record.getMessage()
        res = []
        for line in msg.split("\n"):
            record.msg, record.args = line, None
            res.append(Formatter.format(self, record))
        return "\n".join(res)
开发者ID:native2k,项目名称:dockstarTouchIf,代码行数:35,代码来源:Logging.py

示例2: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
 def format(self, record):
     record = deepcopy(record)
     if self.usesColor:
         color_end = '\033[00m'
         try:
             color_begin= {
                  logging.DEBUG    : '',
                  logging.INFO     : '\033[01;32m',
                  logging.WARNING  : '\033[01;33m',
                  logging.ERROR    : '\033[01;31m',
                  logging.CRITICAL : '\033[01;31m',
                  logging.FATAL    : '\033[00;31m'
                 }[record.levelno]
         except:
             color_begin = ''
             color_end = ''
             
         record.levelname = color_begin+'*'+color_end
         
         #record.levelno == logging.ERROR or 
         if record.levelno == logging.CRITICAL or record.levelno == logging.FATAL:
             record.msg = color_begin+record.msg+color_end
     if self.useIdents:
         ident = len(inspect.stack())
         if ident<self.minIdent:
             self.minIdent = ident
         record.msg=' '*(ident-self.minIdent)+record.msg
     return Formatter.format(self, record)
开发者ID:wdanilo,项目名称:dotfiles,代码行数:30,代码来源:colorFormatter.py

示例3: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
    def format(self, record):
        try:
            msg = super(ANSIFormatter, self).format(record)
        except:
            # for python2.6
            # Formatter is old-style class in python2.6 and type is classobj
            # another trick: http://stackoverflow.com/a/18392639/1276501
            msg = Formatter.format(self, record)

        lvl2color = {
            "DEBUG": "blue",
            "INFO": "green",
            "WARNING": "yellow",
            "ERROR": "red",
            "CRITICAL": "bgred"
        }

        rln = record.levelname
        if rln in lvl2color:
            return "[{0}]: {1}".format(
                utils.color_msg(lvl2color[rln], rln),
                msg
            )
        else:
            return msg
开发者ID:the7day,项目名称:simiki,代码行数:27,代码来源:log.py

示例4: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
    def format (self, record):
        # Add the color codes to the record
        record.__dict__.update(escape_codes)

        # If we recognise the level name,
        # add the levels color as `log_color`
        if record.levelname in self.log_colors:
            color = self.log_colors[record.levelname]
            record.log_color = escape_codes[color]
        else:
            record.log_color = ""

        #NOTE: caller_name is very slow
        #record.__dict__['caller_name'] = caller_name(9)
        record.__dict__['abbr_levelname'] = record.levelname[0]
        record.__dict__['pathname2'] = '.'.join(record.pathname.split('/')[-2:]).rstrip('.py')

        # Format the message
        if version_info > (2, 7):
            message = super(ColoredFormatter, self).format(record)
        else:
            message = Formatter.format(self, record)

        # Add a reset code to the end of the message (if it wasn't explicitly added in format str)
        if self.reset and not message.endswith(escape_codes['reset']):
            message += escape_codes['reset']

        return message
开发者ID:justinleoye,项目名称:crawler-group,代码行数:30,代码来源:log_utils.py

示例5: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
 def format(self, record):
     plain_result = Formatter.format(self, record)
     for severity_threshold, severity_color in self.SEVERITY_COLOR_THRESHOLDS:
         if record.levelno >= severity_threshold:
             color = severity_color
         else:
             break
     return colorize(plain_result, color=color)
开发者ID:yaniv-aknin,项目名称:yalib,代码行数:10,代码来源:__init__.py

示例6: QTextHandler

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
class QTextHandler(QtGui.QTextEdit):
    def __init__(self, parent=None):
        super(QTextHandler, self).__init__(parent)

    def setFormatter(self, fmt=None):
        self.formatter = Formatter(fmt)

    def handle(self, record):
        self.append(self.formatter.format(record))
开发者ID:buendiya,项目名称:wavegen,代码行数:11,代码来源:qtexthandler.py

示例7: CustomSMTPHandler

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
class CustomSMTPHandler(SMTPHandler):
    def __init__(self, mailhost, fromaddr, toaddrs, subject):
        super(CustomSMTPHandler, self).__init__(
            mailhost, fromaddr, toaddrs, subject
        )
        self.subject_formatter = Formatter(subject)

    def getSubject(self, record):  # noqa: N802
        return self.subject_formatter.format(record).replace('\n', '')
开发者ID:adamcik,项目名称:oauthclientbridge,代码行数:11,代码来源:logging.py

示例8: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
 def format(self, record):
     levelname = record.levelname
     msg = Formatter.format(self, record)
     if levelname in COLORS:
         if stdout.isatty():
             msg = COLORS[levelname] % msg
     else:
         print ("*" * 100, levelname, "(%s)" % type(levelname), "not in",
                 COLORS.keys())
     return msg
开发者ID:silpol,项目名称:tryton-bef,代码行数:12,代码来源:log.py

示例9: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
    def format(self, record):
        try:
            msg = super(NonANSIFormatter, self).format(record)
        except:
            # for python2.6
            # Formatter is old-style class in python2.6 and type is classobj
            # another trick: http://stackoverflow.com/a/18392639/1276501
            msg = Formatter.format(self, record)

        rln = record.levelname
        return "[{0}]: {1}".format(rln, msg)
开发者ID:JamesPan,项目名称:simiki,代码行数:13,代码来源:log.py

示例10: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
 def format(self, record):
     rv = []
     length = 0
     for piece in _ws_re.split(Formatter.format(self, record)):
         length += len(piece)
         if length > 140:
             if length - len(piece) < 140:
                 rv.append(u'…')
             break
         rv.append(piece)
     return u''.join(rv)
开发者ID:hytc1106hwc,项目名称:twitterlog,代码行数:13,代码来源:twitterlog.py

示例11: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
 def format(self, record):
     """Format the record with colors."""
     color = self.color_seq % (30 + self.colors[record.levelname])
     message = Formatter.format(self, record)
     message = message.replace('$RESET', self.reset_seq)\
         .replace('$BOLD', self.bold_seq)\
         .replace('$COLOR', color)
     for color, value in self.colors.items():
         message = message.replace(
             '$' + color, self.color_seq % (value + 30))\
             .replace('$BG' + color, self.color_seq % (value + 40))\
             .replace('$BG-' + color, self.color_seq % (value + 40))
     return message + self.reset_seq
开发者ID:AlexTalker,项目名称:qtile,代码行数:15,代码来源:log_utils.py

示例12: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
    def format(self, record):
        colored_record = copy(record)

        # Add colors to levelname
        levelname = colored_record.levelname
        color = MAPPING.get(levelname, 'white')
        colored_record.levelname = COLORIZE(levelname, color)

        # Add colors to tagged message text
        msg = colored_record.getMessage()
        plain_msg, color_msg = self.color_words(msg)
        record.msg = plain_msg
        colored_record.msg = color_msg

        return Formatter.format(self, colored_record)
开发者ID:LinuxCNC,项目名称:linuxcnc,代码行数:17,代码来源:colored_formatter.py

示例13: SysLogHandler

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
class SysLogHandler(_SysLogHandler):
    """ 扩展标准库的SysLogHandler,使用loggername作为tag """

    def __init__(self, tag=None, withpid=False, **kwargs):
        super(SysLogHandler, self).__init__(**kwargs)
        # 产生tag的formatter
        fmt = tag or "%(name)s"
        if withpid:
            fmt += "[%(process)d]"
        self.tag_formatter = Formatter(fmt)

    def format(self, record):
        msg = super(SysLogHandler, self).format(record)
        tag = self.tag_formatter.format(record)
        return "%s: %s" % (tag, msg)
开发者ID:graycarl,项目名称:moon,代码行数:17,代码来源:logging.py

示例14: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
    def format(self, record):
        try:
            message = super(SecureFormatter, self).format(record)
        except TypeError:
            # In pyhton 2.6 the Formatter does not seem to 
            # be defined as 
            # class Formatter(object)
            # Using it in the super-statement this will raise a TypeError
            message = Formatter.format(self, record)
        secured = False

        s = ""
        for c in message:
            if c in string.printable:
                s += c
            else:
                s += '.'
                secured = True

        if secured:
            s = "!!!Log Entry Secured by SecureFormatter!!! " + s

        return s
开发者ID:hrz-unimr,项目名称:privacyidea,代码行数:25,代码来源:log.py

示例15: format

# 需要导入模块: from logging import Formatter [as 别名]
# 或者: from logging.Formatter import format [as 别名]
	def format (self, record):
		# Add the color codes to the record
		record.__dict__.update(escape_codes)

		# If we recognise the level name,
		# add the levels color as `log_color`
		if record.levelname in self.log_colors:
			color = self.log_colors[record.levelname]
			record.log_color = escape_codes[color]
		else:
			record.log_color = ""

		# Format the message
		if version_info > (2, 7):
			message = super(ColoredFormatter, self).format(record)
		else:
			message = Formatter.format(self, record)

		# Add a reset code to the end of the message (if it wasn't explicitly added in format str)
		if self.reset and not message.endswith(escape_codes['reset']):
			message += escape_codes['reset']

		return message
开发者ID:EdhecRiskInstitute,项目名称:colorlog,代码行数:25,代码来源:colorlog.py


注:本文中的logging.Formatter.format方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。