forked from fx-kirin/py-stdlogging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlogging.py
82 lines (63 loc) · 2.65 KB
/
stdlogging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 zenbook <zenbook@zenbook-XPS>
#
# Distributed under terms of the MIT license.
"""
"""
import logging
import sys
def _validate_param(d, param_name):
if d is None:
raise ValueError(f"Expected '{param_name} param not found")
class StreamToLogger:
"""
This is adapter class from any stream-like object to logging.Logger.
"""
def __init__(self, **kwargs):
"""
It is recommended to use package-level initStream() function and not this method directly.
:param logger: Required. Standard Python's Logger or any Logger-like object.
:param stream: Required. sys.stderr, sys.stdout or any other stream-like object.
:param log_level: Optional. If not supplied, logging.DEBUG will be used.
"""
_validate_param(kwargs, 'logger')
_validate_param(kwargs, 'log_level')
_validate_param(kwargs, 'stream')
self.logger = kwargs.pop('logger')
self.log_level = kwargs.pop('log_level', logging.DEBUG)
self.stream = kwargs.pop('stream')
def write(self, lines):
if lines:
lines = lines+'\n'
for line in lines.split('\n'):
if line:
self.logger.log(self.log_level, line.rstrip())
def flush(self):
self.stream.flush()
def initStream(logger=None, logger_level=logging.ERROR, stream_getter=None, stream_setter=None):
"""
Preffered API.
stream_getter() is supplier/factory method that returns stream-like object (i.e. sys.stderr) that we're adapting upon.
It's intended usage is to supply stream-like object that we want to apapt upon.
stream_setter() is consumer method that receives wrapped object as parameter.
It's intended usage is to overwrite source stream-like, i.e. sys.stderr = s.
:param logger: Optional. If not supplied logging.getLogger('stderr') will be used.
:param logger_level: Optional. If not supplied logging.ERROR will be used.
:param stream_getter: Optional. if not supplied method that returns sys.stderr will be used.
:param stream_setter: Optional. if not supplied method that get's strema-like object and set's sys.stderr will be used.
:return:
"""
if logger is None:
logger = logging.getLogger('stderr')
if stream_getter is None:
def stream_getter():
return sys.stderr
if stream_setter is None:
def stream_setter(s):
sys.stderr = s
stream = stream_getter()
sl = StreamToLogger(logger=logger, stream=stream, logger_level=logger_level)
stream_setter(sl)