Best way to show line number of logging line? #61

Open
mrjcleaver opened this Issue Mar 13, 2012 · 5 comments

Comments

Projects
None yet
4 participants

In log4js-node, is there a way to automatically show the file and line number that logs a message?

I found

http://code.google.com/p/aost/source/browse/trunk/tools/firefox-plugin/trump/chrome/content/logger.js?r=858 - should I do something custom like this?

Sorry, I did look through existing tickets and documentation but my search came up no hits. It just seems like an obvious thing to want.

Best, Martin.

Collaborator

nomiddlename commented Mar 13, 2012

There's nothing to do this at the moment, but I'd be happy to accept a pull request that implemented it.

Collaborator

nomiddlename commented Mar 27, 2012

Looks like this module would be an easy way to implement it, if anyone is interested: http://www.devthought.com/2012/03/26/a-better-console-log/

vjpr commented May 26, 2013

This is what I use:

path = require 'path'
stackTrace = require 'stack-trace'

getCallerFile = ->
  frame = stackTrace.get()[8]
  file = path.relative process.cwd(), frame.getFileName()
  dir = path.dirname file
  ext = path.extname file
  base = path.basename file, ext
  line = frame.getLineNumber()
  #method = frame.getFunctionName()
  "#{dir}/#{base.underline}#{ext}##{line}"
  log4js.configure
    appenders: [
      {
        type: 'logLevelFilter'
        level: 'TRACE'
        appender:
          type: 'console'
          layout:
            type: 'pattern'
            #pattern: "%[%p%] #{"%c".underline.grey} - %m"
            #pattern: "%[%p%] #{"%c".underline} (#{"%x{filename}"}) - %m"
            pattern: "%[%5p%] #{"%x{filename}".grey} - %m"
            tokens:
              filename: getCallerFile

Has anyone tackled a Pull Request using the module proposed by @nomiddlename ? I can try with my still basic Javascript.

Collaborator

nomiddlename commented Nov 1, 2017

PR #580 should fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment