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

推荐订阅源

L
LangChain Blog
Security Latest
Security Latest
P
Proofpoint News Feed
GbyAI
GbyAI
PCI Perspectives
PCI Perspectives
博客园 - Franky
N
Netflix TechBlog - Medium
博客园_首页
WordPress大学
WordPress大学
K
Kaspersky official blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
T
Threatpost
The Hacker News
The Hacker News
H
Help Net Security
S
Securelist
Recent Announcements
Recent Announcements
腾讯CDC
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
C
Cisco Blogs
V
V2EX
C
Check Point Blog
S
Schneier on Security
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
博客园 - 叶小钗
A
Arctic Wolf
AWS News Blog
AWS News Blog
Latest news
Latest news
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
The GitHub Blog
The GitHub Blog
小众软件
小众软件
B
Blog
aimingoo的专栏
aimingoo的专栏
C
Cyber Attacks, Cyber Crime and Cyber Security
V
Visual Studio Blog
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy

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 Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything BFF模式详解:构建前后端协同的中间层 I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
🐍 Flask Python Structured Logging — What Most Miss in Production
Python-T Poi · 2026-05-24 · via DEV Community

Roughly 80% of Flask applications still rely on basic print() statements or unstructured logging.info() calls for observability in production. Despite widespread adoption of modern monitoring tools like Datadog, Loki, and Elasticsearch, most Python web apps ship logs as plain text — making debugging slow, filtering unreliable, and alerting brittle. This isn’t a legacy issue; it’s happening in brand-new Flask services today.

📑 Table of Contents

  • ⚙️ Built-in Logging — Why Structure Matters
  • 🐍 Loguru — Simpler, More Expressive Setup
  • 🧠 Context Propagation — Keeping Data Across Functions
  • 🔧 Handling Exceptions — Auto-JSON Tracebacks
  • 📦 Flask Integration — Seamless Middleware Injection
  • 💡 Filtering Noise — Exclude Health Checks
  • 🔐 Security — Avoid Logging Sensitive Data
  • 🔍 Production Best Practices — Making Logs Actionable
  • 📦 Deployment — Logging in Docker & Kubernetes
  • 📉 Monitoring — Querying Structured Logs
  • 🟩 Final Thoughts
  • ❓ Frequently Asked Questions
  • Can I use both Python logging and Loguru in the same app?
  • How do I rotate JSON log files in production?
  • Are JSON logs slower than plain text?
  • 📚 References & Further Reading

⚙️ Built-in Logging — Why Structure Matters

The Python logging module is not a thin wrapper around print() — it’s a fully composable system for routing, formatting, and filtering log records based on severity, source, and custom context. Every log call (e.g., logger.info("User logged in")) creates a LogRecord object. This record contains metadata — timestamp, filename, line number, function name, log level — before any formatter processes it. That metadata enables deterministic serialization into JSON without context loss. To emit structured output, replace the default logging.Formatter with one that serializes the record.

import logging
import json
import sys class JsonFormatter(logging.Formatter): def format(self, record): log_entry = { "timestamp": self.formatTime(record, self.datefmt), "level": record.levelname, "logger": record.name, "module": record.module, "function": record.funcName, "line": record.lineno, "message": record.getMessage(), } if record.exc_info: log_entry["exception"] = self.formatException(record.exc_info) return json.dumps(log_entry) # Configure root logger
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JsonFormatter())
logging.basicConfig(handlers=[handler], level=logging.INFO) logger = logging.getLogger("flask_app")

Enter fullscreen mode Exit fullscreen mode

Now, when you log:

logger.info("User login attempted", extra={"user_id": 123, "ip": "192.168.1.1"})

Enter fullscreen mode Exit fullscreen mode

You get:

{"timestamp": "-11-15 14:22:30,123", "level": "INFO", "logger": "flask_app", "module": "auth", "function": "login", "line": 45, "message": "User login attempted", "user_id": 123, "ip": "192.168.1.1"}

Enter fullscreen mode Exit fullscreen mode

The extra dictionary is merged into the top level of the JSON output because those keys become attributes on the LogRecord instance. This behavior is consistent and predictable — no additional configuration needed.


🐍 Loguru — Simpler, More Expressive Setup

The standard logging module requires boilerplate and careful handler management. Loguru reduces that surface area with better defaults, cleaner composition, and native support for structured output. Its core abstraction is the sink — a generalized destination for log events. Sinks can be streams, files, or network endpoints, and each can have its own format, filter, and serialization logic. Install it:

$ pip install loguru


Collecting loguru Downloading loguru-0.7.2-py3-none-any.whl (58 kB)
Installing collected packages: loguru
Successfully installed loguru-0.7.2

Enter fullscreen mode Exit fullscreen mode

Configure JSON output:

