aspose file tools
The moose likes Java in General and the fly likes Method get the name of the class that called it... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Method get the name of the class that called it..." Watch "Method get the name of the class that called it..." New topic
Author

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

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
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
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12324
    
    1
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


Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Method get the name of the class that called it...
 
Similar Threads
Using Stored procedure in spring with hibernate
create calendar java
one to one mapping problem
log4j logging twice
without being defined in a <security-role>