當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。