Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code's file name, function, line number from where logging was done #200

Open
kartikrustagi opened this issue Nov 20, 2012 · 34 comments
Open
Labels
Feature Request Request for new functionality to support use cases not already covered

Comments

@kartikrustagi
Copy link

It will be really helpful to have some standard logging metadata like file name, line number and perhaps function from which the .log method was called. Something similar to what we have in https://github.com/baryon/tracer

This is how they are doing it in tracer: https://github.com/baryon/tracer/blob/master/lib/console.js

Basically using the Error object to get the stack trace and these details out of it. Not sure if this is the best way.

ps: more than willing to contribute if you guys think this is useful

@SoulRaven
Copy link

this request has already been made, by me and other guys, and i understand, that is a little overkilling because each log request is necessary to call Error stack

@kartikrustagi
Copy link
Author

Then probably we can enable/disable this with a flag, default being disabled.

@SoulRaven
Copy link

yes, i guess is possible, but ho knows, if will be available any time soon

@ajostergaard
Copy link

@SoulRaven - I guess getting data from the req would be considered equally evil? e.g. like connect.logger

@SoulRaven
Copy link

i think is evil in some way, because on each log event, a Error object/stack must be created.
For me is ok this, but i am not sure if is the best think to do, even if is a must have option in any other log application.

@ajostergaard
Copy link

Ummm - the req object already exists though. :-S

@ankit042
Copy link

Are there any updates?

@jupe
Copy link

jupe commented Feb 4, 2014

I'm also waiting for updates..

@sassanh
Copy link

sassanh commented Mar 14, 2014

Waiting for this feature too...

@moperacz
Copy link

👍

1 similar comment
@felipegs
Copy link

👍

@taoeffect
Copy link

Here's what I'm using (with Winston) in the meantime:

gLineInfo: (prefix='') ->
    stack = new Error().stack
    # console.log stack.split('\n')[2]
    [file, line] = stack.split('\n')[2].split ':'
    [func, file] = file.split ' ('
    [func, file] = ['??', func] unless file # sometimes the function isn't specified
    [func, file] = [func.split(' ').pop(), path.basename(file)]
    [junk, func] = func.split('.')
    func = junk unless func
    func = if func is '??' or func is '<anonymous>' then ' (' else " (<#{func}> "
    prefix + func + file + ':' + line + ')'

Example usage from DNSChain:

@log.error gLineInfo("exception in handler"), {q:q, result:result}

@taoeffect
Copy link

Ah, and here's some example usage plus corresponding output from DNSChain's dns.coffee L200:

@log.warn gLineInfo('oldDNS timeout'), {err:err}

Produces:

2014-05-28T14:35:39.946Z - warn: [DNS] oldDNS timeout (dns.coffee:200) message=timeout

@indexzero indexzero added Feature Request Request for new functionality to support use cases not already covered v0.9.0 and removed v0.9.0 labels Sep 30, 2014
@Toub
Copy link

Toub commented Dec 9, 2014

+1

@Toub
Copy link

Toub commented Dec 9, 2014

User should be able to customize the format.

Ideally something like http://www.slf4j.org/nlog4j/api/org/apache/log4j/PatternLayout.html, or at least activate filename, linenumber and functionname independently (or based on log severity).

@indexzero
Copy link
Member

It should behave like console.trace

@Glavin001
Copy link

+1

7 similar comments
@akuma
Copy link

akuma commented Jul 3, 2015

+1

@aryeguy
Copy link

aryeguy commented Jul 14, 2015

+1

@Gary-Ascuy
Copy link

+1

@denizozger
Copy link

+1

@AmitThakkar
Copy link

+1

@Bigomby
Copy link

Bigomby commented Sep 14, 2015

+1

@na-ga
Copy link

na-ga commented Sep 16, 2015

+1

@jp-eagle
Copy link

+1

2 similar comments
@YogeshGK
Copy link

YogeshGK commented Oct 8, 2015

+1

@transitive-bullshit
Copy link

+1

@transitive-bullshit
Copy link

For anyone else wanting this functionality, I've created a public gist which shows how you can wrap winston to do it yourself.

@jesusprubio
Copy link

+1

@MickelSierra85
Copy link

Hi, is there any plan to add this to winson?

@yanCode
Copy link

yanCode commented Dec 29, 2015

the support of line number & file name really make much sense in my current project.

@amcdnl
Copy link

amcdnl commented Jan 13, 2016

+1

1 similar comment
@algomaks
Copy link

+1

@indexzero
Copy link
Member

There is no plan to add this to winston currently. The logistics of adding this code have severe performance implications. It would require capturing a call-stack on every log call which is very expensive.

A PR would be welcome for this IFF:

  1. It is optional
  2. The performance implications can be shown to be not extreme.
  3. It comes with tests.

Until such PR is made I am locking this issue to avoid further +1s. If you wanted to leave a +1 apologies, but your +1 has already been heard. It is clear many folks want this feature, but I think most of those folks don't understand the perf side effects of the implementation details.

@winstonjs winstonjs locked and limited conversation to collaborators Jan 21, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Feature Request Request for new functionality to support use cases not already covered
Projects
None yet
Development

No branches or pull requests