• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method get the name of the class that called it...  RSS feed

 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm trying to write my own version of Log4J, the reason being there are some specific things I'd like it to do that Log4J (to the best of my knowledge) doesn't

Namely things like:
- Spliting the log into seperate logs depending of the type. So log4j.error would go into error.log, log4j.info into a standard stdout.log etc etc


I have written everything i need, but I can't figure out how to do the Class name bit.

I'd like to beable to write something like



And output this:

[Thurs 06 Mar 2008 11:55:21, ThisIsTheClass, INFO] a message
[Thurs 06 Mar 2008 11:55:21, ThisIsTheClass, INFO] another message

So i'm think I need something like



Obviously 'getTheNameOfTheClassThatCalledMe' does not exist.

Or is there a way of changing the getClass method to something like this:



So I could pass the class name back into the method.
Again, 'this.getThisClassName' doesn't exist (being the ename of the class that is calling the class).

Any advise?
[ March 06, 2008: Message edited by: Keith Seller ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Seller:
Namely things like:
- Spliting the log into seperate logs depending of the type. So log4j.error would go into error.log, log4j.info into a standard stdout.log etc etc

Do you really need to write your own logging library for that? You could easily do that by writing your own appender for Log4J and plugging it in - no need to re-invent the whole of Log4J yourself.

Log4J is open source software, so you could just download the source code and figure out how Log4J does what you want to know.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a mechanism for getting the complete calling "StackTrace" that shows how a Thread got to a particular point. Look at the JavaDocs for java.lang.Throwable

You essentially create a new Throwable (but dont throw it) and call the getStackTrace() method.

Bill
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!