CartoDB-SQL-API/app/services/logger.js

71 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-05-23 23:24:48 +08:00
'use strict';
const bunyan = require('bunyan');
2018-06-08 16:59:34 +08:00
class Logger {
2018-05-23 23:24:48 +08:00
constructor (path, name) {
const env = process.env.NODE_ENV;
const logLevel = process.env.LOG_LEVEL;
2018-05-23 23:24:48 +08:00
const stream = {
level: logLevel ? logLevel : (env === 'test') ? 'fatal' : (env === 'development') ? 'debug' : 'info'
2018-05-23 23:24:48 +08:00
};
if (path) {
stream.path = path;
} else {
stream.stream = process.stdout;
}
this.path = path;
this.logger = bunyan.createLogger({
name,
streams: [stream]
});
}
fatal (...args) {
this.logger.fatal(...args);
2018-05-23 23:24:48 +08:00
}
error (...args) {
this.logger.error(...args);
}
warn (...args) {
this.logger.warn(...args);
}
info (...args) {
this.logger.info(...args);
}
debug (...args) {
this.logger.debug(...args);
}
trace (...args) {
this.logger.trace(...args);
2018-05-23 23:24:48 +08:00
}
2018-06-19 00:47:12 +08:00
reopenFileStreams () {
this.logger.reopenFileStreams();
}
2019-04-05 00:15:21 +08:00
// Ensures that the writable stream is flushed.
// Use this function before exiting the process to not lose log entries
//
// See: https://github.com/trentm/node-bunyan/issues/37
// See: https://github.com/trentm/node-bunyan/issues/73
2019-04-05 00:21:49 +08:00
end (callback) {
2019-04-05 00:15:21 +08:00
// process.stdout cannot be closed
if (!this.path) {
2019-04-05 00:21:49 +08:00
return callback();
2019-04-05 00:15:21 +08:00
}
2019-04-05 00:21:49 +08:00
this.logger.streams[0].stream.on('finish', callback);
2019-04-05 00:15:21 +08:00
this.logger.streams[0].stream.end(); // close stream, flush buffer to disk
}
2018-05-23 23:24:48 +08:00
}
2018-06-08 16:59:34 +08:00
module.exports = Logger;