This page is no longer maintained — Please continue to the home page at www.scala-lang.org

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

5 replies
tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
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)?
David Hall
Joined: 2008-12-28,
User offline. Last seen 42 years 45 weeks ago.
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)?
>

tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
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)?
>

Landei
Joined: 2008-12-18,
User offline. Last seen 45 weeks 4 days ago.
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]

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
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.


Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
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 © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland