Delete Set public Set private Add tags Delete tags
  Add tag   Cancel
  Delete tag   Cancel
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Login

Logging to Files/shaare/oXLUGw

  • python
  • python

Logging to Files

Basic File Logging with FileHandler

  • Use logging.FileHandler to write log records to a file.
  • mode='a' (append) preserves existing logs; mode='w' (write) overwrites on each run.
  • You can specify encoding (e.g., 'utf-8') and delay=True to open the file only on first write.

Size-Based Rotation with RotatingFileHandler

  • RotatingFileHandler rotates when the file reaches maxBytes.
  • backupCount determines how many old files to keep (.1, .2, …).
  • New rotations rename existing backups, deleting the oldest beyond backupCount.

Time-Based Rotation with TimedRotatingFileHandler

  • TimedRotatingFileHandler rotates based on elapsed time (when, interval).
  • Common when values (case insensitive): 'S', 'M', 'H', 'D', 'midnight', 'W0'-'W6'
    • 'S' – Rotate every N seconds (as given by interval), useful for very short-lived scripts or testing.
    • 'M' – Rotate every N minutes, good for high-volume services where hourly isn’t fine-grained enough.
    • 'H' – Rotate every N hours, often used for long-running daemons that batch logs hourly.
    • 'D' – Rotate every N days, for simple daily log files without tying to midnight.
    • 'midnight' – Rotate once per day exactly at midnight (local time), regardless of interval, ideal for calendar-aligned logs.
    • 'W0'–'W6' – Rotate weekly on a specific weekday, where W0 = Monday through W6 = Sunday. Use interval weeks between rotations.
  • backupCount limits number of rotated files; use .suffix to customize timestamp format.
import logging
import logging.handlers
import os
import time

def cleanup_log_files(base_name: str):
    for file_name in os.listdir("."):
        if file_name.startswith(base_name):
            os.remove(file_name)

# Basic logging with FileHandler
print("Basic logging with FileHandler")
print("-------\n")

basic_logger = logging.getLogger("file.basic")
basic_logger.setLevel(logging.DEBUG)

basic_fh = logging.FileHandler(
    "basicfile.log", delay=True, encoding="utf-8"
)
basic_fh.setLevel(logging.INFO)

basic_logger.addHandler(basic_fh)

basic_logger.info("INFO: will be written to file")

# Size-based log rotation with RotatingFileHandler
print("Size-based log rotation with RotatingFileHandler")
print("-------\n")

rotating_logs_filename = "rotatingfile.log"

cleanup_log_files(rotating_logs_filename)

rotating_logger = logging.getLogger("file.rotating")
rotating_logger.setLevel(logging.DEBUG)

rotating_fh = logging.handlers.RotatingFileHandler(
    rotating_logs_filename,
    maxBytes=500,
    backupCount=2,
    encoding="utf-8",
)
rotating_fh.setFormatter(
    logging.Formatter("%(levelname)-8s %(message)s")
)

rotating_logger.addHandler(rotating_fh)

for i in range(30):
    rotating_logger.info(f"Entry {i}: {'Z' * 50}")
    time.sleep(0.05)

# Time-based log rotation with TimedRotatingFileHandler
print("Time-based log rotation with TimedRotatingFileHandler")
print("-------\n")

timed_rotating_logs_filename = "timedrotatingfile.log"

cleanup_log_files(timed_rotating_logs_filename)

timed_rotating_logger = logging.getLogger("file.timed")
timed_rotating_logger.setLevel(logging.DEBUG)

timed_rotating_fh = logging.handlers.TimedRotatingFileHandler(
    timed_rotating_logs_filename,
    when="s",
    interval=3,
    backupCount=2,
    encoding="utf-8",
)
timed_rotating_fh.setFormatter(
    logging.Formatter("%(levelname)-8s %(message)s")
)

timed_rotating_logger.addHandler(timed_rotating_fh)

for i in range(30):
    timed_rotating_logger.info(f"Entry {i}: {'Z' * 50}")
    time.sleep(0.5)
1 month ago Permalink
cluster icon
  • Making HTTP Requests : Making HTTP Requests The requests library simplifies HTTP interactions by abstracting raw HTTP details, making it ideal for DevOps automation tasks. ...
  • Dictionaries : Dictionaries (dict) Dictionaries are mutable, insertion-ordered collections of key-value pairs. Keys must be unique and immutable; values can be of an...
  • For & While Loops : For & While Loops Python provides two main ways to repeat actions: for loops (for iterating over known sequences) and while loops (for repeating as lo...
  • Range, zip : Efficient Looping: range Creating large lists for loops is memory-intensive (e.g., list(range(1_000_000))). range() stores only start, stop, and step...
  • Handling Errors and Status Codes : Handling Errors and Status Codes HTTP status codes communicate the outcome of an API request, and handling them correctly is key to robust automation...


(97)
Filter untagged links
Fold Fold all Expand Expand all Are you sure you want to delete this link? Are you sure you want to delete this tag? The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community