Find out which is the calling method at runtime

Hello.
I have to write a very simple, custom logger. Don't tell me to use logging API, since I can not, unfortunately :-(
In order to log informations I use a class calles FileLogger, which has some methods, like warn(String message), info(String message) which use a private method
void write(String message, int logLevel){
what I'd like to do is to make the method write(String message, int logLevel) log non only the message and the log level, but also the class and the method from which it was called.
How can I do?
Thanks

There is a way to do it - if you only want it when an error occurs, it's fairly easy - and PIFFLE on performance - it only occurs when there's an error (which should be rare!) for pete's sake!
Make it even better and have an if checking a public static final boolean in your error class and you get ZERO overhead if you have logging off - of course you have to change the kode.
-G
import java.io.*;
import java.util.*;
public class A
public static String caller()
ByteArrayOutputStream bs = new ByteArrayOutputStream();
PrintWriter pw = null;
try
throw new Exception("Exc");
catch (Exception e)
e.printStackTrace(new PrintWriter(bs, true));
StringTokenizer st = new StringTokenizer(bs.toString(),"\r\n");
st.nextToken();
st.nextToken();
st.nextToken();
if (!st.hasMoreElements())
return "None";
String theString = st.nextToken();
theString = theString.substring(theString.indexOf(' ')+1);
return theString;
public static void doSomething()
System.out.println("Caller is "+caller());
public static void main(String[] args) throws Exception
doSomething();
System.out.println("Caller is "+caller());
}

