gpt4 book ai didi

node.js - Nodejs - TypeError : logger. 信息不是函数

转载 作者:行者123 更新时间:2023-12-05 06:48:35 26 4
gpt4 key购买 nike

我正在尝试通过引用
logger.js 中结合winstonmorgan Node.js - logging / Use morgan and winston .

但是当我运行应用程序时出现以下错误。

/server/src/index.js:12

logger.info('Feathers application started on http://%s:%d', app.get('host'), port)

^


TypeError: logger.info is not a function

at Server.<anonymous> (/server/src/index.js:12:10)

at Server.emit (events.js:327:22)

App.js

...
const logger = require('./logger');
...

索引.js

/* eslint-disable no-console */
const logger = require('./logger');
const app = require('./app');
const port = app.get('port');
const server = app.listen(port);

process.on('unhandledRejection', (reason, p) =>
logger.error('Unhandled Rejection at: Promise ', p, reason)
);

server.on('listening', () =>
logger.info('Feathers application started on http://%s:%d', app.get('host'), port)
);

记录器.js

const { createLogger, format, transports } = require('winston');

// Configure the Winston logger. For the complete documentation see https://github.com/winstonjs/winston
const logger = createLogger({
// To see more detailed errors, change this to 'debug'
level: 'info',
format: format.combine(
format.splat(),
format.simple()
),
transports: [
new transports.Console(),
new transports.File({ filename: 'log-file.log' })
],
exitOnError: false
});

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

console.log(logger.stream)

module.exports = (require("morgan")("combined", { stream: logger.stream }));

根据评论更新了代码。
现在没有错误,但是当我通过 rest api 发送 http 请求时没有日志记录。

const path = require('path');
const favicon = require('serve-favicon');
const compress = require('compression');
const helmet = require('helmet');
const cors = require('cors');
const logger = require('./logger');

const feathers = require('@feathersjs/feathers');
const configuration = require('@feathersjs/configuration');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');


const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');
const channels = require('./channels');

const authentication = require('./authentication');

const sequelize = require('./sequelize');

const app = express(feathers());

// Load app configuration
app.configure(configuration());
// Enable security, CORS, compression, favicon and body parsing
app.use(helmet({
contentSecurityPolicy: false
}));
app.use(cors());
app.use(compress());
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ limit: '10mb', extended: true }));
app.use(favicon(path.join(app.get('public'), 'favicon.ico')));
// Host the public folder
app.use('/', express.static(app.get('public')));

// Set up Plugins and providers
app.configure(express.rest());
app.configure(socketio());

app.configure(sequelize);

// Configure other middleware (see `middleware/index.js`)
app.configure(middleware);
app.configure(authentication);
// Set up our services (see `services/index.js`)
app.configure(services);
// Set up event channels (see channels.js)
app.configure(channels);

// Configure a middleware for 404s and the error handler
app.use(express.notFound());

app.use(require("morgan")("combined", { stream: logger.stream })); //added here

app.hooks(appHooks);

module.exports = app;

记录器.js

const { createLogger, format, transports } = require('winston');

// Configure the Winston logger. For the complete documentation see https://github.com/winstonjs/winston
const logger = createLogger({
// To see more detailed errors, change this to 'debug'
level: 'info',
format: format.combine(
format.splat(),
format.simple()
),
transports: [
new transports.Console(),
new transports.File({ filename: 'log-file.log' })
],
exitOnError: false
});

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

module.exports = logger

最佳答案

您导出的是 Morgan 实例(这是一个中间件函数),而不是 Winston 记录器实例。因此,您从 require('./logger') 获得的对象不是 Logger,并且它没有 .info() 方法。

您应该改为导出记录器,并将其包装在 Morgan 中,仅用于传递给 app.use()

关于node.js - Nodejs - TypeError : logger. 信息不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66797425/

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