Skip to content

Commit 7b9ccd4

Browse files
committed
Applied minor improvement to logging preimport
1 parent 4c79ac5 commit 7b9ccd4

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

docs/pages/enhancements.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ A context manager is also available:
112112
- `renameLogger`: renames a logger from an old to a new name.
113113
- `setLogger` / `setLoggers`: sets respectively one or multiple loggers using Tinyscript's logger configuration.
114114
- `setLoggingLevel`: sets a logging level to every logger matching the given patterns.
115+
- `silentLogger` : returns the logger configured to log nothing (useful to silent the logger of a dependency).
115116
- `unsetLogger` / `unsetLoggers`: unsets respectively one or multiple loggers (removing them from the root `logging` dictionary).
116117
- `InterceptionHandler`: handler that intercepts the last log record.
117118
- `RelativeTimeColoredFormatter`: custom formatter for handling relative log times.

src/tinyscript/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.30.25
1+
1.30.26

src/tinyscript/preimports/log.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,9 @@
1414

1515

1616
PY3 = sys.version[0] == "3"
17+
logging.NONE = 1000
1718
logging.START_TIME = None
1819

19-
20-
# setup a null logger
21-
logging.nullLogger = logging.getLogger("null")
22-
logging.nullLogger.setLevel(1000)
23-
logging.nullLogger.addHandler(logging.NullHandler())
24-
25-
2620
try:
2721
logging._acquireLock
2822
except AttributeError:
@@ -249,6 +243,17 @@ def setLoggingLevel(level="INFO", *patterns, **kwargs):
249243
logging.setLoggingLevel = setLoggingLevel
250244

251245

246+
def silentLogger(name):
247+
""" Silent the input logger. """
248+
(l := logging.getLogger(name)).setLevel(logging.NONE)
249+
for h in l.handlers:
250+
l.removeHandler(h)
251+
l.addHandler(logging.NullHandler())
252+
return l
253+
logging.silentLogger = silentLogger
254+
logging.nullLogger = silentLogger("null") # setup a null logger
255+
256+
252257
def unsetLogger(name, force=False):
253258
""" Remove a logger. If the name does not exist in the dictionary of loggers, it raises an exception.
254259

tests/test_preimports_logging.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def f_with_logging(testcase, **kwargs):
1313
testcase.assertEqual(logger.name, "other")
1414

1515

16-
class LoggingInFunc(object):
16+
class LoggingInFunc:
1717
def f1(self, testcase):
1818
testcase.assertRaises(AttributeError, getattr, self, "logger")
1919

@@ -48,6 +48,9 @@ def test_manipulate_loggers(self):
4848
h = logging.StreamHandler()
4949
l.addHandler(h)
5050
self.assertIn(h, l.handlers)
51+
logging.silentLogger("test")
52+
self.assertNotIn(h, l.handlers)
53+
self.assertEqual(l.level, logging.NONE)
5154
self.assertIsNone(logging.setLoggers())
5255
self.assertIsNone(logging.setLogger("test"))
5356
self.assertNotIn(h, l.handlers)

0 commit comments

Comments
 (0)