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

  • Home
  • SEARCH
  • 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 838711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:20:58+00:00 2026-05-15T05:20:58+00:00

I am using System.out.println in my code to track the execution of a program

  • 0

I am using System.out.println in my code to track the execution of a program and get some useful output. This creates results like this in the console:

Main function. Program starts.
Method getArea. Getting values
Method getSide. Side is 6
Method getArea. First value is 6
Method getSide. Side is 8
Method getArea. Second value is 8
Method getArea. Area is 48
Main function. The final area is 48

I would like to create tha method, which adds a space in front of the output every time the code goes deeper in the method call stack. For example, the same code but instead of using System.out.println, now with Misc.smartPrintln:

Main function. Program starts.
 Method getArea. Getting values
  Method getSide. Side is 6
 Method getArea. First value is 6
  Method getSide. Side is 8
 Method getArea. Second value is 8
 Method getArea. Area is 48
Main function. The final area is 48

The method would have this definition:

public static void smartPrintln(String string);

I don’t know how to implement this functionality. Any ideas how to solve this? And, could the use of a logger offer this functionality?

  • 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-15T05:20:59+00:00Added an answer on May 15, 2026 at 5:20 am

    Create a temporary Throwable object.
    Use its getStackTrace() method to analyze the stack and determine the level.
    e.g.

    public static void smartPrintln(String string) {
        Throwable t = new Throwable();
        StackTraceElement[] stackElements = t.getStackTrace();
        int level = stackElements.length - 1; // don't forget our function adds a level
        for (int i = 0; i < level; i++) {
            System.out.print(' '); // could probably make this more efficient
        }
        System.out.println(string);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a simple hello-wordish code using hibernate sf = new Configuration().configure().buildSessionFactory(); System.out.println(sessionFactory is
while using the appengine sdk i can't view the Output of my System.out.println statements
I am using the following code String str=12345; byte[] b=str.getBytes(UTF-8); String str1=new String(b,UTF-8); System.out.println(str1);
I want to print any text without using system.out.println() in java? It is possible
I have two different-sized monitors, connected together using (I believe) TwinView. I tried System.out.println(Toolkit.getDefaultToolkit().getScreenSize());
I have this JList that I'm using as the display for System.out . I'm
I'm using the following code to get ErrorStream. shellinput[0] = /system/xbin/dd if=/dev/zero of=; shellinput[1]
A simple design question. Sample code: Integer int1 = new Integer(20); System.out.println(Integer.toBinaryString(int1)); Why JDK
I'm using this code, and I get the stack trace that is listed below.
I heard that using System.out.println for logging purposes is a very bad practice and

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.