Express Morgan不将日志写入文件或STDOUT [英] Express Morgan not writing logs to file or STDOUT

查看:229
本文介绍了Express Morgan不将日志写入文件或STDOUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Express应用程序,我想向其中添加日志记录.我安装了Morgan,并在管理我的API调用的文件中设置了中间件:

I have an Express application that I would like to add logging to. I installed Morgan and set up the middleware in the file which manages my API calls:

/src/api/calls.js

摩根中间件已设置为将日志记录结果发送到文件:errors.txt

the morgan middleware is set up to send the logging results to a file: errors.txt

    var express = require('express'),
        app = express(),
        dbConfig = require('../config/db.config'),
        connectToMongo = require('./db.js'),
        bodyParser = require("body-parser"),
        ObjectId = require('mongodb').ObjectID,
        morgan = require('morgan'),
        fs = require('fs'),
        Sb;

    //middleware
    var accessLogStream = fs.createWriteStream('../sample/errors.txt', {flags: 'a'})
    app.use(morgan('combined',  {stream: accessLogStream}));

    app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
      next();
    });
    app.use(bodyParser.urlencoded({ extended: true }));

module.exports = function apiRoutes(app) {
  app.get("/v1/api/sample/:call", function(req, res, next) {
     //route db calls and code here
  });
}

/app.js

需要将api路由输入到app.js文件中,并且将apiRoutes函数称为:

the api routes are required into the app.js file and the apiRoutes function is called:

var express = require('express');
var app = express();
var config = require('./src/config/server.config');
//api and server
var http = require('http');
var server = new http.Server(app);

//load the api calls for the server
var apiRoutes = require('./src/api/calls.js');
apiRoutes(app);

问题是-将代码更改为STDOUT时,我没有将任何日志记录数据写入我的errors.txt文件甚至控制台中.我在这里想念什么?

the problem is - I'm not getting any logging data written into my errors.txt file or even in the console when I change the code to simply STDOUT. What am I missing here?

推荐答案

我想出了这一点-所需的日志记录代码位于app.js文件中,而不是在包含api调用的文件中.我知道日志记录代码必须是中间件的第一部分,但是我没有意识到在实例化服务器的新实例之后它就必须是正确的.

I figured it out- the logging code needed to be located in the app.js file, not in the file that holds the api calls. I knew the logging code needed to be the first piece of middleware, but I didn't realize it needed to be right after the new instance of the server is instantiated.

var express = require('express'),
    app = express(),
    config = require('./src/config/server.config'),
    morgan = require('morgan'),
    fs = require('fs'),
//api and server
    http = require('http'),
    server = new http.Server(app); 

//set up the logger
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
app.use(morgan('combined',  {"stream": accessLogStream}));


//load the api calls for the server
var apiRoutes = require('./src/api/calls.js');
apiRoutes(app);

这篇关于Express Morgan不将日志写入文件或STDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