惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
IT之家
IT之家
博客园 - 聂微东
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
小众软件
小众软件
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
量子位
月光博客
月光博客
博客园 - 【当耐特】
博客园 - 叶小钗

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Python logging trap: The Log Line That Tells You Nothing
Ben L · 2026-05-14 · via DEV Community

Ben L

The alert fires at 2am. You ssh in, open the logs, and find this:

ERROR myapp: connection pool exhausted

One line. The error itself, and nothing else.

You know what broke. You have no idea why. Was it a sudden traffic spike? A query that held a connection too long? A retry loop that ran away? The answer was in the DEBUG logs — but you turned those off six months ago because the noise was unbearable.

Then there's the other version of this problem. A teammate pings you: "hey, can you send me the error log for that failure yesterday?" You zip it up and send it over. They come back ten minutes later: "this just has the error line, there's no context here." You know they're right. You don't have anything better to send them. The conversation stalls because the information was never captured in the first place.

The logging level trap

Every Python developer eventually ends up in the same place. You start with DEBUG because you want visibility. The files balloon. Grepping through megabytes of chatter to find anything useful makes you want to quit your job. So you raise the level to WARNING, the logs go quiet, and life is good — until the next incident, when you realize you've traded noise for blindness.

The standard advice is to log more context in your error messages. So you start stuffing state into every logger.error() call. It helps, a little. But you're essentially rebuilding, by hand, the context that the preceding DEBUG logs already had — and you still can't reconstruct the sequence of events that led there.

The real problem is that log levels are a blunt instrument. You don't want DEBUG logs. You want DEBUG logs when something goes wrong.

What you actually want

Silent during normal operation. Full context the moment an error fires.

I built incident-logging (https://pypi.org/project/incident-logging/) because I kept wanting exactly this and kept hacking around the absence of it. It's a single-file Python logging handler — no dependencies — that buffers your DEBUG and INFO records silently. The moment a WARNING, ERROR, or CRITICAL is emitted, it flushes the recent buffer followed by the triggering message, then clears and starts over.

The buffer is a ring buffer. It holds the most recent N records. Old ones fall off. So you always get the context window just before the incident — not hours of irrelevant history, not nothing. And when a teammate asks for the error log, you actually have something useful to give them.

How to use it

(This is how I am using it)


import logging
from logging.handlers import RotatingFileHandler
from incident_logging import IncidentHandler

logger = logging.getLogger("myapp")
logger.setLevel(logging.DEBUG)

incident = RotatingFileHandler("incidents.log", maxBytes=1_000_000, backupCount=3)
logger.addHandler(IncidentHandler(target_handler=incident, buffer_size=50))

Enter fullscreen mode Exit fullscreen mode

Source and install instructions: https://github.com/BenLin0/EmergencyLogging