Ejemplo n.º 1
0
    async def test_logger_handlers_are_not_initialized_twice(self):
        handler = Mock(spec=AsyncStreamHandler, level=logging.DEBUG)
        with patch("aiologger.logger.AsyncStreamHandler",
                   return_value=handler) as Handler:
            formatter = Mock()
            logger = Logger.with_default_handlers(formatter=formatter)
            await asyncio.gather(
                logger.info("sardinha"),
                logger.info("tilápia"),
                logger.info("xerelete"),
                logger.error("fraldinha"),
            )

            Handler.allert_has_calls([
                call(
                    stream=self.write_pipe,
                    level=logging.DEBUG,
                    formatter=formatter,
                    filter=StdoutFilter(),
                ),
                call(
                    stream=self.write_pipe,
                    level=logging.WARNING,
                    formatter=formatter,
                ),
            ])

            await logger.shutdown()
Ejemplo n.º 2
0
    async def with_default_handlers(cls,
                                    *,
                                    name='aiologger',
                                    level=logging.NOTSET,
                                    formatter: logging.Formatter = None,
                                    loop=None):
        if formatter is None:
            formatter = logging.Formatter()

        stdout_handler = await AsyncStreamHandler.init_from_pipe(
            pipe=sys.stdout,
            level=logging.DEBUG,
            protocol_factory=AiologgerProtocol,
            formatter=formatter,
            filter=StdoutFilter(),
            loop=loop)

        stderr_handler = await AsyncStreamHandler.init_from_pipe(
            pipe=sys.stderr,
            level=logging.WARNING,
            protocol_factory=AiologgerProtocol,
            formatter=formatter,
            loop=loop)

        self = cls(name=name, level=level)

        self.addHandler(stdout_handler)
        self.addHandler(stderr_handler)

        return self
Ejemplo n.º 3
0
    def with_default_handlers(
        cls,
        *,
        name="aiologger",
        level=LogLevel.NOTSET,
        formatter: Optional[Formatter] = None,
        loop: Optional[AbstractEventLoop] = None,
        **kwargs,
    ):
        self = cls(name=name, level=level, loop=loop, **kwargs)  # type: ignore
        self.add_handler(
            AsyncStreamHandler(
                stream=sys.stdout,
                level=LogLevel.DEBUG,
                formatter=formatter,
                filter=StdoutFilter(),
                loop=loop,
            ))
        self.add_handler(
            AsyncStreamHandler(
                stream=sys.stderr,
                level=LogLevel.WARNING,
                formatter=formatter,
                loop=loop,
            ))

        return self
Ejemplo n.º 4
0
class StdoutFilterTests(unittest.TestCase):
    def setUp(self):
        self.stdout_filter = StdoutFilter()

    def test_it_filters_records_with_warning_log_level(self):
        record = Mock(levelno=LogLevel.WARNING)
        self.assertFalse(self.stdout_filter.filter(record))

    def test_it_filters_records_with_error_log_level(self):
        record = Mock(levelno=LogLevel.ERROR)
        self.assertFalse(self.stdout_filter.filter(record))

    def test_it_filters_records_with_critical_log_level(self):
        record = Mock(levelno=LogLevel.CRITICAL)
        self.assertFalse(self.stdout_filter.filter(record))

    def test_it_doesnt_filters_records_with_debug_log_level(self):
        record = Mock(levelno=LogLevel.DEBUG)
        self.assertTrue(self.stdout_filter.filter(record))

    def test_it_doesnt_filters_records_with_info_log_level(self):
        record = Mock(levelno=LogLevel.INFO)
        self.assertTrue(self.stdout_filter.filter(record))
Ejemplo n.º 5
0
    def get_logger(cls,
                   configuration: dict,
                   name: str,
                   path: str,
                   level: str = 'NOTSET'):
        logger = cls(name=name, level=level)
        if 'stream' in configuration:
            configuration_handler = configuration['stream']
            level = NAME_TO_LEVEL[configuration_handler['level']]
            formatter = cls._get_formatter(
                handler_configuration=configuration_handler)

            logger.add_handler(
                AsyncStreamHandler(
                    stream=sys.stdout,
                    level=level,
                    formatter=formatter,
                    filter=StdoutFilter(),
                ))
            logger.add_handler(
                AsyncStreamHandler(
                    stream=sys.stderr,
                    level=max(LogLevel.WARNING, level),
                    formatter=formatter,
                ))
        if 'file' in configuration:
            logger.add_handler(
                AsyncFileHandler(
                    filename=os.path.join(path, cls.LOG_FILENAME)))
        if 'smtp' in configuration:
            configuration_handler = configuration['smtp']
            logger.add_handler(
                AsyncSMTPHandler(
                    level=configuration_handler['level'],
                    sender=configuration_handler['sender'],
                    recipients=configuration_handler['recipients'],
                    subject=configuration_handler['subject'],
                    username=configuration_handler['username'],
                    password=configuration_handler['password'],
                    hostname=configuration_handler['hostname'],
                    port=configuration_handler['port'],
                    use_tls=configuration_handler['use_tls'],
                ))
        return logger
Ejemplo n.º 6
0
    async def _create_default_handlers(cls,
                                       formatter: logging.Formatter = None,
                                       loop=None) -> Iterable[logging.Handler]:

        stdout_handler = await AsyncStreamHandler.init_from_pipe(
            pipe=sys.stdout,
            level=logging.DEBUG,
            protocol_factory=AiologgerProtocol,
            formatter=formatter,
            filter=StdoutFilter(),
            loop=loop)

        stderr_handler = await AsyncStreamHandler.init_from_pipe(
            pipe=sys.stderr,
            level=logging.WARNING,
            protocol_factory=AiologgerProtocol,
            formatter=formatter,
            loop=loop)
        return [stdout_handler, stderr_handler]
Ejemplo n.º 7
0
 def with_default_handlers(
     cls,
     *,
     name: str = "dslogger",
     level: LoggingLevel = logging.NOTSET,
     formatter: OptionalLoggingFormatter = None,
     loop: OptionalAbstractEventLoop = None,
     **kwargs,
 ):
     """Create new logger."""
     self = cls(name=name, level=level, loop=loop, **kwargs)
     self._stdout_handler = DSAsyncStreamHandler(stream=sys.stdout,
                                                 level=level,
                                                 formatter=formatter,
                                                 filter=StdoutFilter())
     self._stderr_handler = DSAsyncStreamHandler(stream=sys.stderr,
                                                 level=logging.WARNING,
                                                 formatter=formatter)
     self.addHandler(self._stdout_handler)
     self.addHandler(self._stderr_handler)
     return self
Ejemplo n.º 8
0
 def setUp(self):
     self.stdout_filter = StdoutFilter()