pyglove 0.5.0.dev202509240810__py3-none-any.whl → 0.5.0.dev202509250810__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pyglove might be problematic. Click here for more details.
- pyglove/core/logging.py +45 -1
- pyglove/core/logging_test.py +12 -21
- {pyglove-0.5.0.dev202509240810.dist-info → pyglove-0.5.0.dev202509250810.dist-info}/METADATA +1 -1
- {pyglove-0.5.0.dev202509240810.dist-info → pyglove-0.5.0.dev202509250810.dist-info}/RECORD +7 -7
- {pyglove-0.5.0.dev202509240810.dist-info → pyglove-0.5.0.dev202509250810.dist-info}/WHEEL +0 -0
- {pyglove-0.5.0.dev202509240810.dist-info → pyglove-0.5.0.dev202509250810.dist-info}/licenses/LICENSE +0 -0
- {pyglove-0.5.0.dev202509240810.dist-info → pyglove-0.5.0.dev202509250810.dist-info}/top_level.txt +0 -0
pyglove/core/logging.py
CHANGED
|
@@ -17,9 +17,11 @@ This module allows PyGlove to use external created logger for logging PyGlove
|
|
|
17
17
|
events without introducing library dependencies in PyGlove.
|
|
18
18
|
"""
|
|
19
19
|
|
|
20
|
+
import contextlib
|
|
20
21
|
import inspect
|
|
21
22
|
import logging
|
|
22
|
-
|
|
23
|
+
import sys
|
|
24
|
+
from typing import Any, Callable, Iterator, List, Union
|
|
23
25
|
|
|
24
26
|
|
|
25
27
|
_DEFAULT_LOGGER = logging.getLogger()
|
|
@@ -117,3 +119,45 @@ def critical(msg: str, *args, **kwargs) -> None:
|
|
|
117
119
|
**kwargs: Keyword arguments for the logger.
|
|
118
120
|
"""
|
|
119
121
|
_DEFAULT_LOGGER.critical(msg, *args, **kwargs)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def use_stream(
|
|
125
|
+
stream: Any,
|
|
126
|
+
level: int = logging.INFO,
|
|
127
|
+
name: str = 'custom',
|
|
128
|
+
fmt: str = '{levelname:8} | {asctime} | {message}',
|
|
129
|
+
datefmt: str = '%Y-%m-%d %H:%M:%S') -> logging.Logger:
|
|
130
|
+
"""Use stdout for logging."""
|
|
131
|
+
logger = logging.getLogger(name)
|
|
132
|
+
logger.setLevel(level)
|
|
133
|
+
stdout_handler = logging.StreamHandler(stream=stream)
|
|
134
|
+
stdout_handler.setLevel(level)
|
|
135
|
+
stdout_handler.setFormatter(
|
|
136
|
+
logging.Formatter(fmt=fmt, datefmt=datefmt, style='{'))
|
|
137
|
+
logger.addHandler(stdout_handler)
|
|
138
|
+
set_logger(logger)
|
|
139
|
+
return logger
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def use_stdout(
|
|
143
|
+
level: int = logging.INFO,
|
|
144
|
+
fmt: str = '{levelname:8} | {asctime} | {message}',
|
|
145
|
+
datefmt: str = '%Y-%m-%d %H:%M:%S') -> logging.Logger:
|
|
146
|
+
"""Use stdout for logging."""
|
|
147
|
+
return use_stream(sys.stdout, level, 'stdout', fmt, datefmt)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
@contextlib.contextmanager
|
|
151
|
+
def redirect_stream(
|
|
152
|
+
stream: Any,
|
|
153
|
+
level: int = logging.INFO,
|
|
154
|
+
name: str = 'custom',
|
|
155
|
+
fmt: str = '{levelname:8} | {asctime} | {message}',
|
|
156
|
+
datefmt: str = '%Y-%m-%d %H:%M:%S') -> Iterator[logging.Logger]:
|
|
157
|
+
"""Redirect the stream to the given logger."""
|
|
158
|
+
previous_logger = get_logger()
|
|
159
|
+
try:
|
|
160
|
+
logger = use_stream(stream, level, name, fmt, datefmt)
|
|
161
|
+
yield logger
|
|
162
|
+
finally:
|
|
163
|
+
set_logger(previous_logger)
|
pyglove/core/logging_test.py
CHANGED
|
@@ -21,15 +21,9 @@ class LoggingTest(unittest.TestCase):
|
|
|
21
21
|
"""Tests for pg.logging."""
|
|
22
22
|
|
|
23
23
|
def testLogging(self):
|
|
24
|
-
string_io = io.StringIO()
|
|
25
|
-
logger = logging.getLogger('logger1')
|
|
26
|
-
logger.setLevel(logging.INFO)
|
|
27
|
-
console_handler = logging.StreamHandler(stream=string_io)
|
|
28
|
-
console_handler.setLevel(logging.INFO)
|
|
29
|
-
logger.addHandler(console_handler)
|
|
30
|
-
|
|
31
24
|
self.assertIs(pg_logging.get_logger(), logging.getLogger())
|
|
32
|
-
|
|
25
|
+
string_io = io.StringIO()
|
|
26
|
+
logger = pg_logging.use_stream(string_io, name='logger1', fmt='')
|
|
33
27
|
self.assertIs(pg_logging.get_logger(), logger)
|
|
34
28
|
|
|
35
29
|
pg_logging.debug('x=%s', 1)
|
|
@@ -46,19 +40,16 @@ class LoggingTest(unittest.TestCase):
|
|
|
46
40
|
]) + '\n')
|
|
47
41
|
|
|
48
42
|
string_io = io.StringIO()
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
pg_logging.
|
|
58
|
-
|
|
59
|
-
'x=6',
|
|
60
|
-
]) + '\n')
|
|
61
|
-
|
|
43
|
+
with pg_logging.redirect_stream(
|
|
44
|
+
string_io, level=logging.DEBUG, name='logger2', fmt=''
|
|
45
|
+
):
|
|
46
|
+
pg_logging.debug('x=%s', 6)
|
|
47
|
+
self.assertEqual(string_io.getvalue(), '\n'.join([
|
|
48
|
+
'x=6',
|
|
49
|
+
]) + '\n')
|
|
50
|
+
|
|
51
|
+
pg_logging.use_stdout()
|
|
52
|
+
pg_logging.info('y=%s', 7)
|
|
62
53
|
|
|
63
54
|
if __name__ == '__main__':
|
|
64
55
|
unittest.main()
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
pyglove/__init__.py,sha256=LP1HNk_VVWMHaakX3HZ0NeZ2c4lq2uJaRbalvT8haOg,1352
|
|
2
2
|
pyglove/core/__init__.py,sha256=H4Yah3bYPrEqvuwRvhbA1MbQn7W6l2j5mmXkeAUNI30,9700
|
|
3
|
-
pyglove/core/logging.py,sha256=
|
|
4
|
-
pyglove/core/logging_test.py,sha256=
|
|
3
|
+
pyglove/core/logging.py,sha256=zTNLGnWrl6kkjjEjTphQMeNf9FfqFtl1G9VCh2LhnHg,4614
|
|
4
|
+
pyglove/core/logging_test.py,sha256=ioDbmf4S6xQXeyGWihvk6292wfYdldBw_TK1WXvIq5k,1648
|
|
5
5
|
pyglove/core/coding/__init__.py,sha256=tuHIg19ZchtkOQbdFVTVLkUpBa5f1eo66XtnKw3lcIU,1645
|
|
6
6
|
pyglove/core/coding/errors.py,sha256=aP3Y4amBzOKdlb5JnESJ3kdoijQXbiBiPDMeA88LNrk,3310
|
|
7
7
|
pyglove/core/coding/errors_test.py,sha256=fwOR8vLiRvLadubsccyE19hLHj-kThlCQt88qmUYk9M,2311
|
|
@@ -216,8 +216,8 @@ pyglove/ext/scalars/randoms.py,sha256=LkMIIx7lOq_lvJvVS3BrgWGuWl7Pi91-lA-O8x_gZs
|
|
|
216
216
|
pyglove/ext/scalars/randoms_test.py,sha256=nEhiqarg8l_5EOucp59CYrpO2uKxS1pe0hmBdZUzRNM,2000
|
|
217
217
|
pyglove/ext/scalars/step_wise.py,sha256=IDw3tuTpv0KVh7AN44W43zqm1-E0HWPUlytWOQC9w3Y,3789
|
|
218
218
|
pyglove/ext/scalars/step_wise_test.py,sha256=TL1vJ19xVx2t5HKuyIzGoogF7N3Rm8YhLE6JF7i0iy8,2540
|
|
219
|
-
pyglove-0.5.0.
|
|
220
|
-
pyglove-0.5.0.
|
|
221
|
-
pyglove-0.5.0.
|
|
222
|
-
pyglove-0.5.0.
|
|
223
|
-
pyglove-0.5.0.
|
|
219
|
+
pyglove-0.5.0.dev202509250810.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
|
|
220
|
+
pyglove-0.5.0.dev202509250810.dist-info/METADATA,sha256=-A3u9ImrpxGGoXwbhEuP4hb5beUNkO9iFaxVQ-tju1Y,7089
|
|
221
|
+
pyglove-0.5.0.dev202509250810.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
222
|
+
pyglove-0.5.0.dev202509250810.dist-info/top_level.txt,sha256=wITzJSKcj8GZUkbq-MvUQnFadkiuAv_qv5qQMw0fIow,8
|
|
223
|
+
pyglove-0.5.0.dev202509250810.dist-info/RECORD,,
|
|
File without changes
|
{pyglove-0.5.0.dev202509240810.dist-info → pyglove-0.5.0.dev202509250810.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
{pyglove-0.5.0.dev202509240810.dist-info → pyglove-0.5.0.dev202509250810.dist-info}/top_level.txt
RENAMED
|
File without changes
|