Is it possible to get the package/class/method/function name as string for logging?

I've been using some simple logging for my application where I manually just add these names myself, so I wondered if it's possible to retrieve them automatically (preferably during compilation)?

Re: Is it possible to get the package/class/method/function nam

You can do something like (new Exception).getStackTrace()(1), which
gets you one of these:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StackTraceElement.html

for the method that called you.

HTH,
David

On Wed, Jan 7, 2009 at 3:09 PM, Trond Olsen wrote:
> I've been using some simple logging for my application where I manually just
> add these names myself, so I wondered if it's possible to retrieve them
> automatically (preferably during compilation)?
>

Re: Is it possible to get the package/class/method/function nam

How obvious, hehe. Thank you.

On Thu, Jan 8, 2009 at 12:13 AM, David Hall <david [dot] lw [dot] hall [at] gmail [dot] com> wrote:
You can do something like (new Exception).getStackTrace()(1), which
gets you one of these:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StackTraceElement.html

for the method that called you.

HTH,
David

On Wed, Jan 7, 2009 at 3:09 PM, Trond Olsen <tolsen77 [at] gmail [dot] com> wrote:
> I've been using some simple logging for my application where I manually just
> add these names myself, so I wondered if it's possible to retrieve them
> automatically (preferably during compilation)?
>

Re: Is it possible to get the package/class/method/function nam

Trond Olsen wrote:
>
> How obvious, hehe. Thank you.
>
> On Thu, Jan 8, 2009 at 12:13 AM, David Hall
> wrote:
>
>> You can do something like (new Exception).getStackTrace()(1), which
>> gets you one of these:
>>
>> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StackTraceElement.html
>>
>> for the method that called you.
>>
>> HTH,
>> David
>>
>> On Wed, Jan 7, 2009 at 3:09 PM, Trond Olsen wrote:
>> > I've been using some simple logging for my application where I manually
>> just
>> > add these names myself, so I wondered if it's possible to retrieve them
>> > automatically (preferably during compilation)?
>> >
>>
>
>
Throwing an exception for this is overkill. Since Java 1.5 you can write
Thread.currentThread().getStackTrace()[1]

Re: Is it possible to get the package/class/method/function nam

Sure, but that's implemented as return (new Exception()).getStackTrace().

Note that nobody was throwing an exception.

2009/1/8 Landei <Daniel [dot] Gronau [at] gmx [dot] de>


Trond Olsen wrote:
>
> How obvious, hehe. Thank you.
>
> On Thu, Jan 8, 2009 at 12:13 AM, David Hall <david [dot] lw [dot] hall [at] gmail [dot] com>
> wrote:
>
>> You can do something like (new Exception).getStackTrace()(1), which
>> gets you one of these:
>>
>> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StackTraceElement.html
>>
>> for the method that called you.
>>
>> HTH,
>> David
>>
>> On Wed, Jan 7, 2009 at 3:09 PM, Trond Olsen <tolsen77 [at] gmail [dot] com> wrote:
>> > I've been using some simple logging for my application where I manually
>> just
>> > add these names myself, so I wondered if it's possible to retrieve them
>> > automatically (preferably during compilation)?
>> >
>>
>
>
Throwing an exception for this is overkill. Since Java 1.5 you can write
Thread.currentThread().getStackTrace()[1]
--
View this message in context: http://www.nabble.com/Is-it-possible-to-get-the-package-class-method-function-name-as-string-for-logging--tp21342516p21347513.html
Sent from the Scala - User mailing list archive at Nabble.com.


Re: Is it possible to get the package/class/method/function nam



On Thu, Jan 8, 2009 at 4:07 PM, Ricky Clarkson <ricky [dot] clarkson [at] gmail [dot] com> wrote:
Sure, but that's implemented as return (new Exception()).getStackTrace().

Note that nobody was throwing an exception.

I saw Tony throwing one.
 


2009/1/8 Landei <Daniel [dot] Gronau [at] gmx [dot] de>


Trond Olsen wrote:
>
> How obvious, hehe. Thank you.
>
> On Thu, Jan 8, 2009 at 12:13 AM, David Hall <david [dot] lw [dot] hall [at] gmail [dot] com>
> wrote:
>
>> You can do something like (new Exception).getStackTrace()(1), which
>> gets you one of these:
>>
>> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StackTraceElement.html
>>
>> for the method that called you.
>>
>> HTH,
>> David
>>
>> On Wed, Jan 7, 2009 at 3:09 PM, Trond Olsen <tolsen77 [at] gmail [dot] com> wrote:
>> > I've been using some simple logging for my application where I manually
>> just
>> > add these names myself, so I wondered if it's possible to retrieve them
>> > automatically (preferably during compilation)?
>> >
>>
>
>
Throwing an exception for this is overkill. Since Java 1.5 you can write
Thread.currentThread().getStackTrace()[1]
--
View this message in context: http://www.nabble.com/Is-it-possible-to-get-the-package-class-method-function-name-as-string-for-logging--tp21342516p21347513.html
Sent from the Scala - User mailing list archive at Nabble.com.





--
Viktor Klang
Senior Systems Analyst

Copyright © 2013 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland