A handler class which writes formatted logging records to disk files.
| 1191 | |
| 1192 | |
| 1193 | class FileHandler(StreamHandler): |
| 1194 | """ |
| 1195 | A handler class which writes formatted logging records to disk files. |
| 1196 | """ |
| 1197 | def __init__(self, filename, mode='a', encoding=None, delay=False, errors=None): |
| 1198 | """ |
| 1199 | Open the specified file and use it as the stream for logging. |
| 1200 | """ |
| 1201 | # Issue #27493: add support for Path objects to be passed in |
| 1202 | filename = os.fspath(filename) |
| 1203 | #keep the absolute path, otherwise derived classes which use this |
| 1204 | #may come a cropper when the current directory changes |
| 1205 | self.baseFilename = os.path.abspath(filename) |
| 1206 | self.mode = mode |
| 1207 | self.encoding = encoding |
| 1208 | if "b" not in mode: |
| 1209 | self.encoding = io.text_encoding(encoding) |
| 1210 | self.errors = errors |
| 1211 | self.delay = delay |
| 1212 | # bpo-26789: FileHandler keeps a reference to the builtin open() |
| 1213 | # function to be able to open or reopen the file during Python |
| 1214 | # finalization. |
| 1215 | self._builtin_open = open |
| 1216 | if delay: |
| 1217 | #We don't open the stream, but we still need to call the |
| 1218 | #Handler constructor to set level, formatter, lock etc. |
| 1219 | Handler.__init__(self) |
| 1220 | self.stream = None |
| 1221 | else: |
| 1222 | StreamHandler.__init__(self, self._open()) |
| 1223 | |
| 1224 | def close(self): |
| 1225 | """ |
| 1226 | Closes the stream. |
| 1227 | """ |
| 1228 | with self.lock: |
| 1229 | try: |
| 1230 | if self.stream: |
| 1231 | try: |
| 1232 | self.flush() |
| 1233 | finally: |
| 1234 | stream = self.stream |
| 1235 | self.stream = None |
| 1236 | if hasattr(stream, "close"): |
| 1237 | stream.close() |
| 1238 | finally: |
| 1239 | # Issue #19523: call unconditionally to |
| 1240 | # prevent a handler leak when delay is set |
| 1241 | # Also see Issue #42378: we also rely on |
| 1242 | # self._closed being set to True there |
| 1243 | StreamHandler.close(self) |
| 1244 | |
| 1245 | def _open(self): |
| 1246 | """ |
| 1247 | Open the current base file with the (original) mode and encoding. |
| 1248 | Return the resulting stream. |
| 1249 | """ |
| 1250 | open_func = self._builtin_open |
no outgoing calls
no test coverage detected
searching dependent graphs…