Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daily log file won't save if next function is process.exit #114

Open
fliespl opened this issue Apr 19, 2020 · 4 comments
Open

Daily log file won't save if next function is process.exit #114

fliespl opened this issue Apr 19, 2020 · 4 comments

Comments

@fliespl
Copy link

fliespl commented Apr 19, 2020

Basic example would be as follows. In this case - nothing get saved if using:

    let logger = tracer.dailyfile({
        root: 'logs',
        maxLogFiles: 30,
        logPathFormat: '{{root}}/{{date}}.log',
    });
var pool  = mysql.createPool(mysqlConfig);

pool.query('SELECT 1', function(err, data) {
    if(err) {
          logger.error(err);
          process.exit(1);
    }
});
@SufianBabri
Copy link
Contributor

This is surely because the process gets killed before the log file is written.

I don't know if what you're trying to is possible by using dailyfile, but maybe if you explain your use-case, someone may suggest an alternative way of doing it.

@SufianBabri
Copy link
Contributor

Alright, I get what you are trying to achieve.

What you want is a callback that could be invoked after the log file has been written, e.g. in case of uncaught exception, you want it logged and the process killed afterwards.

I don't see any way to do this at the moment. I tried to set a custom transport method but it probably gets invoked before the file is written and I wasn't able to get the file to which the data needs to be written.

This is what I came up with:

const exceptionsLogger = tracer.dailyfile({
	root: 'logs',
	allLogsFileName: 'daily-exceptions-log',
	inspectOpt: {
		showHidden: false,
		depth: null
	},
	maxLogFiles: 90,
	transport: (data: LogOutput) => {
		const filePath = path.join(data.folder, data.file);

		// the 'data' contains the path of file where I have this method
		console.log('data', data);

		// fs.appendFileSync(filePath, 'some data');
		process.exit(1);
	}
});

I think it will be good if we could set a callback to the tracer.dailyfile.

@fliespl
Copy link
Author

fliespl commented Apr 25, 2020

Hi @SufianBabri,
thanks much.

Currently I have made a quick workaround, by just calling exit after 100ms timeout. It gives time for log to finish writing. I think that adding option to LogOutput to force writing in sync mode would resolve it.

@fliespl
Copy link
Author

fliespl commented Jan 4, 2021

I am returning with this :)

How about allowing us to specicying callback parameter to "write" method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants