• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.out.println() variation  RSS feed

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have currently have a class with some tools in.
The class is called O, and has the method:


Allowing me to just write O.p("Hello World"); for example.

What I would like to do is make a similar method that displays the class name.
For example, in the class Main, calling O.p("Hello World"); should output "Main: Hello World"

I'm not sure if theres a simple solution for this, but it would help me in debugging as I have quite a few classes in my programs.

Thanks for any help
 
Sheriff
Posts: 21201
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way to do so would be to use the stack trace (Thread.currentThread().getStackTrace()), then take the second element (the first element will be the p method call). This won't be efficient though.
 
Sheriff
Posts: 23121
45
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not efficient, maybe, but it's what log4j does when you ask it to report class names and that's rarely a problem.

(You could just use log4j, Colin, instead of trying to build your own logging package.)
 
Rob Spoor
Sheriff
Posts: 21201
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hardly ever let log4j or slf4j print the class name. I let it print out the logger name which is usually the same as the class name*, but not the class name itself.

* Since I create my slf4j loggers as LoggerFactory.getLogger(CurrentClass.class)
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replies,

I need jar files for the those logging things don't I?

For some reason when I jar a project that has jars added, sometimes the jar runs fine on my pc, but not on others computers.
Something to do with not being able to find those classes, I don't get why it works on one pc, but not another, so would rather avoid that from happening.
 
Paul Clapham
Sheriff
Posts: 23121
45
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

colin shuker wrote:I need jar files for the those logging things don't I?

For some reason when I jar a project that has jars added, sometimes the jar runs fine on my pc, but not on others computers.
Something to do with not being able to find those classes, I don't get why it works on one pc, but not another, so would rather avoid that from happening.



Well, yeah. You do need jar files. As for why it doesn't work on those other computers, that would be because you didn't set up the classpath properly there. It isn't hard to set up a class path, as I'm sure you know by now. It doesn't make sense to reinvent every wheel just for that reason.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I don't really get the thing with the classpath exactly.
 
Forget Steve. Look at this tiny ad:
ScroogeXHTML 7.1 - RTF to HTML5 / XHTML converter
https://coderanch.com/t/690611/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!