py.lib.aw_log

Yohn Y. 2024-02-11 Child:9155a66edb31

0:41b53fd5637e Go to Latest

py.lib.aw_log/src/aw_log/console.py

..init

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/aw_log/console.py	Sun Feb 11 13:53:23 2024 +0300
     1.3 @@ -0,0 +1,40 @@
     1.4 +# coding: utf-8
     1.5 +# devel.a0fs.ru -- aw_log.console -- v0.r202402.1
     1.6 +
     1.7 +from typing import Any
     1.8 +from time import ctime
     1.9 +from sys import stderr, stdout
    1.10 +from typing import TextIO, Optional
    1.11 +
    1.12 +from . import AbstractLogBase
    1.13 +
    1.14 +
    1.15 +class NullLog(AbstractLogBase):
    1.16 +    def _write(self, mark: str, msg: Any):
    1.17 +        pass
    1.18 +
    1.19 +
    1.20 +class AbstractConsoleLog(AbstractLogBase):
    1.21 +    def __init__(self, prefix: str = 'main'):
    1.22 +        super().__init__(prefix)
    1.23 +        self.fd: Optional[TextIO] = None
    1.24 +
    1.25 +    def _write(self, mark: str, msg: Any):
    1.26 +        if self.fd is None:
    1.27 +            raise ValueError(f'Не определён канал логирования')
    1.28 +
    1.29 +        tm = ctime()
    1.30 +        for l in super()._write_helper(mark, msg):
    1.31 +            self.fd.write(f'{tm} | {l}')
    1.32 +
    1.33 +
    1.34 +class StdoutLog(AbstractConsoleLog):
    1.35 +    def __init__(self, prefix: str = 'main'):
    1.36 +        super().__init__(prefix)
    1.37 +        self.fd = stdout
    1.38 +
    1.39 +
    1.40 +class StderrLog(AbstractConsoleLog):
    1.41 +    def __init__(self, prefix: str = 'main'):
    1.42 +        super().__init__(prefix)
    1.43 +        self.fd = stderr