|
1 | | -# Copyright 2001-2015 by Vinay Sajip. All Rights Reserved. |
| 1 | +# Copyright 2001-2016 by Vinay Sajip. All Rights Reserved. |
2 | 2 | # |
3 | 3 | # Permission to use, copy, modify, and distribute this software and its |
4 | 4 | # documentation for any purpose and without fee is hereby granted, |
|
18 | 18 | Additional handlers for the logging package for Python. The core package is |
19 | 19 | based on PEP 282 and comments thereto in comp.lang.python. |
20 | 20 |
|
21 | | -Copyright (C) 2001-2015 Vinay Sajip. All Rights Reserved. |
| 21 | +Copyright (C) 2001-2016 Vinay Sajip. All Rights Reserved. |
22 | 22 |
|
23 | 23 | To use, simply 'import logging.handlers' and log away! |
24 | 24 | """ |
@@ -1238,17 +1238,25 @@ class MemoryHandler(BufferingHandler): |
1238 | 1238 | flushing them to a target handler. Flushing occurs whenever the buffer |
1239 | 1239 | is full, or when an event of a certain severity or greater is seen. |
1240 | 1240 | """ |
1241 | | - def __init__(self, capacity, flushLevel=logging.ERROR, target=None): |
| 1241 | + def __init__(self, capacity, flushLevel=logging.ERROR, target=None, |
| 1242 | + flushOnClose=True): |
1242 | 1243 | """ |
1243 | 1244 | Initialize the handler with the buffer size, the level at which |
1244 | 1245 | flushing should occur and an optional target. |
1245 | 1246 |
|
1246 | 1247 | Note that without a target being set either here or via setTarget(), |
1247 | 1248 | a MemoryHandler is no use to anyone! |
| 1249 | +
|
| 1250 | + The ``flushOnClose`` argument is ``True`` for backward compatibility |
| 1251 | + reasons - the old behaviour is that when the handler is closed, the |
| 1252 | + buffer is flushed, even if the flush level hasn't been exceeded nor the |
| 1253 | + capacity exceeded. To prevent this, set ``flushOnClose`` to ``False``. |
1248 | 1254 | """ |
1249 | 1255 | BufferingHandler.__init__(self, capacity) |
1250 | 1256 | self.flushLevel = flushLevel |
1251 | 1257 | self.target = target |
| 1258 | + # See Issue #26559 for why this has been added |
| 1259 | + self.flushOnClose = flushOnClose |
1252 | 1260 |
|
1253 | 1261 | def shouldFlush(self, record): |
1254 | 1262 | """ |
@@ -1282,10 +1290,12 @@ def flush(self): |
1282 | 1290 |
|
1283 | 1291 | def close(self): |
1284 | 1292 | """ |
1285 | | - Flush, set the target to None and lose the buffer. |
| 1293 | + Flush, if appropriately configured, set the target to None and lose the |
| 1294 | + buffer. |
1286 | 1295 | """ |
1287 | 1296 | try: |
1288 | | - self.flush() |
| 1297 | + if self.flushOnClose: |
| 1298 | + self.flush() |
1289 | 1299 | finally: |
1290 | 1300 | self.acquire() |
1291 | 1301 | try: |
|
0 commit comments