On this page
logging.FileHandler
Prev: logging.fatal | Up: logging | Next: logging.Filter
Rel: logging.addLevelName, logging.basicConfig, logging.BufferingFormatter, logging.captureWarnings
Ref: docs.python.org — logging.FileHandler
Tags: #public #python3 #stdlib #logging
Import
import logging
from logging import FileHandler
Signature & Overview
logging.FileHandler(filename, mode='a', encoding=None, delay=False, errors=None)
A handler class which writes formatted logging records to disk files.
Methods & Attributes
FileHandler.acquire(self)— Acquire the I/O thread lock.FileHandler.addFilter(self, filter)— Add the specified filter to this handler.FileHandler.close(self)— Closes the stream.FileHandler.createLock(self)— Acquire a thread lock for serializing access to the underlying I/O.FileHandler.emit(self, record)— Emit a record.FileHandler.filter(self, record)— Determine if a record is loggable by consulting all the filters.FileHandler.flush(self)— Flushes the stream.FileHandler.format(self, record)— Format the specified record.FileHandler.get_name(self)—FileHandler.get_namemethod/property.FileHandler.handle(self, record)— Conditionally emit the specified logging record.FileHandler.handleError(self, record)— Handle errors which occur during an emit() call.FileHandler.name(...)—FileHandler.namemethod/property.FileHandler.release(self)— Release the I/O thread lock.FileHandler.removeFilter(self, filter)— Remove the specified filter from this handler.FileHandler.setFormatter(self, fmt)— Set the formatter for this handler.FileHandler.setLevel(self, level)— Set the logging level of this handler. level must be an int or a str.FileHandler.setStream(self, stream)— Sets the StreamHandler's stream to the specified value, if it is different.FileHandler.set_name(self, name)—FileHandler.set_namemethod/property.
Usage
import logging
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
log_path = Path(tmp) / "app.log"
handler = logging.FileHandler(log_path, encoding="utf-8")
handler.setFormatter(logging.Formatter("%(levelname)s %(message)s"))
logger = logging.getLogger("demo.file")
logger.setLevel(logging.INFO)
logger.addHandler(handler)
try:
logger.info("started")
finally:
logger.removeHandler(handler)
handler.close()
assert "INFO started" in log_path.read_text(encoding="utf-8")
Gotchas
- No log rotation:
FileHandlergrows without bound—uselogging.handlers.RotatingFileHandlerorTimedRotatingFileHandlerin long-running services. - Always specify
encoding="utf-8"and close handlers when tearing down.
REPL Inspect
from logging import FileHandler
help(FileHandler)
Linked from
Related notes
- tuts/python3/stdlib_module/logging/fatal Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/Filter Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/logging/addLevelName Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/basicConfig Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/BufferingFormatter Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/captureWarnings Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/critical shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/debug shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/disable shared tags: #logging, #stdlib