gpt4 book ai didi

Express Morgan not writing logs to file or STDOUT(Express Morgan未将日志写入文件或STDOUT)

转载 作者:bug小助手 更新时间:2023-10-22 16:11:36 31 4
gpt4 key购买 nike



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:

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



/src/api/calls.js

/src/api/calls.js



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

morgan中间件被设置为将日志记录结果发送到一个文件: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

/app.js



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

需要将api路由放入app.js文件中,并调用apiRoutes函数:



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);


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?

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


更多回答
优秀答案推荐

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.

我想明白了——日志记录代码需要位于app.js文件中,而不是保存api调用的文件中。我知道日志记录代码需要成为中间件的第一部分,但我没有意识到它需要在服务器的新实例实例化后立即出现。



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);


In case you are running/debugging your Node code from VS Code, then this problem is caused by VS Code.

如果您正在从VS代码运行/调试Node代码,则此问题是由VS代码引起的。


Morgan writes to console via process.stdout.write, which is not supported by VS Code's Debug Console. The Debug Console will show only things written via console.log.

Morgan通过process.stdout.write写入控制台,VS Code的调试控制台不支持此操作。调试控制台将只显示通过Console.log编写的内容。


There are two ways to fix this. Both include adding entires to launch.json:

有两种方法可以解决这个问题。两者都包括向launch.json添加entires:



  • first solution - add "console": "integratedTerminal"

  • second solution - add "outputCapture": "std" (will not respect log colors)


I found this solutions here:

我在这里找到了以下解决方案:




when using express, it works for me

使用express时,它对我有效



app.use(morgan('dev'));


In my case, when I moved the following code to the start of the app.js file, then it started working:

在我的例子中,当我将以下代码移动到app.js文件的开头时,它就开始工作了:


app.use(logger('combined'));
app.use(logger('common', {
stream: fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
}));

For some reason, when this was at the beginning of the file:

出于某种原因,当这是在文件的开头时:


app.use(logger('combined'));

And this was at the end:

这是在最后:


app.use(logger('common', {
stream: fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
}));

Then it didn't work. I didn't figure out why.

然后它就不起作用了。我不知道为什么。


更多回答

That is absolutely correct. I got the same issue and your solution saves my day. Thanks!

这是绝对正确的。我遇到了同样的问题,你的解决方案帮了我一天。谢谢

What if you had app.use(morgan('combined', {"stream": accessLogStream})); before new http.Server(app); ?

如果你有app.use(morgan('combined',{“stream”:accessLogStream});在新http之前。服务器(应用程序)?

The solution worked for me, Thanks!! But I didn't understand the reason behind it

解决方案对我有效,谢谢!!但我不明白背后的原因

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com