Esempio n. 1
0
def create_logger(conf, name):
    """日志对象"""
    # 异步流
    logger = logging.getLogger(name)
    level = logging.getLevelName(conf['level'])
    logger.setLevel(level)
    handler = FluentHandler(tag=conf['tag'],
                            host=conf['host'],
                            port=conf['port'])
    handler.setFormatter(
        FluentRecordFormatter(
            fmt={
                'level': '%(levelname)s',
                'sys_host': '%(hostname)s',
                'sys_name': '%(name)s',
                'sys_module': '%(module)s',
                'function': '[%(pathname)s:%(funcName)s:%(lineno)d]',
                'stack_trace': '%(exc_text)s'
            }))
    handler.setLevel(level)
    logger.addHandler(handler)
    # 控制台
    if conf['env'] == 'dev':
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(
            logging.Formatter(
                '%(asctime)s %(levelname)s [%(pathname)s:%(funcName)s:%(lineno)d] %(message)s'
            ))
        stream_handler.setLevel(level)
        logger.addHandler(stream_handler)

    return logger
Esempio n. 2
0
    def add_handlers(self, config_path: str = None, **kwargs):
        self.logger.handlers = []

        with open(config_path) as fp:
            config = yaml.load(fp)

        for h in config['handlers']:
            cfg = config['configs'].get(h, None)
            fmt = getattr(formatter, cfg.get('formatter', 'PlainFormatter'))

            if h not in self.supported or not cfg:
                raise ValueError(
                    f'can not find configs for {h}, maybe it is not supported')

            handler = None
            if h == 'StreamHandler':
                handler = logging.StreamHandler(sys.stdout)
                handler.setFormatter(fmt(cfg['format'].format_map(kwargs)))
            elif h == 'SysLogHandler':
                if cfg['host'] and cfg['port']:
                    handler = SysLogHandlerWrapper(address=(cfg['host'],
                                                            cfg['port']))
                else:
                    # a UNIX socket is used
                    if platform.system() == 'Darwin':
                        handler = SysLogHandlerWrapper(
                            address='/var/run/syslog')
                    else:
                        handler = SysLogHandlerWrapper(address='/dev/log')
                if handler:
                    handler.ident = cfg.get('ident', '')
                    handler.setFormatter(fmt(cfg['format'].format_map(kwargs)))
            elif h == 'FileHandler':
                handler = logging.FileHandler(cfg['output'].format_map(kwargs),
                                              delay=True)
                handler.setFormatter(fmt(cfg['format'].format_map(kwargs)))
            elif h == 'FluentHandler':
                try:
                    from fluent import asynchandler as fluentasynchandler
                    from fluent.handler import FluentRecordFormatter

                    handler = fluentasynchandler.FluentHandler(
                        cfg['tag'],
                        host=cfg['host'],
                        port=cfg['port'],
                        queue_circular=True)

                    cfg['format'].update(kwargs)
                    fmt = FluentRecordFormatter(cfg['format'])
                    handler.setFormatter(fmt)
                except (ModuleNotFoundError, ImportError):
                    pass

            if handler:
                self.logger.addHandler(handler)

        verbose_level = LogVerbosity.from_string(config['level'])
        self.logger.setLevel(verbose_level.value)
def init_log():

    logging.basicConfig(level=LOG_LEVEL)
    logger = logging.getLogger(LOGNAME)

    log_handler = None

    if FLUENT_HOST and FLUENT_PORT:
        log_handler = handler.FluentHandler(
            'rssbot', host=FLUENT_HOST, port=FLUENT_PORT)
        formatter = FluentRecordFormatter(custom_format)
    else:
        log_handler= StreamHandler()

    formatter = handler.FluentRecordFormatter(custom_format)
    log_handler.setFormatter(formatter)
    logger.addHandler(log_handler)
    log = logger
Esempio n. 4
0
    def add_handlers(self, config_path: str = None, **kwargs):
        """
        Add handlers from config file.

        :param config_path: Path of config file.
        :param kwargs: Extra parameters.
        :returns: None
        """
        self.logger.handlers = []

        with open(config_path) as fp:
            config = JAML.load(fp)

        for h in config['handlers']:
            cfg = config['configs'].get(h, None)
            fmt = getattr(formatter, cfg.get('formatter', 'PlainFormatter'))

            if h not in self.supported or not cfg:
                raise ValueError(
                    f'can not find configs for {h}, maybe it is not supported')

            handler = None
            if h == 'StreamHandler':
                handler = logging.StreamHandler(sys.stdout)
                handler.setFormatter(fmt(cfg['format'].format_map(kwargs)))
            elif h == 'SysLogHandler':
                if cfg['host'] and cfg['port']:
                    handler = SysLogHandlerWrapper(address=(cfg['host'],
                                                            cfg['port']))
                else:
                    # a UNIX socket is used
                    if platform.system() == 'Darwin':
                        handler = SysLogHandlerWrapper(
                            address='/var/run/syslog')
                    else:
                        handler = SysLogHandlerWrapper(address='/dev/log')
                if handler:
                    handler.ident = cfg.get('ident', '')
                    handler.setFormatter(fmt(cfg['format'].format_map(kwargs)))

                try:
                    handler._connect_unixsocket(handler.address)
                except OSError:
                    handler = None
                    pass
            elif h == 'FileHandler':
                handler = logging.FileHandler(cfg['output'].format_map(kwargs),
                                              delay=True)
                handler.setFormatter(fmt(cfg['format'].format_map(kwargs)))
            elif h == 'FluentHandler':
                from ..importer import ImportExtensions
                with ImportExtensions(required=False, verbose=False):
                    from fluent import asynchandler as fluentasynchandler
                    from fluent.handler import FluentRecordFormatter

                    handler = fluentasynchandler.FluentHandler(
                        cfg['tag'],
                        host=cfg['host'],
                        port=cfg['port'],
                        queue_circular=True)

                    cfg['format'].update(kwargs)
                    fmt = FluentRecordFormatter(cfg['format'])
                    handler.setFormatter(fmt)

            if handler:
                self.logger.addHandler(handler)

        verbose_level = LogVerbosity.from_string(config['level'])
        if 'JINA_LOG_LEVEL' in os.environ:
            verbose_level = LogVerbosity.from_string(
                os.environ['JINA_LOG_LEVEL'])
        self.logger.setLevel(verbose_level.value)