Node.js, logging with Pino: warning 'Possible EventEmitter memory leak detected' - node.js

To register any events in my project, I use pino. This is my 'Logger.js' file:
require('dotenv').config();
const pino = require('pino');
const logger = (
name,
level = (process.env.LOG_LEVEL || 'info'),
file = process.env.LOG_FILE || './log.log',
) => pino({
name,
level,
transport:
{
targets: [
{ // то screen
target: 'pino-pretty',
level,
options:
{
colorize: true,
translateTime: true,
sync: true,
},
},
{ // то file
target: 'pino-pretty',
level,
options:
{
colorize: false,
translateTime: true,
sync: true,
destination: file,
},
},
],
},
});
module.exports = logger;
Then I use this logger in any classes. For example, such as this class:
const logger = require('./Logger')('User');
class testClass1 {
constructor(a) {
this.a = a;
}
async increase(x) {
logger.debug(`x=${x}`);
this.a += x;
return this.a
}
}
module.exports = testClass1;
Classes can be used in project files, for example:
const testClass1 = require('./testClass1');
async function test() {
const test1 = new testClass1(2);
test1.increase(2);
}
test();
Everything works well. But if the number of used classes with logger is more than 10, I have a warning: Possible EventEmitter memory leak detected.
What can be done to avoid this?
Maybe there are some recommendations on how to keep a log?

Your logger.js file exports a function that creates a new logger.
I would try to create only one logger and if needed I would create child loggers for uses like the User.js file you have

Related

How to sent log to Jaeger in NestJs?

In my nestjs project, I am using Winston as a logger. here is the example of my log.info:
logger.log(
'Error: test',
inout_data,
);
here is the logger:
var winston = require('winston');
const logger = winston.createLogger({
level: 'verbose',
format: winston.format.json(),
transports: [
new winston.transports.Console({
format: winston.format.json(),
}),
],
});
export default logger;
this is how I setup the Jaeger:
import { NodeSDK, tracing } from '#opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '#opentelemetry/auto-instrumentations-node';
import { JaegerExporter, ExporterConfig } from '#opentelemetry/exporter-jaeger';
import { BatchSpanProcessor } from '#opentelemetry/sdk-trace-base';
import { Resource } from '#opentelemetry/resources';
import { SemanticResourceAttributes } from '#opentelemetry/semantic-conventions';
interface Tag {
key: string;
value: TagValue;
}
declare type TagValue = string | number | boolean;
const options = {
tags: [], // optional
host: 'localhost',
port: 6832, // optional
maxPacketSize: 65000,
};
const exporter = new JaegerExporter(options);
export const sdk = new NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'test,
[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: '2',
}),
traceExporter: exporter,
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
I want to send the log to Jaeger, but I can not find a way to connect them. Am I missing anything here?

Mock fs function with jest

First of all, I'm new to es6 and jest.
I have a Logger class for instantiate winston and I would like to test it.
Here my code :
const winston = require('winston');
const fs = require('fs');
const path = require('path');
const config = require('../config.json');
class Logger {
constructor() {
Logger.createLogDir(Logger.logDir);
this.logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new (winston.transports.Console)({
format: winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.simple(),
),
}),
new (winston.transports.File)({
filename: path.join(Logger.logDir, '/error.log'),
level: 'error',
}),
new (winston.transports.File)({
filename: path.join(Logger.logDir, '/info.log'),
level: 'info',
}),
new (winston.transports.File)({
filename: path.join(Logger.logDir, '/combined.log'),
}),
],
});
}
static get logDir() {
return (config.logDir == null) ? 'log' : config.logDir;
}
static createLogDir(logDir) {
if (!fs.existsSync(logDir)) {
// Create the directory if it does not exist
fs.mkdirSync(logDir);
}
}
}
exports.logger = new Logger().logger;
export default new Logger();
I would like to test my function createLogDir().
I my head, I think it's a good idea to test the state of fs.existsSync.
If fs.existsSync return false, fs.mkdirSync must be called.
So I try to write some jest test :
describe('logDir configuration', () => {
test('default path must be used', () => {
const logger = require('./logger');
jest.mock('fs');
fs.existsSync = jest.fn();
fs.existsSync.mockReturnValue(false);
const mkdirSync = jest.spyOn(logger, 'fs.mkdirSync');
expect(mkdirSync).toHaveBeenCalled();
});
});
However, I've got an error :
● logDir configuration › default path must be used
Cannot spy the fs.mkdirSync property because it is not a function; undefined given instead
18 | fs.existsSync = jest.fn();
19 | fs.existsSync.mockReturnValue(true);
> 20 | const mkdirSync = jest.spyOn(logger, 'fs.mkdirSync');
21 | expect(mkdirSync).toHaveBeenCalled();
22 | });
23 | });
at ModuleMockerClass.spyOn (node_modules/jest-mock/build/index.js:590:15)
at Object.test (src/logger.test.js:20:28)
Can you help me to debug and test my function please ?
Regards.
The error there is because it is looking for a method called fs.mkdirSync on your logger object, which doesn't exist. If you had access to the fs module in your test then you would spy on the mkdirSync method like this:
jest.spyOn(fs, 'mkdirSync');
However, I think you need to take a different approach.
Your createLogDir function is a static method - meaning that it can only be called on the class, and not on an instance of that class (new Logger() is an instance of the class Logger). Therefore, in order to test that function you need to export the class and not an instance of it, i.e.:
module.exports = Logger;
Then you could have the following tests:
const Logger = require('./logger');
const fs = require('fs');
jest.mock('fs') // this auto mocks all methods on fs - so you can treat fs.existsSync and fs.mkdirSync like you would jest.fn()
it('should create a new log directory if one doesn\'t already exist', () => {
// set up existsSync to meet the `if` condition
fs.existsSync.mockReturnValue(false);
// call the function that you want to test
Logger.createLogDir('test-path');
// make your assertion
expect(fs.mkdirSync).toHaveBeenCalled();
});
it('should NOT create a new log directory if one already exists', () => {
// set up existsSync to FAIL the `if` condition
fs.existsSync.mockReturnValue(true);
Logger.createLogDir('test-path');
expect(fs.mkdirSync).not.toHaveBeenCalled();
});
Note: it looks like you're mixing CommonJS and es6 module syntax (export default is es6) - I would try to stick to one or the other

How are you supposed to create Winston logger stream for Morgan in TypeScript

What is the correct way to create a winston logger in TypeScript that will log the express Morgan middleware logging? I found a number of JavaScript samples but have had trouble converting them over to TypeScript, because I get an error Type '{ write: (message: string, encoding: any) => {}; logger: any; }' is not assignable to type '(options?: any) => ReadableStream'. Object literal may only specify known properties, and 'write' does not exist in type '(options?: any) => ReadableStream'.
Here is my code:
import { Logger, transports } from 'winston';
// http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
// https://www.loggly.com/ultimate-guide/node-logging-basics/
const logger = new Logger({
transports: [
new (transports.Console)({
level: process.env.NODE_ENV === 'production' ? 'error' : 'debug',
handleExceptions: true,
json: false,
colorize: true
}),
new (transports.File)({
filename: 'debug.log', level: 'info',
handleExceptions: true,
json: true,
colorize: false
})
],
exitOnError: false,
});
if (process.env.NODE_ENV !== 'production') {
logger.debug('Logging initialized at debug level');
}
// [ts]
// Type '{ write: (message: string, encoding: any) => {}; logger: any; }' is not assignable to type '(options?: any) => ReadableStream'.
// Object literal may only specify known properties, and 'write' does not exist in type '(options?: any) => ReadableStream'.
logger.stream = {
write: function (message: string, encoding: any) {
logger.info(message);
};
}
export default logger;
I have been able to work around this by adjusting my code to use const winston = require('winston'); but would like to know how you are supposed to do this maintaining types?
Ultimately I ended up with this as a solution. I created a class with one method called write
export class LoggerStream {
write(message: string) {
logger.info(message.substring(0, message.lastIndexOf('\n')));
}
}
then when adding to express I created an instance of the class:
app.use(morgan('combined', { stream: new LoggerStream() }));
This works well for my situation
stream is expected to be factory function that returns a a stream, not a stream itself.
A stream is expected to be a real readable stream, not an object that mimics it.
Since it is supposed to be writable as well, it should be a duplex:
logger.stream = (options?: any) => new stream.Duplex({
write: function (message: string, encoding: any) {
logger.info(message);
}
});
This is a solution that is suggested by Winston TS types. I cannot confirm if it works correctly.
Thanks to #estus who got me past where I was hung up. Here is the solution I ended up using:
import { Logger, transports } from 'winston';
import stream from 'stream';
import split from 'split';
// http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
// https://www.loggly.com/ultimate-guide/node-logging-basics/
const logger = new Logger({
transports: [
new (transports.Console)({
level: process.env.NODE_ENV === 'production' ? 'error' : 'debug',
handleExceptions: true,
json: false,
colorize: true
}),
new (transports.File)({
filename: 'debug.log', level: 'info',
handleExceptions: true,
json: true,
colorize: false
})
],
exitOnError: false,
});
if (process.env.NODE_ENV !== 'production') {
logger.debug('Logging initialized at debug level');
}
logger.stream = split().on('data', function (message: string) {
logger.info(message);
});
export default logger;
Ultimately this issue got me to the final solution - https://github.com/expressjs/morgan/issues/70
If you're using TypeScript classes for logger, you can declare a stream() function that returns a StreamOptions type from morgan:
import { StreamOptions } from 'morgan';
//...rest of the class code
public stream(): StreamOptions {
return {
write: (message: string): void => {
this.info(message.trim());
}
};
Then you can use this stream function inside express middleware:
app.use('combined', { stream: this.logger.stream()})

How to add session id to each log in winston logger

In my node application I'm using winston module to store my application logs. I am getting the log as:
2017-11-22T07:16:38.632Z - info: asset type is attached successfully
Now I want to add sessionID after timestamp. I want my log as:
2017-11-22T07:16:38.632Z -**sessionId here**- info: asset type is attached successfully.
My code which I used for winston logging is:
var winston = require('winston');
require('winston-daily-rotate-file');
const levels = {
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6,
trace: 7
};
var transport = new (winston.transports.DailyRotateFile)({
filename: 'logs/./log',
datePattern: 'yyyy-MM-dd.',
prepend: true,
json: false,
level: process.env.ENV === 'development' ? 'debug' : 'info'
});
var logger = new (winston.Logger)({
levels: levels,
transports: [
transport
]
});
module.exports = logger;
You have to custom the log format https://github.com/winstonjs/winston/tree/2.x#custom-log-format
First update your transport :
var transport = new (winston...
...
level: process.env.ENV === 'development' ? 'debug' : 'info',
timestamp: () => {
let today = new Date();
return today.toISOString();
},
formatter: options => `${options.timestamp()} -${options.meta.sessionId}- ${options.level}: ${options.message}`
});
Then, just pass the session ID to your logger meta feature :
logger.info('asset type is attached successfully', {sessionId: 'mySessionID'});
and you get
2017-11-22T14:13:17.697Z -mySessionID- info: asset type is attached successfully
Edit : instead of exporting only the winston.logger object, we export an object which requires a sessionId as a parameter, and contains the winston.logger. We also update the transport, so we customize its formatter property in the new Logger object. In the formatter property, we replace the meta declaration with the new this.sessionId variable, so we don't use the meta property anymore.
logger.js :
var transport = new (winston...
...
level: process.env.ENV === 'development' ? 'debug' : 'info',
timestamp: () => {
let today = new Date();
return today.toISOString();
}
});
class Logger {
constructor(session) {
this.sessionId = session;
this.transport = transport;
this.transport.formatter = options => `${options.timestamp()} -${this.sessionId}- ${options.level}: ${options.message}`;
this.logger = new (winston.Logger)({
levels: levels,
transports: [this.transport]
});
}
}
module.exports = Logger;
server.js :
const Logger = require('./logger.js');
let logman = new Logger('my_session_id');
let logger = logman.logger;
logger.info('asset type is attached successfully');
2017-11-23T13:13:08.769Z -my_session_id- info: asset type is attached successfully
I had the same request and wasn't wild about the solution... I liked the way the standard logger was formatting logs and didn't want to reinvent that wheel. Also, I was hoping for something more compact and I think I found it. I'm not a javascript programmer, so it might not be good coding practice, but it seems to work well for me...
server.js
//Initiate winston logging please
const logger = require('winston');
const common = require('winston/lib/winston/common');
function myFormat(options) {
options.formatter = null
options.label = helpers.getSessionId(logger.req)
return common.log(options);
}
var consoleLoggingConfig = {
timestamp: true,
level: process.env.LOG_LEVEL ? process.env.LOG_LEVEL : "info",
handleExceptions: true,
humanReadableUnhandledException: true,
formatter: myFormat
}
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, consoleLoggingConfig)
logger.info('Winston logging initiated', consoleLoggingConfig)
//Helper to ensure that logger has access to the request object to obtain the session id
app.use(helpers.attachReqToLogger)
module.exports=logger
helpers.js
// Helper to ensure that the winston logger has the request object on it to obtain the sessionId
helpers.attachReqToLogger = function(req, res, next) {
logger.req = req
next();
}
import winston from "winston";
import { v4 as uuidv4 } from "uuid";
const { format } = winston;
const commonFormat = [format.timestamp(), format.json()];
const withId = [
format.printf(({ message, id, ...rest }) => {
return JSON.stringify(
{
// if frontend privide id, use it, else create one
id: id ?? uuidv4(),
message,
...rest,
},
null,
// prettyPrint seems will overload printf's output
// so i mannually do this
4,
);
}),
];
export const logger = winston.createLogger({
level: "debug",
format: format.combine(...commonFormat, ...withId),
// ...

Add module name in winston log entries

Is there a possibility to save the current module name in order to be printed automatically in winston log entries when they are called later?
Currently, when I want to print the module name in logs, I have to add it manually:
var logHeader = 'mymodule'
log.info(logHeader + 'Hello')
For example, with debug, you can do (ignore the log format feature %s for now):
var debug = require('debug')('http')
, name = 'My App'
debug('booting %s', name);
This will prin http prefix before the log:
http booting My App
Can this be done in winston? I have searched in the documentation but I couldn't find anything relevant.
I found a better way to do this.
I added an additional layer over the winston logger, in this case a function, that keeps the module name for each module that needs the logger. So when a module requires my new logger, it actually calls the exported function with the module name, in this case __filename.
log.js
var winston = require('winston')
var winstonLogger = new (winston.Logger)({
transports: [
new (winston.transports.File) ({
filename: 'MyLogs.txt',
handleExceptions: true,
humanReadableUnhandledException: true,
level: 'info',
timestamp: true,
json: false
}),
new (winston.transports.Console) ({
level: 'info',
prettyPrint: true,
colorize: true,
timestamp: true
})
]
})
module.exports = function(fileName) {
var myLogger = {
error: function(text) {
winstonLogger.error(fileName + ': ' + text)
},
info: function(text) {
winstonLogger.info(fileName + ': ' + text)
}
}
return myLogger
}
module1.js
var log = require('log')(__filename)
log.info('Info log example')
info: C:\Users\user1\project\module1.js: Info log example
module2.js
var log = require('log')(__filename)
log.error('Error log example')
error: C:\Users\user1\project\module2.js: Error log example
Also, this way, I didn't need to change anywhere the way I submit a text log; log.info('text') stays exactly like before.
This is what Child Loggers are for. They might not have been available when this question was asked, but for future reference, you can create your main logger instance and then export a function that creates a child logger with some default options.
// logging.js
const winston = require('winston')
const logger = winston.createLogger({
transports: [
new winston.transports.Console({
format: winston.format.printf(options => {
// you can pass any custom variable in options by calling
// logger.log({level: 'debug', message: 'hi', moduleName: 'my_module' })
return `[${options.moduleName}] ${options.level}: ${options.message}$`;
})
})
]
});
module.exports = function(name) {
// set the default moduleName of the child
return logger.child({moduleName: name});
}
Then at the top of each module, import the child using:
// my_module.js
const logger = require('./logging.js')('my_module_name');
logger.error('computer says no');
// output:
// [my_module_name] error: computer says no
You can specify a custom log format with Winston -
var moduleName = 'myModule';
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
formatter: function(options) {
// Return string will be passed to logger.
return moduleName + ' - ' + (options.message ? options.message : '')
}
})
]
});
logger.info('This is a log message');
This will print -
myModule - This is a log message
So your module name will be appended to every log messsage.
With winston v3.3.3 you can do this by using winston.format.label and a customization of winston.format.printf:
const winston = require('winston');
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.label({label: 'mymodule'}),
winston.format.printf(({label, message}) => {
return `${label}: ${message}`;
})
),
transports: [
new winston.transports.Console(),
],
});
logger.info('Hello, World!'); // prints "mymodule: Hello, World!"

Resources