首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >nodejs日志文件在哪里?

nodejs日志文件在哪里?
EN

Stack Overflow用户
提问于 2012-05-30 19:17:32
回答 2查看 240.8K关注 0票数 128

我找不到存放nodejs日志文件的地方。因为在我的节点服务器中我有“分段故障”,所以我想查看日志文件中的其他信息...

EN

回答 2

Stack Overflow用户

发布于 2017-01-07 05:32:54

如果你在你的开发中使用docker,你可以在另一个shell中完成这项工作: docker attach running_node_app_container_name

这将向您显示STDOUT和STDERR。

票数 8
EN

Stack Overflow用户

发布于 2018-10-31 14:43:36

对于nodejs日志文件,您可以使用winston和morgan,而不是console.log()语句、user winston.log()或其他winston方法来记录日志。要使用winston和morgan,您需要使用npm安装它们。例子: npm i摩根,winston npm i -S -S

然后,在项目中创建一个名为winston的文件夹,然后在该文件夹中创建一个config.js,并复制下面给出的代码。

代码语言:javascript
复制
const appRoot = require('app-root-path');
const winston = require('winston');

// define the custom settings for each transport (file, console)
const options = {
  file: {
    level: 'info',
    filename: `${appRoot}/logs/app.log`,
    handleExceptions: true,
    json: true,
    maxsize: 5242880, // 5MB
    maxFiles: 5,
    colorize: false,
  },
  console: {
    level: 'debug',
    handleExceptions: true,
    json: false,
    colorize: true,
  },
};

// instantiate a new Winston Logger with the settings defined above
let logger;
if (process.env.logging === 'off') {
  logger = winston.createLogger({
    transports: [
      new winston.transports.File(options.file),
    ],
    exitOnError: false, // do not exit on handled exceptions
  });
} else {
  logger = winston.createLogger({
    transports: [
      new winston.transports.File(options.file),
      new winston.transports.Console(options.console),
    ],
    exitOnError: false, // do not exit on handled exceptions
  });
}

// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
  write(message) {
    logger.info(message);
  },
};

module.exports = logger;

复制完上面的代码后,创建一个带有名称日志的文件夹,使其与winston平行,或者在您想要的任何地方创建一个文件夹,并在该日志文件夹中创建一个文件app.log。返回config.js并将第5行"filename:${appRoot}/logs/app.log“中的路径设置为您创建的相应app.log。

在此之后,转到您的index.js并在其中包含以下代码。

代码语言:javascript
复制
const morgan = require('morgan');
const winston = require('./winston/config');
const express = require('express');
const app = express();
app.use(morgan('combined', { stream: winston.stream }));

winston.info('You have successfully started working with winston and morgan');
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10815218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档