21

任意のレベルのすべてのロガーステートメントについて、ログステートメントが実行されたファイル名を表示する必要があります。以下は、以下の図です。

例:以下はJobWork.jsから実行された行です

logger.info("getInCompleteJobs in job works");

実際 :

2012-11-05T06:07:19.158Z - info: getInCompleteJobs in job works

必須 :

2012-11-05T06:07:19.158Z - info JobWork.js : getInCompleteJobs in job works

logステートメントからパラメータとしてfileNameを渡さずに、ファイル名を指定する必要があります。

4

5 に答える 5

43

ここではWinstonを使用しているようです-私は通常module、ロガーモジュールに渡してから、Winstonのlabelプロパティを解析済みバージョンのに設定しmodule.filenameます。何かのようなもの:

logger.js:

const path = require('path');

// Return the last folder name in the path and the calling
// module's filename.
const getLabel = function(callingModule) {
  const parts = callingModule.filename.split(path.sep);
  return path.join(parts[parts.length - 2], parts.pop());
};

module.exports = function (callingModule) {
  return new winston.Logger({
    transports: [new winston.transports.Console({
      label: getLabel(callingModule)
    })]
  });
};

使用法(モジュールはcontrollers/users.js)と仮定します:

const logger = require('./logger')(module);
logger.info('foo');

結果:

2014-11-25T15:31:12.186Z - info: [controllers/users.js] foo
于 2014-11-25T15:33:17.790 に答える
18

v8のオブジェクトに添付されたスタックトレース情報を使用してError、コードが呼び出されたファイル/行を見つけることができます。このアプローチはうまく機能しますが、うまく機能しません。したがって、開発中に使用する場合は、本番環境に移行するときに無効にする必要があります。

したがって、次のようなことができます。

  var logger_info_old = logger.info;
  logger.info = function(msg) {
    var fileAndLine = traceCaller(1);
    return logger_info_old.call(this, fileAndLine + ":" + msg);
  }

  /**
  * examines the call stack and returns a string indicating 
  * the file and line number of the n'th previous ancestor call.
  * this works in chrome, and should work in nodejs as well.  
  *
  * @param n : int (default: n=1) - the number of calls to trace up the
  *   stack from the current call.  `n=0` gives you your current file/line.
  *  `n=1` gives the file/line that called you.
  */
  function traceCaller(n) {
    if( isNaN(n) || n<0) n=1;
    n+=1;
    var s = (new Error()).stack
      , a=s.indexOf('\n',5);
    while(n--) {
      a=s.indexOf('\n',a+1);
      if( a<0 ) { a=s.lastIndexOf('\n',s.length); break;}
    }
    b=s.indexOf('\n',a+1); if( b<0 ) b=s.length;
    a=Math.max(s.lastIndexOf(' ',b), s.lastIndexOf('/',b));
    b=s.lastIndexOf(':',b);
    s=s.substring(a+1,b);
    return s;
  }
于 2012-11-16T06:22:59.160 に答える
4

各ファイルが個別のノードプロセスであると仮定すると、次のようなものを使用できます。process.argv[1].match(/[\w-]+\.js/gi)[0]

モジュールで機能するものを探している場合、これは機能する可能性があります。

process.mainModule.filename.match(/[\w-]+\.js/gi)[0]
于 2012-11-16T06:10:25.997 に答える
1

「consola」パッケージを使用したソリューション(ESモジュールのみ):

import { createLogger } from '@nitra/consola'
consola = createLogger(import.meta.url)

consola.debug('TEST')

出力:

[my-file.js] › TEST
于 2021-06-25T08:38:11.107 に答える
0

このコードを使用すると、ファイル名と行番号が記載されたログが表示されます。このコードを新しいファイルに貼り付けるwinston.jsと、このファイルで使用する必要があります。

var winston = require('winston')
var path = require('path')
var PROJECT_ROOT = path.join(__dirname, '..')
var appRoot = require('app-root-path');


const options = {
  file: {
    level: 'info',
    filename: `${appRoot}/logs/app.log`,
    handleExceptions: true,
    json: true,
    maxsize: 5242880, // 5MB
    maxFiles: 5,
    colorize: false,
    timestamp: true
  },
  console: {
    level: 'debug',
    handleExceptions: true,
    json: true,
    colorize: true,
    timestamp: true
  }
};

var logger = new winston.Logger({
  transports: [
    new winston.transports.File(options.file),
    new winston.transports.Console(options.console)
  ],
  exitOnError: false // do not exit on handled exceptions
});

logger.stream = {
  write: function (message) {
    logger.info(message)
  }
}

// A custom logger interface that wraps winston, making it easy to instrument
// code and still possible to replace winston in the future.

module.exports.debug = module.exports.log = function () {
  logger.debug.apply(logger, formatLogArguments(arguments))
}

module.exports.info = function () {
  logger.info.apply(logger, formatLogArguments(arguments))
}

module.exports.warn = function () {
  logger.warn.apply(logger, formatLogArguments(arguments))
}

module.exports.error = function () {
  logger.error.apply(logger, formatLogArguments(arguments))
}

module.exports.stream = logger.stream

/**
 * Attempts to add file and line number info to the given log arguments.
 */
function formatLogArguments (args) {
  args = Array.prototype.slice.call(args)

  var stackInfo = getStackInfo(1)

  if (stackInfo) {
    // get file path relative to project root
    var calleeStr = '(' + stackInfo.relativePath + ':' + stackInfo.line + ')'

    if (typeof (args[0]) === 'string') {
      args[0] = calleeStr + ' ' + args[0]
    } else {
      args.unshift(calleeStr)
    }
  }

  return args
}

/**
 * Parses and returns info about the call stack at the given index.
 */
function getStackInfo (stackIndex) {
  // get call stack, and analyze it
  // get all file, method, and line numbers
  var stacklist = (new Error()).stack.split('\n').slice(3)

  // stack trace format:
  // http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
  // do not remove the regex expresses to outside of this method (due to a BUG in node.js)
  var stackReg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/gi
  var stackReg2 = /at\s+()(.*):(\d*):(\d*)/gi

  var s = stacklist[stackIndex] || stacklist[0]
  var sp = stackReg.exec(s) || stackReg2.exec(s)

  if (sp && sp.length === 5) {
    return {
      method: sp[1],
      relativePath: path.relative(PROJECT_ROOT, sp[2]),
      line: sp[3],
      pos: sp[4],
      file: path.basename(sp[2]),
      stack: stacklist.join('\n')
    }
  }
}
于 2019-06-13T07:23:32.563 に答える