Skip to content

Commit 898469b

Browse files
authored
Merge pull request #199 from carmocca/patch-1
Respect existing handlers
2 parents 8ae8d9f + d8fc4f1 commit 898469b

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

flogging/flogging.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,21 +277,22 @@ def ensure_utf8_stream(stream):
277277
sys.stdout, sys.stderr = (ensure_utf8_stream(s) for s in (sys.stdout, sys.stderr))
278278
np_set_string_function(repr_array)
279279

280-
# basicConfig is only called to make sure there is at least one handler for the root logger.
281-
# All the output level setting is down right afterwards.
282-
logging.basicConfig()
280+
root = logging.getLogger()
281+
if not root.handlers:
282+
# basicConfig is only called to make sure there is at least one handler for the root logger.
283+
# All the output level setting is down right afterwards.
284+
logging.basicConfig()
283285
logging.captureWarnings(capture=True)
284286
for key, val in os.environ.items():
285287
if key.startswith("flog_"):
286288
domain = key[len("flog_") :]
287289
logging.getLogger(domain).setLevel(logging._nameToLevel.get(val, level))
288-
root = logging.getLogger()
289290
root.setLevel(level)
290291

291292
if not structured:
292293
handler = root.handlers[0]
293294
# pytest injects DontReadFromInput which does not have "closed"
294-
if not getattr(sys.stdin, "closed", False) and sys.stdout.isatty():
295+
if handler.formatter is None and not getattr(sys.stdin, "closed", False) and sys.stdout.isatty():
295296
handler.setFormatter(AwesomeFormatter())
296297
else:
297298
handler = StructuredHandler(level, level_from_msg)

0 commit comments

Comments
 (0)