我正在尝试通过引用
Node.js - 日志记录/使用摩根和温斯顿将
winston
和
morgan
logger.js
中。但是当我运行应用程序时出现以下错误。
/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)
应用程序.js
...
const logger = require('./logger');
...
index.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)
);
logger.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;
Logger.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()
。