Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

How to encapsulate Logger info

 
Ranch Hand
Posts: 602
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
im using the log4j logger , but now i like to do generic Logger to my application
and encapsulate all the log4j initial data it needed in some generic Logger class
my first proablem is for example if i call from my class this generic Logger how can it know
my class name whiteout passing it the "MyClass.class" as parameter so log4j could use this information?

for example the code :


thanks for your help
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of

you could write something like

Then you wouldn't need to pass the class name along.

Your logging class could also examine the current stack trace, where the second-but-last entry would be the calling class and method. You can get the stack trace via "new Throwable().getStackTrace()".

But why do you want to roll your own logging abstraction, when something like Jakarta Commons Logging is available, which can wrap java.util.logging, log4j, and other logging implementations?
[ November 12, 2006: Message edited by: Ulf Dittmer ]
 
Meir Yan
Ranch Hand
Posts: 602
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seams not to work in static method

the getClass().getName()

is there way to overcome this ? my method have to be static
 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the shortcut MyClass.class, which should work in a static method as well.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!