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 284695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:28:49+00:00 2026-05-12T05:28:49+00:00

Often I will send debugging info to my log file from within Java, and

  • 0

Often I will send debugging info to my log file from within Java, and it helps to give decent information as to where in the code the log message was generated so I can quickly refer back to it. I don’t want to hard-code anything in the log message about its location, because I have to remember to update the log message if I rename its method or otherwise refactor the code into a different structure. I’d love to have a static function that simply returns the calling function’s class and method name. For example, if I have a class called Machine and a method called doStuff, I’d like my log message generation code in that method to look something like this:

String log_msg = Blah.getClassAndMethodName()+" did the wrong thing!";

The static method getClassAndMethodName() in class Blah would return “Machine.doStuff”. That way, when I look at the log file, I can easily tell where in the code the message was generated. I realize this is a bit tricky, since I need this method in Blah to return information about its calling function in a separate class, Machine, but this would make it easy to just use this static call anywhere.

I don’t know if this is possible, but I’d appreciate any pointers towards an answer! Also, assuming it is possible, is this a particularly high overhead kind of operation? (I’m assuming reflection would be needed, not my strong suit)

Thanks!

  • 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-12T05:28:49+00:00Added an answer on May 12, 2026 at 5:28 am

    Try this:

        public static Log make() {
        Throwable t = new Throwable();
        StackTraceElement directCaller = t.getStackTrace()[1];
        return LogFactory.getLog(directCaller.getClassName());
    }
    

    The first entry in in the stacktrace, is the calling class

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From the sound of things, the people who wrote that… May 12, 2026 at 5:20 pm
  • Editorial Team
    Editorial Team added an answer It has to do with the fact that you provide… May 12, 2026 at 5:20 pm
  • Editorial Team
    Editorial Team added an answer How about one that does the whole thing in two… May 12, 2026 at 5:20 pm

Related Questions

I am developing a Java web application that bases it behavior through large XML
In the place I work, very often it happens that a developer and QA
I have a blog, that is update pretty often. I like, while on the
So I have an 'export' application that arrives the user at an end page

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.