Similar Messages

  • How to find out which job is calling package

    Respected sir,
    How to find out which job is calling my package. Please help me regarding this.
    Regards,
    user570124

    Please read about [url http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_util.htm#i997163]DBMS_UTILITY.FORMAT_CALL_STACK in the manual.
    or [url http://asktom.oracle.com/tkyte/who_called_me/index.html]this routine from Tom Kyte may be what you are looking for.
    Regards,
    Rob.

  • HT4519 when i try send a email off my iphone its says my house name is ,how do i find out which is the correct name?

    when i try send a email off my iphone its says my house name is wrong,how do i find out which is the correct name?

    Try deleting the account and then add it back.

  • How to find out which was the last Function Fired in a given schema ..?

    Hi All ,
    Suppose there is a function called track_account (Accnt_id, bank_name ...)
    I have a requirement wherein I need to find out when was the last date when this function was called , by which user and what was the parameter list that was passed .For example i this case - what accountid and bank_name combinations were passed .
    I know the information is stored in one of the V$ tables but am not able to locate the exact table and column that would give me this information .
    Please suggest .
    Edited by: user3481493 on Mar 8, 2010 10:58 PM

    amol_dev wrote:
    Thank you for your reply Brynjar.
    However, I was trying to figure out a way to do this without having to change the calling command line, because due to legacy reasons, it is not possible to change the command line calling the parent applications of this common code.You cannot change the command line, but you can change the applications?
    In main() do this:
    System.setProperty("whoami",(new Throwable().getStackTrace()[0].getClassName()));Elsewhere:
    String whoami = System.getProperty("whoami");

  • How do I find out which "page" the user was on

    I am using JAVA servlets and XSLT's to create a web service. I need to know how I can use the HTTPRequest that is sent to me to the server to find out, which "page" they were on when they clicked the login link.
    The login link appears on all "pages" and once they click it a loginServlet is activated an a login page is shown. I need to know which "page" they were on so I can take them back to it after login has completed.
    So take for example I am at http://localhost:8080/contact/ and then they clicked a hyperlink with value http://localhost:8080/login to call loginServlet. I know getPathInfo will give me the "/login" but how do i get that "/contact" so i can send them back to that page once the system has logged them in.
    Any help, suggestions, comments would be highly appreciated it. Thanks.

    Retrieve the "Referer" value from the requestHeader:
    String referer = request.getHeader("Referer");Notice the misspelling, you must spell it this way. BTW, check for nulls. I don't think there's any guarantee this value is set, although I've never run across a situation where it wasn't.

  • How to find out which application the code is running in?

    Hi,
    I am trying to develop a common code which will be part of various different applications.
    But I need to be able to figure out within the Java application which process is calling me.
    For example: one of the applications may be invoked as:
    java /opt/app1_dir/app1 ... (various command line options)
    I need the ability to access the String "/opt/app1_dir/app1" in the above call. I think String array from main method args[0,...] will give me the ability to access the command line parameters but not the actual command string.
    Due to legacy reasons, it is not possible to change the calling string.
    Can anyone please help me to figure out how I can access the above in my java application.
    Thanks,
    Amol

    amol_dev wrote:
    Thank you for your reply Brynjar.
    However, I was trying to figure out a way to do this without having to change the calling command line, because due to legacy reasons, it is not possible to change the command line calling the parent applications of this common code.You cannot change the command line, but you can change the applications?
    In main() do this:
    System.setProperty("whoami",(new Throwable().getStackTrace()[0].getClassName()));Elsewhere:
    String whoami = System.getProperty("whoami");

  • Is there a way to find out which transactions are called by first transacti

    I'm trying to find out if there is a way to find out for ex:
    You go to PFCG and put in transaction CO15
    What other transactions is CO15 going to need or call.
    Thanks
    Joe

    In your test or QA environment, use ST01 to start a trace for authorization checks.  Execute CO15 as you normally would using options on the screen to enter the production order confirmation.  Once complete, stop the trace and review the log.  the log will include all authority checks that occurred.
    Another response was to use the USOBT table.  You can call these tables directly using SU24.  the check maintain entries are validate when an authority check is executed.

  • TS1702 How can I find out which is the correct app to read a PDF on my iPhone 5?

    I purchased a PDF reader from the app store, but I still cannot read the file. When I try to download the file, it states that I need an updated version of PDF. I notice there are several PDF apps available, but I don't know which is the correct one. How can I find out the correct application I need?

    Select the tune and then - Get Info - in the dialog box is a note of the Apple ID used to purchase.
    MJ

  • How to find out which user processed an action at runtime?

    Hi all,
    Is there any way to find out the user who processed an action at runtime? Is there a predifined CA for this or should I do a bit of development?
    I need this information in the next action after that.
    Thanks!

    what you mean by group of users.....
    whether all the users are using the same user name ?
    if it is not like that we can get the user name of the current logged in user .
    using the following code.
    IWDClientUser wdUser = WDClientUser.getCurrentUser();
    with regards
    shanto aloor

  • TS1277 how do i find out which 5 computers i have authorised ofr itunes. i think i only have 4 but cant find out which one is the fifth

    I am told from itunes that i cannot authorise my macbook air as i already have 5 computers authorised on this account. I only have 4 but dont know how to find out which is the fifth authorised computer

    You cannot.
    There is no list
    Deauthorize all, then authorize the active computers.

  • How to find out which one of the few devices connected to the account made a purchase through ITunes?

    How can I find out which of the devices assosiated to my account made a purchase?

    the only reliable way to do this is by scanning table TBTCP using the person's userID in field AUTHCKNAM and filtering by job statuses: scheduled, released, ready and active (sorry I can't remember the code#'s off top of my head, something like P=scheduled, S=scheduled...you have to look it up).   When you search SM37 it only permits you to scan by the userID who created the job, but it won't tell you if a different userID is defined in a job step.  That's why you need to scan TBTCP.  Don't forget that you should also check for a user's open Workflow items before deletion.
    I noticed some posts in this forum that suggest a userID should never be deleted, only locked, added to a special user group, and all role assignments removed.  However I've never worked somewhere that does this.

  • When using addTarget:action:forControlEvents: how to find out which event?

    When you use addTarget:action:forControlEvents: how can the action find out which event got called? the (id)sender is the object of sender, but no parameter for the event?

    thanks but the event is always nil for
    [uitextfield addTarget:self action:@selector(validate:forEvent:) forControlEvents: UIControlEventAllEvents ];
    any ideas?

  • Finding out which jars are needed

    I am using an open source library. I am only using a fraction of its functionality, and the library is huge (it drags in many other JARs from other OS projects). This is making my install file very large, and I need to cut it down.
    If I take just the parts of the library I am actually using, is there any way to find out which of the other JAR files are still being used? It seems that a missing JAR at runtime might not be detected when the program runs up (is that correct?) but might result in a runtime error later on when Java tries to load the library.
    Is there a more methodical way of doing this than just removing various JARs and checking if it still works?

    Run app with -verbose:class - you can do this standalone or add parameter to JRE in the Java Control Panel
    (also enable tracing and then check result trace file).
    This option will list where each class is loaded from.

  • How to find out which object on a JPanel was clicked...

    I have a JPanel that contains about 200 other small JPanels(only 8 are visible at any one time), and a JButton(with Visible set to false).
    When one of the small JPanels is rightclicked i want to know which one was clicked so I can add the JButton to it (set theJButton Visible on the JPanel that was rightclicked). When the JButton is clicked, it will hide the JPanel it was on and hide itself again.
    I added a MouseListener(MousePressed) to the main Jpanel but I dont know how to find out which of the smaller JPanels was clicked. I would Also need to know how (in the ActionEvent for the JButton) the JButton knows which JPanel it is on so it knows which one to delete.
    ------------------------------------------------------------------CODE
    void JPanel1_mousePressed(MouseEvent e) {
    if ((e.getModifiers() & e.BUTTON3_MASK)!=0){
    ------------------------------------------------------------------CODE
    void JButton1_actionPerformed(ActionEvent e) {
    ------------------------------------------------------------------CODE
    Can this be done or should I add a MousePressed Listener to each of the 200 small JPanels and create a JButton for each of them??
    Any Info Appreciated... Thanks....

    Here you get a hint, hope have added all mouseListener as well and registered....addMouseLi...etc
    // many JPanels here...
    JPanel clickedPanel;  // a reference only
    // In mouse Click..
    public void mouseClicked( MouseEvent e ){
              clickedPanel = e.getSource( );
              clickedPanel.add(new JButton("Look, I am Added") );
    }  // it is Only HINT, a working HINT

  • HT204053 how can i find out which id i used to purchase an app?

    i can't update iphoto, imovie and garageband cause it says "sign in to the account you used to purchase it" but those were pre-installed when i bought my iMac and i don't have a second id. what can i do or how can i find out which is the right account for these apps?
    thanks in advance

    I also have a linked problem like that. I bought various apps under different ids and now I don't know how to sign in - where to sign in under the emails that I bought them on so i can't update my Pages app.
    Can anyone help us please?
    Is there a way to put them altogether now under my apple id with icloud?
    Thanks

Maybe you are looking for