MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / FileHandler

Class FileHandler

tools/python-3.11.9-amd64/Lib/logging/__init__.py:1152–1234  ·  view source on GitHub ↗

A handler class which writes formatted logging records to disk files.

Source from the content-addressed store, hash-verified

1150
1151
1152class FileHandler(StreamHandler):
1153 """
1154 A handler class which writes formatted logging records to disk files.
1155 """
1156 def __init__(self, filename, mode='a', encoding=None, delay=False, errors=None):
1157 """
1158 Open the specified file and use it as the stream for logging.
1159 """
1160 # Issue #27493: add support for Path objects to be passed in
1161 filename = os.fspath(filename)
1162 #keep the absolute path, otherwise derived classes which use this
1163 #may come a cropper when the current directory changes
1164 self.baseFilename = os.path.abspath(filename)
1165 self.mode = mode
1166 self.encoding = encoding
1167 if "b" not in mode:
1168 self.encoding = io.text_encoding(encoding)
1169 self.errors = errors
1170 self.delay = delay
1171 # bpo-26789: FileHandler keeps a reference to the builtin open()
1172 # function to be able to open or reopen the file during Python
1173 # finalization.
1174 self._builtin_open = open
1175 if delay:
1176 #We don't open the stream, but we still need to call the
1177 #Handler constructor to set level, formatter, lock etc.
1178 Handler.__init__(self)
1179 self.stream = None
1180 else:
1181 StreamHandler.__init__(self, self._open())
1182
1183 def close(self):
1184 """
1185 Closes the stream.
1186 """
1187 self.acquire()
1188 try:
1189 try:
1190 if self.stream:
1191 try:
1192 self.flush()
1193 finally:
1194 stream = self.stream
1195 self.stream = None
1196 if hasattr(stream, "close"):
1197 stream.close()
1198 finally:
1199 # Issue #19523: call unconditionally to
1200 # prevent a handler leak when delay is set
1201 # Also see Issue #42378: we also rely on
1202 # self._closed being set to True there
1203 StreamHandler.close(self)
1204 finally:
1205 self.release()
1206
1207 def _open(self):
1208 """
1209 Open the current base file with the (original) mode and encoding.

Callers 1

basicConfigFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected