This is a ready-to-use winston logger and winston-daily-rotate-file with the default configuration.
Almost every time I start a new Node.js project usually I need a logger. So I go to a previous project and copy
logger.js
definition with all my transports and configurations. I don't like to keep a lot of copies of the same code over and over. I create this library to avoid that.
About this ready-to-use default configurations:
All definitions, transports and configurations were chosen for my personal preference and workflow.
You could customize this logger adding your own transports, but if you need a considerable different configuration you should use winston and define at your own.
npm install winston-ready
const logger = require('winston-ready');
logger.error("Show me the money!");
This package is configured using (optional) environment variables:
LOG_NAME
(default app)LOG_STYLE
(posible values: single, rotate, both, none; default rotate)LOG_LEVEL
(default info)LOG_PATH
(default logs/)LOG_DAYS
(default 180d)LOG_DATE_PATTERN
(default YYYY-MM-DD)LOG_CONSOLE
(default on)LOG_CONSOLE_LEVEL
(default debug)
With LOG_STYLE
you can define logging as daily rotate files, just a single one or both.
If you set it as none then no logging strategy is defined and you can set you owns transports.
LOG_LEVEL
and LOG_CONSOLE_LEVEL
use common npm level values.
If you set NODE_ENV=development
or LOG_CONSOLE=on
logger will display information at console.
Otherwise only at files. It is recommended that you turn off LOG_CONSOLE
on production.
// my-logger.js
process.env.LOG_STYLE = 'none';
const { container, winston } = require('winston-ready/container');
container.add('my-own', {
level: 'warn',
format: winston.format.json(),
transports: [new winston.transports.File({ filename: 'my-own.log' })],
});
module.exports = container.get('my-own');
// your-module.js
const logger = require('./my-logger');
logger.error("This is my own logger error!");
// my-logger.js
const { container, winston } = require('winston-ready/container');
container.add('morgan', {
level: 'verbose',
transports: [
new winston.transports.File({
filename: 'access.log',
format: winston.format.json()
})],
});
module.exports = {
logger: container.get('default'),
morgan: container.get('morgan'),
};
// your-module.js
const { logger, morgan } = require('./my-logger');
logger.error("Show me the money!");
morgan.info("HTTP access");