Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 580141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:26:29+00:00 2026-05-13T14:26:29+00:00

(Note that it’s more of a Bash question than a Java question, see note

  • 0

(Note that it’s more of a Bash question than a Java question, see note below)

When configuring log4j in each class, we do the following:

public class Example {

  private static final Logger log = Logger.getLogger( Example.class );

The problem is that we now have a medium-sized codebase (200K LOC) containing a lot of Java classes and… Quite some misconfigured log4j loggers.

It’s because people (including me, I admit), did silly cut’n’paste resulting sometimes in this:

public class Another {

  private static final Logger log = Logger.getLogger( Example.class );

And boom, instead of having Another.class, it’s the old Example.class that is left and hence wrongly appearing in the logs (hence causing quite a few headaches).

I find it kind of weird that such kind of misconfiguration can happen but it can and our main issue now is not that it can happen but that we have to fix the misconfured loggers.

How can we go about automatically detecting these? (the fix can be manual but I’d like a way to find all the classes where log4j is misconfigured).

A Bash shell script, for example, would be very welcome.

  1. for every .java file
  2. find every “class XXX”
  3. parse the next ‘x’ lines (say 20)
  4. is there a Logger.getLogger(…) line?
  5. if yes, does it match the “class XXX”?
  6. if no report

False positive ain’t an issue so it’s not a problem if a few bogus “class XXX” are parsed etc.

NOTE: the problem is really that we now have 200 000 lines of code and we’d like to detect the violation automatically (the fix can be manual) so the question is not similar to:

[Is there a better way to get the current class variable in java?1

Actually it’s probably more of a Bash question than a Java question 🙂

Any help on this most welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:26:29+00:00Added an answer on May 13, 2026 at 2:26 pm

    I guess if you’re looking for a one-liner, one-liner

    find -name "*.java" -exec sed -i \
        -e 's/private static final Logger \([a-zA-Z_][a-zA-Z0-9_]*).*$/private static final Logger \1 = LoggerFactory.make()/g' \
        -e 's/import org\.apache\.log4j\.Logger;/&\nimport path.to.LoggerFactory;/g' \
        {} \;
    

    I would back up your code before trying this. It’s probably broken in a couple places, but with a few corrections will get you what you’re looking for. If you’re using svn or something, you’ll have to tweak find to exclude the .svn directories, otherwise your commits will be really messed up.

    The gist: don’t even bother trying to capture class names. Incorporate the solution indirectly linked to by Alexander. But replace your initial Logger declarations with factory calls. The only thing you need to capture is the name of the local variable. Then you need to find where your imports are, which I’m assuming you can do pretty exactly because you’re importing log4j (or java.util.logging). Find that import statement and import your factory right below it.

    BTW, all the warnings you’re getting about automating this are correct and equally apply to this solution. You need to be prepared to javac everything right after trying this at the very least. Really, you should have some test suites with monster code coverage to automatically run at this point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 431k
  • Answers 431k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MSDN has a decent page describing the various helpers included… May 15, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer You should avoid passing STL objects between different binary modules.… May 15, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer I believe what Eric is trying to say is: Casting… May 15, 2026 at 2:16 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.