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


Python RainbowLoggingHandler.suffix方法代码示例

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


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

示例1: initialize_logger

# 需要导入模块: from rainbow_logging_handler import RainbowLoggingHandler [as 别名]
# 或者: from rainbow_logging_handler.RainbowLoggingHandler import suffix [as 别名]

#.........这里部分代码省略.........
    # setup logging directory to store log files
    if args.log_dir:

        if not os.path.isdir(args.log_dir):
            os.makedirs(args.log_dir)
        output_dir = args.log_dir

    else:
        output_dir = create_log_dir()

    LOGGER.info('Logging directory has been set to {}'.format(output_dir))


    # create optional syslog handler, if the argument has been supplied to support it
    if args.syslog:

        DESIG_SYSLOG_SERVER = syslog_server

        handler = logging.handlers.SysLogHandler(address=(DESIG_SYSLOG_SERVER,
                                                          logging.handlers.SYSLOG_UDP_PORT))
        syslog_format = logging.Formatter(
            '[appname]: %(name)s: [alias]: '+str(getpass.getuser())+' %(message)s')
        handler.setFormatter(syslog_format)

        if args.syslog_level:
            # Set syslog level to the user specified level
            if args.syslog_level == 'info':
                handler.setLevel(logging.INFO)
            elif args.syslog_level == 'warning':
                handler.setLevel(logging.WARNING)
            elif args.syslog_level == 'error':
                handler.setLevel(logging.ERROR)

        LOGGER.info(
            'Syslog has been enabled for [{}] logging level, sent to syslog server [{}]'.format(
                                                                            args.syslog_level,
                                                                            DESIG_SYSLOG_SERVER))

        # Add syslog handler to logging object
        LOGGER.addHandler(handler)

    # create error file handler and set level to error
    handler = logging.FileHandler(os.path.join(
        output_dir, "error.log"),"a", encoding=None, delay="true")
    handler.setLevel(logging.ERROR)
    handler.setFormatter(FORMATTER)
    LOGGER.addHandler(handler)

    # create debug file handler and set level to debug
    handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"a")
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(FORMATTER)
    LOGGER.addHandler(handler)

    if args.logfile_rotate:
        '''
        User has specified they would like to create a daily rotating logfile.
        Create an instance and DO NOT create a single serving logfile.
        '''
        LOGGER.info('Logfiles are now set to auto-rotate at midnight UTC.')
        filepath_and_name_format = ('{}/{}'.format(output_dir, script_name.replace('.py','')))
        LOGGER.info('Logfiles are now being written at {}'.format(filepath_and_name_format))

        log_filename=filepath_and_name_format
        # maxBytes takes the max file size in MB and bit-shift converts to bytes
        handler=SizedTimedRotatingFileHandler(
            log_filename, maxBytes=args.logsize<<20)
        handler.setLevel(logging.DEBUG)
        handler.setFormatter(FORMATTER)
        handler.suffix = "%Y-%m-%d.log"
        LOGGER.addHandler(handler)

    else:
        # create individual execution file handler and set level to debug
        now = datetime.datetime.now()
        datetime_stamp = now.strftime("%Y-%m-%d_%H-%M-%S")
        datetime_stamp = ('{}_{}.log'.format(datetime_stamp, script_name.replace('.py','')))
        filename = os.path.join(output_dir, datetime_stamp)

        handler = logging.FileHandler(filename, "a")

        if contains('DEBUG', args.log.upper()):
            handler.setLevel(logging.DEBUG)
        elif contains('WARNING', args.log.upper()):
            handler.setLevel(logging.WARNING)
        elif contains('ERROR', args.log.upper()):
            handler.setLevel(logging.ERROR)
        else:
            handler.setLevel(logging.INFO)

        handler.setFormatter(FORMATTER)
        LOGGER.addHandler(handler)

        # display the exact file name / path to the user
        LOGGER.info('Logs for this session now being written to {}'.format(filename))

        # Attach filepath / filename string to logger
        LOGGER.filename = filename

    return LOGGER
开发者ID:zerosignal0,项目名称:ezlogger,代码行数:104,代码来源:__init__.py


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