emergency-logging 0.1.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 BenLin0
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,118 @@
1
+ Metadata-Version: 2.4
2
+ Name: emergency-logging
3
+ Version: 0.1.0
4
+ Summary: A Python logging handler that buffers DEBUG/INFO and flushes them only when WARNING or ERROR fires
5
+ License-Expression: MIT
6
+ Project-URL: Homepage, https://github.com/BenLin0/EmergencyLogging
7
+ Keywords: logging,handler,debug,buffer
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Topic :: System :: Logging
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+ # EmergencyLogging
17
+
18
+ A Python logging handler that stays silent during normal operation and only writes logs when something goes wrong.
19
+
20
+ ## The problem
21
+
22
+ Verbose debug logging helps diagnose issues, but writing every `DEBUG` and `INFO` message to a file or console creates noise that obscures what matters. The usual workaround — raising the log level to `WARNING` — means you lose the context that would have explained *why* the warning happened.
23
+
24
+ ## How it works
25
+
26
+ `EmergencyHandler` wraps any standard `logging.Handler`. It buffers `DEBUG` and `INFO` records silently. The moment a `WARNING`, `ERROR`, or `CRITICAL` is emitted, it flushes the buffered context followed by the triggering message — then clears the buffer and starts over.
27
+
28
+ ```
29
+ Normal operation: DEBUG INFO DEBUG INFO DEBUG INFO → (nothing written)
30
+ Something goes wrong: DEBUG INFO DEBUG INFO ERROR → DEBUG INFO DEBUG INFO ERROR
31
+ ```
32
+
33
+ The buffer holds the **most recent** N records (default 30). Older records are dropped as new ones arrive, so the buffer always contains the last N lines of context leading up to the problem.
34
+
35
+ ## Installation
36
+
37
+ ### Via pip (recommended)
38
+
39
+ ```bash
40
+ pip install emergency-logging
41
+ ```
42
+
43
+ ### Manual
44
+
45
+ No dependencies outside the standard library. Copy `emergency_logging.py` directly into your project.
46
+
47
+ Requires Python 3.8+.
48
+
49
+ ## Usage
50
+
51
+ ### Basic — wrap the default stderr handler
52
+
53
+ ```python
54
+ import logging
55
+ from emergency_logging import EmergencyHandler
56
+
57
+ logger = logging.getLogger("myapp")
58
+ logger.setLevel(logging.DEBUG)
59
+ logger.addHandler(EmergencyHandler())
60
+
61
+ logger.debug("connecting to database") # buffered
62
+ logger.info("query executed in 4 ms") # buffered
63
+ logger.error("connection pool exhausted") # flushes both lines above, then this
64
+ ```
65
+
66
+ ### With a custom handler and buffer size
67
+
68
+ ```python
69
+ import logging
70
+ from emergency_logging import EmergencyHandler
71
+
72
+ stream = logging.StreamHandler()
73
+ stream.setFormatter(logging.Formatter("%(levelname)s %(name)s: %(message)s"))
74
+
75
+ logger = logging.getLogger("myapp")
76
+ logger.setLevel(logging.DEBUG)
77
+ logger.addHandler(EmergencyHandler(target_handler=stream, buffer_size=50))
78
+ ```
79
+
80
+ ### With RotatingFileHandler
81
+
82
+ The log file stays empty during normal operation and only grows when an incident occurs — keeping file sizes minimal while preserving full diagnostic context when you need it.
83
+
84
+ ```python
85
+ import logging
86
+ from logging.handlers import RotatingFileHandler
87
+ from emergency_logging import EmergencyHandler
88
+
89
+ rotating = RotatingFileHandler("app.log", maxBytes=1024 * 1024, backupCount=5)
90
+ rotating.setFormatter(logging.Formatter("%(asctime)s %(levelname)-8s %(name)s: %(message)s"))
91
+
92
+ logger = logging.getLogger("myapp")
93
+ logger.setLevel(logging.DEBUG)
94
+ logger.addHandler(EmergencyHandler(target_handler=rotating, buffer_size=30))
95
+ ```
96
+
97
+ ## API
98
+
99
+ ### `EmergencyHandler(target_handler=None, buffer_size=30)`
100
+
101
+ | Parameter | Type | Default | Description |
102
+ |---|---|---|---|
103
+ | `target_handler` | `logging.Handler` | `StreamHandler()` | The handler that receives flushed records |
104
+ | `buffer_size` | `int` | `30` | Maximum number of `DEBUG`/`INFO` records to buffer; oldest are dropped when exceeded |
105
+
106
+ The handler passes through `WARNING`, `ERROR`, and `CRITICAL` records immediately (after flushing the buffer). `DEBUG` and `INFO` records are only ever written as part of a flush.
107
+
108
+ ## Running the demos
109
+
110
+ ```bash
111
+ python3 demo.py
112
+ ```
113
+
114
+ ## Running the tests
115
+
116
+ ```bash
117
+ python3 -m unittest test_emergency_logging -v
118
+ ```
@@ -0,0 +1,103 @@
1
+ # EmergencyLogging
2
+
3
+ A Python logging handler that stays silent during normal operation and only writes logs when something goes wrong.
4
+
5
+ ## The problem
6
+
7
+ Verbose debug logging helps diagnose issues, but writing every `DEBUG` and `INFO` message to a file or console creates noise that obscures what matters. The usual workaround — raising the log level to `WARNING` — means you lose the context that would have explained *why* the warning happened.
8
+
9
+ ## How it works
10
+
11
+ `EmergencyHandler` wraps any standard `logging.Handler`. It buffers `DEBUG` and `INFO` records silently. The moment a `WARNING`, `ERROR`, or `CRITICAL` is emitted, it flushes the buffered context followed by the triggering message — then clears the buffer and starts over.
12
+
13
+ ```
14
+ Normal operation: DEBUG INFO DEBUG INFO DEBUG INFO → (nothing written)
15
+ Something goes wrong: DEBUG INFO DEBUG INFO ERROR → DEBUG INFO DEBUG INFO ERROR
16
+ ```
17
+
18
+ The buffer holds the **most recent** N records (default 30). Older records are dropped as new ones arrive, so the buffer always contains the last N lines of context leading up to the problem.
19
+
20
+ ## Installation
21
+
22
+ ### Via pip (recommended)
23
+
24
+ ```bash
25
+ pip install emergency-logging
26
+ ```
27
+
28
+ ### Manual
29
+
30
+ No dependencies outside the standard library. Copy `emergency_logging.py` directly into your project.
31
+
32
+ Requires Python 3.8+.
33
+
34
+ ## Usage
35
+
36
+ ### Basic — wrap the default stderr handler
37
+
38
+ ```python
39
+ import logging
40
+ from emergency_logging import EmergencyHandler
41
+
42
+ logger = logging.getLogger("myapp")
43
+ logger.setLevel(logging.DEBUG)
44
+ logger.addHandler(EmergencyHandler())
45
+
46
+ logger.debug("connecting to database") # buffered
47
+ logger.info("query executed in 4 ms") # buffered
48
+ logger.error("connection pool exhausted") # flushes both lines above, then this
49
+ ```
50
+
51
+ ### With a custom handler and buffer size
52
+
53
+ ```python
54
+ import logging
55
+ from emergency_logging import EmergencyHandler
56
+
57
+ stream = logging.StreamHandler()
58
+ stream.setFormatter(logging.Formatter("%(levelname)s %(name)s: %(message)s"))
59
+
60
+ logger = logging.getLogger("myapp")
61
+ logger.setLevel(logging.DEBUG)
62
+ logger.addHandler(EmergencyHandler(target_handler=stream, buffer_size=50))
63
+ ```
64
+
65
+ ### With RotatingFileHandler
66
+
67
+ The log file stays empty during normal operation and only grows when an incident occurs — keeping file sizes minimal while preserving full diagnostic context when you need it.
68
+
69
+ ```python
70
+ import logging
71
+ from logging.handlers import RotatingFileHandler
72
+ from emergency_logging import EmergencyHandler
73
+
74
+ rotating = RotatingFileHandler("app.log", maxBytes=1024 * 1024, backupCount=5)
75
+ rotating.setFormatter(logging.Formatter("%(asctime)s %(levelname)-8s %(name)s: %(message)s"))
76
+
77
+ logger = logging.getLogger("myapp")
78
+ logger.setLevel(logging.DEBUG)
79
+ logger.addHandler(EmergencyHandler(target_handler=rotating, buffer_size=30))
80
+ ```
81
+
82
+ ## API
83
+
84
+ ### `EmergencyHandler(target_handler=None, buffer_size=30)`
85
+
86
+ | Parameter | Type | Default | Description |
87
+ |---|---|---|---|
88
+ | `target_handler` | `logging.Handler` | `StreamHandler()` | The handler that receives flushed records |
89
+ | `buffer_size` | `int` | `30` | Maximum number of `DEBUG`/`INFO` records to buffer; oldest are dropped when exceeded |
90
+
91
+ The handler passes through `WARNING`, `ERROR`, and `CRITICAL` records immediately (after flushing the buffer). `DEBUG` and `INFO` records are only ever written as part of a flush.
92
+
93
+ ## Running the demos
94
+
95
+ ```bash
96
+ python3 demo.py
97
+ ```
98
+
99
+ ## Running the tests
100
+
101
+ ```bash
102
+ python3 -m unittest test_emergency_logging -v
103
+ ```
@@ -0,0 +1,118 @@
1
+ Metadata-Version: 2.4
2
+ Name: emergency-logging
3
+ Version: 0.1.0
4
+ Summary: A Python logging handler that buffers DEBUG/INFO and flushes them only when WARNING or ERROR fires
5
+ License-Expression: MIT
6
+ Project-URL: Homepage, https://github.com/BenLin0/EmergencyLogging
7
+ Keywords: logging,handler,debug,buffer
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Topic :: System :: Logging
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+ # EmergencyLogging
17
+
18
+ A Python logging handler that stays silent during normal operation and only writes logs when something goes wrong.
19
+
20
+ ## The problem
21
+
22
+ Verbose debug logging helps diagnose issues, but writing every `DEBUG` and `INFO` message to a file or console creates noise that obscures what matters. The usual workaround — raising the log level to `WARNING` — means you lose the context that would have explained *why* the warning happened.
23
+
24
+ ## How it works
25
+
26
+ `EmergencyHandler` wraps any standard `logging.Handler`. It buffers `DEBUG` and `INFO` records silently. The moment a `WARNING`, `ERROR`, or `CRITICAL` is emitted, it flushes the buffered context followed by the triggering message — then clears the buffer and starts over.
27
+
28
+ ```
29
+ Normal operation: DEBUG INFO DEBUG INFO DEBUG INFO → (nothing written)
30
+ Something goes wrong: DEBUG INFO DEBUG INFO ERROR → DEBUG INFO DEBUG INFO ERROR
31
+ ```
32
+
33
+ The buffer holds the **most recent** N records (default 30). Older records are dropped as new ones arrive, so the buffer always contains the last N lines of context leading up to the problem.
34
+
35
+ ## Installation
36
+
37
+ ### Via pip (recommended)
38
+
39
+ ```bash
40
+ pip install emergency-logging
41
+ ```
42
+
43
+ ### Manual
44
+
45
+ No dependencies outside the standard library. Copy `emergency_logging.py` directly into your project.
46
+
47
+ Requires Python 3.8+.
48
+
49
+ ## Usage
50
+
51
+ ### Basic — wrap the default stderr handler
52
+
53
+ ```python
54
+ import logging
55
+ from emergency_logging import EmergencyHandler
56
+
57
+ logger = logging.getLogger("myapp")
58
+ logger.setLevel(logging.DEBUG)
59
+ logger.addHandler(EmergencyHandler())
60
+
61
+ logger.debug("connecting to database") # buffered
62
+ logger.info("query executed in 4 ms") # buffered
63
+ logger.error("connection pool exhausted") # flushes both lines above, then this
64
+ ```
65
+
66
+ ### With a custom handler and buffer size
67
+
68
+ ```python
69
+ import logging
70
+ from emergency_logging import EmergencyHandler
71
+
72
+ stream = logging.StreamHandler()
73
+ stream.setFormatter(logging.Formatter("%(levelname)s %(name)s: %(message)s"))
74
+
75
+ logger = logging.getLogger("myapp")
76
+ logger.setLevel(logging.DEBUG)
77
+ logger.addHandler(EmergencyHandler(target_handler=stream, buffer_size=50))
78
+ ```
79
+
80
+ ### With RotatingFileHandler
81
+
82
+ The log file stays empty during normal operation and only grows when an incident occurs — keeping file sizes minimal while preserving full diagnostic context when you need it.
83
+
84
+ ```python
85
+ import logging
86
+ from logging.handlers import RotatingFileHandler
87
+ from emergency_logging import EmergencyHandler
88
+
89
+ rotating = RotatingFileHandler("app.log", maxBytes=1024 * 1024, backupCount=5)
90
+ rotating.setFormatter(logging.Formatter("%(asctime)s %(levelname)-8s %(name)s: %(message)s"))
91
+
92
+ logger = logging.getLogger("myapp")
93
+ logger.setLevel(logging.DEBUG)
94
+ logger.addHandler(EmergencyHandler(target_handler=rotating, buffer_size=30))
95
+ ```
96
+
97
+ ## API
98
+
99
+ ### `EmergencyHandler(target_handler=None, buffer_size=30)`
100
+
101
+ | Parameter | Type | Default | Description |
102
+ |---|---|---|---|
103
+ | `target_handler` | `logging.Handler` | `StreamHandler()` | The handler that receives flushed records |
104
+ | `buffer_size` | `int` | `30` | Maximum number of `DEBUG`/`INFO` records to buffer; oldest are dropped when exceeded |
105
+
106
+ The handler passes through `WARNING`, `ERROR`, and `CRITICAL` records immediately (after flushing the buffer). `DEBUG` and `INFO` records are only ever written as part of a flush.
107
+
108
+ ## Running the demos
109
+
110
+ ```bash
111
+ python3 demo.py
112
+ ```
113
+
114
+ ## Running the tests
115
+
116
+ ```bash
117
+ python3 -m unittest test_emergency_logging -v
118
+ ```
@@ -0,0 +1,8 @@
1
+ LICENSE
2
+ README.md
3
+ emergency_logging.py
4
+ pyproject.toml
5
+ emergency_logging.egg-info/PKG-INFO
6
+ emergency_logging.egg-info/SOURCES.txt
7
+ emergency_logging.egg-info/dependency_links.txt
8
+ emergency_logging.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ emergency_logging
@@ -0,0 +1,31 @@
1
+ import logging
2
+ import collections
3
+
4
+
5
+ class EmergencyHandler(logging.Handler):
6
+ """
7
+ Buffers DEBUG/INFO records and flushes them only when WARN or ERROR is emitted.
8
+ Keeps the last `buffer_size` buffered records (oldest are dropped when full).
9
+ """
10
+
11
+ def __init__(self, target_handler=None, buffer_size=30):
12
+ super().__init__()
13
+ self.target_handler = target_handler or logging.StreamHandler()
14
+ self.buffer_size = buffer_size
15
+ self._buffer = collections.deque(maxlen=buffer_size)
16
+
17
+ def emit(self, record):
18
+ if record.levelno >= logging.WARNING:
19
+ for buffered in self._buffer:
20
+ self.target_handler.emit(buffered)
21
+ self._buffer.clear()
22
+ self.target_handler.emit(record)
23
+ else:
24
+ self._buffer.append(record)
25
+
26
+ def flush(self):
27
+ self.target_handler.flush()
28
+
29
+ def close(self):
30
+ self.target_handler.close()
31
+ super().close()
@@ -0,0 +1,23 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "emergency-logging"
7
+ version = "0.1.0"
8
+ description = "A Python logging handler that buffers DEBUG/INFO and flushes them only when WARNING or ERROR fires"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ requires-python = ">=3.8"
12
+ keywords = ["logging", "handler", "debug", "buffer"]
13
+ classifiers = [
14
+ "Programming Language :: Python :: 3",
15
+ "Operating System :: OS Independent",
16
+ "Topic :: System :: Logging",
17
+ ]
18
+
19
+ [tool.setuptools]
20
+ py-modules = ["emergency_logging"]
21
+
22
+ [project.urls]
23
+ Homepage = "https://github.com/BenLin0/EmergencyLogging"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+