This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 21450
95
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: 23692
50
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: 21450
95
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: 23692
50
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!