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

推荐订阅源

Recent Announcements
Recent Announcements
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
博客园_首页
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
Jina AI
Jina AI
月光博客
月光博客
小众软件
小众软件
S
SegmentFault 最新的问题
量子位
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

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
Who Should Build the Audit Log? A Question I Faced as a J...
Babisha S · 2026-05-17 · via DEV Community

Babisha S

While building a real project, I ran into a design decision that looked small on the surface, but turned out to teach me something big about how backend systems are structured.

I am not a professional developer yet. I am learning, building things, making mistakes, and figuring stuff out as I go. Recently, I was implementing audit logging for my Spring Boot project, basically keeping a record of "who changed what and when."

That's when I discovered there are two very different ways to write the same feature. And the difference isn't what I expected.

what is audit logging?

Before we get into the design debate, a quick note on what audit logging is. It's a trail of records that says things like: "User john updated Transaction 101 on Tuesday, changing the amount from ₹500 to ₹750." These logs are critical in any business application for debugging, compliance, and accountability.

So the goal is simple: every time something important changes in the system, save a log of it. The tricky question is how.

The two approaches I found

Approach 1 - The service builds the object

Here, the service method accepts individual values and handles everything

public void log(String entityType, Long entityId, String action,
                String performedBy, String username,
                String oldVal, String newVal, String source) {

    repo.save(AuditLog.builder()
        .entityType(entityType)
        .entityId(entityId)
        .action(action)
        .performedBy(performedBy)
        .performedByUsername(username)
        .oldValue(oldVal)
        .newValue(newVal)
        .source(source)
        .build());
}

Enter fullscreen mode Exit fullscreen mode

When other methods calls this, it looks like.

auditService.log(
    "Transaction", 101L, "UPDATE",
    "EMP001", "john",
    oldJson, newJson, "WEB"
);

Enter fullscreen mode Exit fullscreen mode

The caller doesn't need to think about AuditLog at all.

Approach 2 — the caller builds the object

Here, whoever is calling the method builds the object themselves:

AuditLog audit = AuditLog.builder()
    .entityType("Transaction")
    .entityId(101L)
    .action("UPDATE")
    .build();

auditService.log(audit);

Enter fullscreen mode Exit fullscreen mode

The service just saves it to the database.

public void log(AuditLog auditLog) {
    auditLogRepository.save(auditLog);
}

Enter fullscreen mode Exit fullscreen mode

Why Approach 1 is preferred in most business apps

If in future we need to add extra fields like sessionID, deviceInfo in every audit record.

With Approach 1, you change one place — the service method. Every single caller automatically benefits. No one has to update their code.

With Approach 2? Every piece of code that creates an AuditLog.builder() needs to be found and updated. If three different developers wrote three different callers, you might end up with:

Developer A: .action("UPDATE")
Developer B: .action("updated") // inconsistent

Your audit data becomes a mess. Approach 1 prevents this by centralizing the construction logic.

So when is Approach 2 actually the right choice?

Approach 2 isn't wrong it's just for different situations. It shines when the callers genuinely need control over the object:

  • Kafka consumers or batch jobs that produce their own structured audit data
  • External system integrations where the full object already arrives pre-built

What I actually learned from this:

  • The same feature can be designed in fundamentally different ways, and the difference matters.
  • Centralized construction = easier maintenance, Caller construction = more flexibility.

I'm still early in my learning journey, and I find that the most useful things I pick up come from real decisions in real projects not just tutorials. If you're in the same boat, I hope this helped. It definitely made me think differently about how I structure service methods.