commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scovetta, Michael V" <Michael.Scove...@ca.com>
Subject RE: commons logging class level logging
Date Tue, 29 Mar 2005 17:09:43 GMT
Trenton,

I'm working on project to do add method-level authorization to Java
(actually, it's just a simple class, but it's pretty cool)-- Anyway, at
the heart of it is a method:

Play with it a little, I believe if you have

public class Foo {
  public static void main(String[] args) {
    System.out.println(getCurrentMethodName(1));
  }
}
You get "Foo.main:3"

If you have:
public class Foo {
  public static void main(String[] args) {
    bar();
  }
  public static void bar() {
    System.out.println(getCurrentMethodName(2));
  }
}
You also get "Foo.main:3" (since it back-tracked 2 method calls)

It's rough, but I think this'll work for ya. I've attached the source
code for the CallerUtils project, it's not complete yet, but it's a
start.


/**
 * Underlying method to get the calling method's name.
 * @param extra number of extra callees to go back
 * @return method name, package.class.method:line
 */
public static String getCurrentMethodName(int extra) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter pw = new PrintWriter(baos);
        (new Throwable()).printStackTrace(pw);
        pw.flush();
        String stackTrace = baos.toString();
        pw.close();

        StringTokenizer tok = new StringTokenizer(stackTrace, "\n");
        String l = tok.nextToken(); // 'java.lang.Throwable'
        l = tok.nextToken(); // 'at ...getCurrentMethodName'
        for (int i = 0; i < extra; i++) {
            l = tok.nextToken(); // 'at ...timestampProfiler or
others...
        }
        l = tok.nextToken(); // 'at ...<caller to getCurrentRoutine>'
        // Parse line 3
        tok = new StringTokenizer(l.trim(), " <(");
        String t = tok.nextToken(); // 'at'
        t = tok.nextToken(); // '...<caller to getCurrentRoutine>'
        String line = tok.nextToken();
        int colon = line.indexOf(":");
        if (colon != -1) {
            int paren = line.indexOf(")", colon);
            line = line.substring(colon + 1, paren);
            return t + ":" + line;
        } else {
            return t;
        }
    } catch (Exception ex) {
        return "Unknown method.";
    }
}

Michael Scovetta
Computer Associates
Senior Application Developer


-----Original Message-----
From: Trenton D. Adams [mailto:trenta@athabascau.ca] 
Sent: Tuesday, March 29, 2005 11:58 AM
To: Jakarta Commons Users List
Subject: Re: commons logging class level logging

Ok, this is slightly off topic for this list then.  But I'd appreciate 
any help.

What's the quickest and easiest way of my code knowing what class called

a method?  The reason I ask is because we have some wrapper methods in 
our main class that every other class calls to do logging.  I would like

that method to be able to determine what class it was that called, so 
that I could use the proper logger/category.

Also, for log4j, is Logger.getLogger("classname") an efficient way of 
getting a logger for each class?  e.g. should I be calling this method 
every time my *wrapper* log method is called?  How does the commons 
logging do this properly, surely it has to do something similar since 
it's a wrapper too?

Simon Kitching wrote:
> Trenton D. Adams <trenta <at> athabascau.ca> writes:
> 
> 
>>I thought I read somewhere that JCL allows one to turn on/off debug 
>>logging based on the package or class name.  Is that right?  I'm
looking 
>>on the JCL website, but can't find information on that.  Perhaps
that's 
>>because I don't know what to search for!
>>
> 
> 
> JCL is simply a "wrapper" that provides a common API to a number of
different 
> logging libraries. Configuration of the underlying library is
explicitly *not* 
> part of JCL.
> 
> So if you are using log4j as the underlying library then you need to
read up on 
> how to configure log4j to turn on/off logging by category name. If you
are 
> using native jdk-1.4 logging as the underlying logging library then
you need to 
> read up on configuring that, etc.
> 
> Both of the above logging systems *do* have the ability to filter log
messages 
> based on log category name (and the recommended convention is for code
to use 
> the current class name as the log category).
> 
> Regards,
> 
> Simon
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


-- 
Trenton D. Adams
Web Programmer Analyst
Navy Penguins at your service!
Athabasca University
(780) 675-6195
:wq!

__ 
    This communication is intended for the use of the recipient to whom
it
    is addressed, and may contain confidential, personal, and or
privileged
    information. Please contact us immediately if you are not the
intended
    recipient of this communication, and do not copy, distribute, or
take
    action relying on it. Any communications received in error, or
    subsequent reply, should be deleted or destroyed.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org






Mime
View raw message