I am trying to combine winston
and morgan
in logger.js
by Referencing
Node.js - logging / Use morgan and winston .
But below Error occurs when I run the application.
/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');
...
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 }));
Updated the code according to comment.
No error now but no logging appear when I send http request by rest api.
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