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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:52:02+00:00 2026-05-12T08:52:02+00:00

I have a logging function that takes the calling object as a parameter. I

  • 0

I have a logging function that takes the calling object as a parameter. I then call getClass().getSimpleName() on it so that I can easily get the class name to add to my log entry for easy reference. The problem is that when I call my log function from a static method, I can’t pass in “this”. My log function looks something like this:

public static void log(Object o, String msg){
  do_log(o.getClass().getSimpleName()+" "+msg);
}
public void do_something(){
  log(this, "Some message");
}

But let’s say I want to log from a static function:

public static void do_something_static(){
  log(this, "Some message from static");
}

Obviously do_something_static() won’t work because it is static and “this” is not in a static context. How can I get around this? And can I do it without using reflection (since I understand there is a lot of overhead involved and it might affect performance since I log a LOT of data)

I know I can probably hard-code the current class into the call somehow, but I’m sure that when I move the function to another class, I will forget to update the hard-coded reference and it will no longer be correct.

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-12T08:52:02+00:00Added an answer on May 12, 2026 at 8:52 am

    You could add “Class” as first parameter and overload the log method:

    public class SomeClass {
        // Usage test:
        public static void main( String [] args ) {
            log( SomeClass.class, "Hola" );
            log( new java.util.Date(), "Hola" );
        }
    
        // Object version would call Class method... 
        public static void log( Object o , String msg ) {
            log( o.getClass(), msg );
        }
        public static void log( Class c ,  String message ) {
            System.out.println( c.getSimpleName() + " " + message );
        }
    }
    

    Output:

    $ java SomeClass
    SomeClass Hola
    Date Hola
    

    But it feels just bad to have the calling class passed as the first argument. Here’s where object oriented model comes into play as opposite of “procedural” style.

    You could get the calling class using the stacktrace, but as you metioned, there would be an overhead if you call it thousands of times.

    But if you create is as class variable, then there would be only one instance for class if you happen to have 1,000 classes using this utility, you would have at most 1,000 calls.

    Something like this would be better ( subtle variation of this other answer) :

    public class LogUtility {
    
        private final String loggingFrom;
    
        public static LogUtility getLogger() {
            StackTraceElement [] s = new RuntimeException().getStackTrace();
            return new LogUtility( s[1].getClassName() );
        }
    
        private LogUtility( String loggingClassName ) {
            this.loggingFrom = "("+loggingClassName+") ";
        }
    
        public void log( String message ) {
            System.out.println( loggingFrom + message );
        }
    }
    

    Usage test:

    class UsageClass {
        private static final LogUtility logger = LogUtility.getLogger();
    
        public static void main( String [] args ) {
    
            UsageClass usageClass = new UsageClass();
    
            usageClass.methodOne();
            usageClass.methodTwo();
            usageClass.methodThree();
    
        }
        private void methodOne() {
            logger.log("One");
        }
        private void methodTwo() {
            logger.log("Two");
        }
        private void methodThree() {
            logger.log("Three");
        }
    }
    

    OUtput

    $ java UsageClass
    (UsageClass) One
    (UsageClass) Two
    (UsageClass) Three
    

    Notice the declaration:

    ....
    class UsageClass {
        // This is invoked only once. When the class is first loaded.
        private static final LogUtility logger = LogUtility.getLogger();
    ....
    

    That way, it doesn’t matter if you use the “logger” from objectA, objectB, objectC or from a class method ( like main ) they all would have one instance of the logger.

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

Sidebar

Ask A Question

Stats

  • Questions 199k
  • Answers 199k
  • 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 Depends on what you mean. Any time you have a… May 12, 2026 at 7:50 pm
  • Editorial Team
    Editorial Team added an answer I'd check the tag on your page. Sounds like your… May 12, 2026 at 7:50 pm
  • Editorial Team
    Editorial Team added an answer What are you trying to do here? You don't have… May 12, 2026 at 7:50 pm

Related Questions

I created a class that handles serial port asynchronously. I use it to communicate
Original Question I am writting a logging class where the goal is to be
I am currently logging an AJAX application with messages which include the times of
I have no problems getting the file to upload and if I save it

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.