from loguru import logger
import sys
import json # Remove default handler
logger.remove() # Add JSON sink
logger.add( sys.stdout, format=lambda record: json.dumps({ "time": record["time"].isoformat(), "level": record["level"].name, "message": record["message"], "module": record["module"], "function": record["function"], "line": record["line"], **record["extra"] }), level="INFO"
)

Enter fullscreen mode Exit fullscreen mode

Loguru supports contextual binding via bind():

@app.route("/login", methods=["POST"])
def login(): user_id = authenticate(request.json) if user_id: authenticated_logger = logger.bind(user_id=user_id, ip=request.remote_addr) authenticated_logger.info("User authenticated") return {"status": "ok"} else: logger.warning("Login failed", ip=request.remote_addr) return {"status": "unauthorized"}, 401

Enter fullscreen mode Exit fullscreen mode

Output:

{"time": "-11-15T14:25:10.123456+00:00", "level": "INFO", "message": "User authenticated", "module": "app", "function": "login", "line": 23, "user_id": 456, "ip": "192.168.1.1"}

Enter fullscreen mode Exit fullscreen mode

bind() attaches key-value pairs to the logger instance, propagating them across all subsequent log calls from that instance. This avoids repetitive extra kwargs and reduces error surface.

Structured logging isn’t about format — it’s about making every log line queryable, filterable, and traceable.

🧠 Context Propagation — Keeping Data Across Functions

In Flask, request-scoped data like trace IDs or user identifiers should appear in all logs for that request without manual pass-through. Loguru integrates with Python’s contextvars to maintain state across async and threaded contexts. Use patch() to inject bound data into every log record within the request lifecycle.

from flask import g @app.before_request
def attach_log_context(): trace_id = request.headers.get("X-Trace-ID", "unknown") logger.bind(trace_id=trace_id).patch(lambda record: None) @app.after_request
def clear_context(response): logger.unbind("trace_id") return response

Enter fullscreen mode Exit fullscreen mode

After binding, every logger.info() or logger.error() call within the request includes the trace_id field. This aligns logs across functions and services during incident investigation.

🔧 Handling Exceptions — Auto-JSON Tracebacks

Loguru captures full stack traces by default when using logger.exception():

try: risky_operation()
except Exception: logger.exception("Operation failed")

Enter fullscreen mode Exit fullscreen mode

Output includes:

"exception": "Traceback (most recent call last):\\n File \"app.py\", line 30, in login\\n risky_operation()\\n File \"utils.py\", line 12, in risky_operation\\n raise ValueError('Boom')\\nValueError: Boom"

Enter fullscreen mode Exit fullscreen mode

For non-critical paths, use the @logger.catch decorator:

@logger.catch
def risky_operation(): return 1 / 0

Enter fullscreen mode Exit fullscreen mode

This logs the traceback and prevents the exception from halting execution. Useful for optional processing or background tasks where failure shouldn't crash the request.


📦 Flask Integration — Seamless Middleware Injection

To gain observability at the HTTP layer, capture request metadata — method, path, status, duration — automatically. Use Flask’s before_request and after_request hooks to wrap each incoming request.

from time import time
from flask import request, g @app.before_request
def start_timer(): g.start = time() logger.bind(method=request.method, path=request.path, ip=request.remote_addr).patch(lambda record: None) @app.after_request
def log_request(response): duration = time() - g.start logger.info( "Request completed", status=response.status_code, duration=f"{duration:.4f}s", length=response.content_length or "-" ) return response

Enter fullscreen mode Exit fullscreen mode

Example output:

{"time": "-11-15T14:30:00.123456+00:00", "level": "INFO", "message": "Request completed", "module": "app", "function": "log_request", "line": 45, "method": "POST", "path": "/login", "ip": "192.168.1.1", "status": 200, "duration": "0.1234s", "length": "15"}

Enter fullscreen mode Exit fullscreen mode

This adds full request observability without touching application logic.

💡 Filtering Noise — Exclude Health Checks

Health endpoints like /health or /metrics generate high-volume, low-value logs. Filter them early to reduce noise and storage cost. Skip binding and timing for known endpoints:

@app.before_request
def start_timer(): if request.path in ["/health", "/metrics"]: return g.start = time() logger.bind(method=request.method, path=request.path, ip=request.remote_addr).patch(lambda record: None)

Enter fullscreen mode Exit fullscreen mode

Alternatively, disable logging per route using a decorator:

def no_log(func): def wrapper(*args, **kwargs): with logger.disabled(): return func(*args, **kwargs) return wrapper @app.route("/health")
@no_log
def health(): return "OK"

Enter fullscreen mode Exit fullscreen mode

🔐 Security — Avoid Logging Sensitive Data

Never log passwords, authentication tokens, or personally identifiable information (PII). Sanitize request payloads before inclusion:

safe_data = {k: v for k, v in request.json.items() if k not in {"password", "token"}}
logger.bind(body=safe_data).info("Login request received")

Enter fullscreen mode Exit fullscreen mode

Prefer allowlists over denylists:

logged_fields = {k: request.json[k] for k in ["email", "country"] if k in request.json}

Enter fullscreen mode Exit fullscreen mode

This ensures only explicitly permitted fields enter the log stream.


🔍 Production Best Practices — Making Logs Actionable

Structured logs only deliver value if used correctly in production environments. First, always emit logs to stdout. Container orchestrators like Kubernetes expect applications to write logs to standard output so agents (e.g., Fluentd, Vector, Filebeat) can collect and forward them. Avoid writing directly to files. Second, standardize field names. Use consistent keys such as http.method, http.status_code, user.id, and trace.id across services. This enables reusable dashboards and alerting rules in tools like Grafana or Datadog. Third, adopt correlation IDs. Generate a unique ID per request and propagate it through logs and downstream services.

import uuid @app.before_request
def add_correlation_id(): cid = request.headers.get("X-Correlation-ID") or str(uuid.uuid4()) logger.bind(correlation_id=cid) g.correlation_id = cid @app.after_request
def add_correlation_header(response): response.headers["X-Correlation-ID"] = g.correlation_id return response

Enter fullscreen mode Exit fullscreen mode

Fourth, manage log levels rigorously. Use DEBUG for detailed traces, INFO for operational milestones, WARNING for recoverable anomalies, and ERROR for failures. Apply level filtering at the sink:

logger.add(sys.stdout, level="INFO", serialize=True)

Enter fullscreen mode Exit fullscreen mode

Fifth, consider performance. JSON serialization adds measurable CPU overhead under load. For high-throughput services, use orjson — an optimized JSON library written in Rust.

import orjson def json_serializer(obj): return orjson.dumps(obj).decode()

Enter fullscreen mode Exit fullscreen mode

orjson is up to 50× faster than the standard json module and handles common types like datetime and dataclass natively.

📦 Deployment — Logging in Docker & Kubernetes

In Kubernetes, pod logs are scraped from stdout by default. No custom configuration is required if your app emits JSON. Verify output:

$ kubectl logs my-flask-pod-7x9f2


{"time": "-11-15T14:35:00.123456+00:00", "level": "INFO", "message": "Request completed", "method": "GET", "path": "/api/users", "status": 200}

Enter fullscreen mode Exit fullscreen mode

Ensure your log agent parses JSON correctly. For Fluentd, use parser-type: json. For Grafana Loki, configure pipeline_stages in your agent to extract structured labels.

📉 Monitoring — Querying Structured Logs

With JSON logs, you move from text scanning to precise querying. In Loki :

"
{job="flask"} | json | level="ERROR" and path="/login"
"
In Datadog :

"
service:flask @level:ERROR @http.status_code:5xx
"
In Elasticsearch :

"json
{"query": {"term": {"http.status_code": "500"}}}
"
Filtering by status:500 or path:/login executes in milliseconds instead of scanning gigabytes of text. That precision is the core advantage of structured logging.

Good logs don’t just tell you what failed — they tell you who, when, where, and how it mattered.

🟩 Final Thoughts

Adding structured JSON logging to a Flask app isn’t a refactor — it’s a shift in how you treat logs. They become first-class data pipelines, not side-effect outputs. Both the built-in logging module and Loguru can achieve this. The former offers full control and zero dependencies. The latter delivers simpler syntax, better context handling, and native async support. Choose based on team familiarity and long-term maintainability — but don’t skip the step. Your logs will be queried during outages, often under pressure. Give your team structured, consistent, and secure data — not unstructured noise. Structured logging isn’t optional for modern systems. It’s the baseline for reliable observability in distributed environments.

❓ Frequently Asked Questions

Can I use both Python logging and Loguru in the same app?

Yes, but it’s not recommended. Loguru can intercept standard logging calls via logger.enable(), but mixing both increases complexity. Pick one and standardize across the codebase. (Also read: 🐍 How to set up CI/CD for a Python Flask app using GitHub Actions — common mistakes and key tips)

How do I rotate JSON log files in production?

Use Loguru’s built-in rotation: logger.add("logs/app.json", rotation="100 MB", serialize=True). For file-based logging, ensure your log shipper (e.g., Filebeat) can handle log rotation without missing entries.

Are JSON logs slower than plain text?

Yes, marginally — serialization adds CPU cost. But the trade-off in observability is almost always worth it. For high-throughput services, use orjson or consider sampling non-critical logs.

📚 References & Further Reading

  • Python logging module documentation — official guide to handlers, formatters, and log levels: docs.python.org
  • Flask logging best practices — integrating logging with request context and error handlers: flask.palletsprojects.com