Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 95878 invoked from network); 29 Mar 2005 17:09:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 29 Mar 2005 17:09:52 -0000 Received: (qmail 47124 invoked by uid 500); 29 Mar 2005 17:09:49 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 46582 invoked by uid 500); 29 Mar 2005 17:09:47 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 46569 invoked by uid 99); 29 Mar 2005 17:09:47 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (hermes.apache.org: local policy) Received: from mail13.ca.com (HELO mail13.ca.com) (141.202.248.42) by apache.org (qpsmtpd/0.28) with ESMTP; Tue, 29 Mar 2005 09:09:46 -0800 Received: from usilms28.ca.com ([141.202.201.28]) by mail13.ca.com with Microsoft SMTPSVC(5.0.2195.6713); Tue, 29 Mar 2005 12:09:43 -0500 X-MimeOLE: Produced By Microsoft Exchange V6.0.6556.0 content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----_=_NextPart_001_01C53482.19A6FEAA" Subject: RE: commons logging class level logging Date: Tue, 29 Mar 2005 12:09:43 -0500 Message-ID: <4876153C00898F43B189AF95F84BCE148E23EE@usilms28.ca.com> X-MS-Has-Attach: yes X-MS-TNEF-Correlator: Thread-Topic: commons logging class level logging Thread-Index: AcU0gIok1n8fxPaUQRKJ5YMxwily2QAAHHRg From: "Scovetta, Michael V" To: "Jakarta Commons Users List" X-OriginalArrivalTime: 29 Mar 2005 17:09:43.0486 (UTC) FILETIME=[19E68DE0:01C53482] X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N ------_=_NextPart_001_01C53482.19A6FEAA Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable 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 =3D new ByteArrayOutputStream(); PrintWriter pw =3D new PrintWriter(baos); (new Throwable()).printStackTrace(pw); pw.flush(); String stackTrace =3D baos.toString(); pw.close(); StringTokenizer tok =3D new StringTokenizer(stackTrace, "\n"); String l =3D tok.nextToken(); // 'java.lang.Throwable' l =3D tok.nextToken(); // 'at ...getCurrentMethodName' for (int i =3D 0; i < extra; i++) { l =3D tok.nextToken(); // 'at ...timestampProfiler or others... } l =3D tok.nextToken(); // 'at ...' // Parse line 3 tok =3D new StringTokenizer(l.trim(), " <("); String t =3D tok.nextToken(); // 'at' t =3D tok.nextToken(); // '...' String line =3D tok.nextToken(); int colon =3D line.indexOf(":"); if (colon !=3D -1) { int paren =3D line.indexOf(")", colon); line =3D 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]=20 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=20 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=20 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=20 that I could use the proper logger/category. Also, for log4j, is Logger.getLogger("classname") an efficient way of=20 getting a logger for each class? e.g. should I be calling this method=20 every time my *wrapper* log method is called? How does the commons=20 logging do this properly, surely it has to do something similar since=20 it's a wrapper too? Simon Kitching wrote: > Trenton D. Adams athabascau.ca> writes: >=20 >=20 >>I thought I read somewhere that JCL allows one to turn on/off debug=20 >>logging based on the package or class name. Is that right? I'm looking=20 >>on the JCL website, but can't find information on that. Perhaps that's=20 >>because I don't know what to search for! >> >=20 >=20 > JCL is simply a "wrapper" that provides a common API to a number of different=20 > logging libraries. Configuration of the underlying library is explicitly *not*=20 > part of JCL. >=20 > So if you are using log4j as the underlying library then you need to read up on=20 > how to configure log4j to turn on/off logging by category name. If you are=20 > using native jdk-1.4 logging as the underlying logging library then you need to=20 > read up on configuring that, etc. >=20 > Both of the above logging systems *do* have the ability to filter log messages=20 > based on log category name (and the recommended convention is for code to use=20 > the current class name as the log category). >=20 > Regards, >=20 > Simon >=20 >=20 >=20 >=20 >=20 > --------------------------------------------------------------------- > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-user-help@jakarta.apache.org >=20 --=20 Trenton D. Adams Web Programmer Analyst Navy Penguins at your service! Athabasca University (780) 675-6195 :wq! __=20 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 ------_=_NextPart_001_01C53482.19A6FEAA Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org ------_=_NextPart_001_01C53482.19A6FEAA--