Skip to content

Commit 15534fc

Browse files
authored
Merge pull request #1982 from cmu-delphi/ds/logger2
doc: add logger examples to README and format
2 parents 25ff20b + 38de491 commit 15534fc

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

_delphi_utils_python/README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,32 @@ Submodules:
1717
- `validator`: Data sanity checks and anomaly detection.
1818

1919

20-
Source code can be found here:
21-
[https://github.com/cmu-delphi/covidcast-indicators/](https://github.com/cmu-delphi/covidcast-indicators/)
20+
Source code can be found here:
21+
[https://github.com/cmu-delphi/covidcast-indicators/](https://github.com/cmu-delphi/covidcast-indicators/)
22+
23+
## Logger Usage
24+
25+
Single-thread usage.
26+
27+
```py
28+
from delphi_utils.logger import get_structured_logger
29+
30+
logger = get_structured_logger('my_logger')
31+
logger.info('Hello, world!')
32+
```
33+
34+
Multi-thread usage.
35+
36+
```py
37+
from delphi_utils.logger import get_structured_logger, pool_and_threadedlogger
38+
39+
def f(x, threaded_logger):
40+
threaded_logger.info(f'x={x}')
41+
return x*x
42+
43+
logger = get_structured_logger('my_logger')
44+
logger.info('Hello, world!')
45+
with pool_and_threadedlogger(logger, n_cpu) as (pool, threaded_logger):
46+
for i in range(10):
47+
pool.apply_async(f, args=(i, threaded_logger))
48+
```

_delphi_utils_python/delphi_utils/logger.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Structured logger utility for creating JSON logs.
22
3+
See the delphi_utils README.md for usage examples.
4+
35
The Delphi group uses two ~identical versions of this file.
46
Try to keep them in sync with edits, for sanity.
57
https://github.com/cmu-delphi/covidcast-indicators/blob/main/_delphi_utils_python/delphi_utils/logger.py
@@ -133,19 +135,17 @@ class LoggerThread():
133135
"""
134136
A construct to use a logger from multiprocessing workers/jobs.
135137
136-
the bare structlog loggers are thread-safe but not multiprocessing-safe.
137-
a `LoggerThread` will spawn a thread that listens to a mp.Queue
138-
and logs messages from it with the provided logger,
139-
so other processes can send logging messages to it
140-
via the logger-like `SubLogger` interface.
141-
the SubLogger even logs the pid of the caller.
138+
The bare structlog loggers are thread-safe but not multiprocessing-safe. A
139+
`LoggerThread` will spawn a thread that listens to a mp.Queue and logs
140+
messages from it with the provided logger, so other processes can send
141+
logging messages to it via the logger-like `SubLogger` interface. The
142+
SubLogger even logs the pid of the caller.
142143
143-
this is good to use with a set of jobs that are part of a mp.Pool,
144-
but isnt recommended for general use
145-
because of overhead from threading and multiprocessing,
146-
and because it might introduce lag to log messages.
144+
This is good to use with a set of jobs that are part of a mp.Pool, but isnt
145+
recommended for general use because of overhead from threading and
146+
multiprocessing, and because it might introduce lag to log messages.
147147
148-
somewhat inspired by:
148+
Somewhat inspired by:
149149
docs.python.org/3/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes
150150
"""
151151

@@ -236,13 +236,11 @@ def pool_and_threadedlogger(logger, *poolargs):
236236
"""
237237
Provide (to a context) a multiprocessing Pool and a proxy to the supplied logger.
238238
239-
Emulates the multiprocessing.Pool() context manager,
240-
but also provides (via a LoggerThread) a SubLogger proxy to logger
241-
that can be safely used by pool workers.
242-
The SubLogger proxy interface supports these methods: debug, info, warning, error,
243-
and critical.
244-
Also "cleans up" the pool by waiting for workers to complete
245-
as it exits the context.
239+
Emulates the multiprocessing.Pool() context manager, but also provides (via
240+
a LoggerThread) a SubLogger proxy to logger that can be safely used by pool
241+
workers. The SubLogger proxy interface supports these methods: debug, info,
242+
warning, error, and critical. Also "cleans up" the pool by waiting for
243+
workers to complete as it exits the context.
246244
"""
247245
with multiprocessing.Manager() as manager:
248246
logger_thread = LoggerThread(logger, manager.Queue())

0 commit comments

Comments
 (0)