node-microsvc-lib
Version:
NodeJS microservice framework library
89 lines • 3.4 kB
JavaScript
/**
* Created by pedrosousabarreto@gmail.com on 21/May/2019.
*/
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BunyanLogger = void 0;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const Bunyan = __importStar(require("bunyan"));
const PrettyStream = require("bunyan-prettystream");
class BunyanLogger {
constructor(svc_configs, log_file_path) {
log_file_path = path.resolve(log_file_path); // convert to absolute
fs.mkdirSync(path.dirname(log_file_path), { recursive: true });
// const parts = path.dirname(log_file_path).split(path.sep);
// // For every part of our path, call our wrapped mkdirSync()
// // on the full path until and including that part
// for (let i = 1; i <= parts.length; i++) {
// try {
// fs.mkdirSync(path.join.apply(null, parts.slice(0, i)))
// } catch (e) {
// debugger;
// }
// }
let pretty_stdout = new PrettyStream();
pretty_stdout.pipe(process.stdout);
this._logger = Bunyan.createLogger({
name: svc_configs.app_name,
instance_id: svc_configs.instance_id,
env: svc_configs.env,
app_version: svc_configs.app_version,
streams: [
{
level: 'trace',
type: 'raw',
stream: pretty_stdout
}, {
level: "debug",
type: "rotating-file",
path: log_file_path,
period: "3h",
count: 4 // keep 12h of backups
}
]
});
}
create_child(attrs) {
if (!attrs)
attrs = {};
// @ts-ignore
return this._logger.child(attrs, false);
}
debug(message, ...optionalParams) {
this._logger.debug.apply(this._logger, [message, ...optionalParams]);
}
info(message, ...optionalParams) {
this._logger.info.apply(this._logger, [message, ...optionalParams]);
}
warn(message, ...optionalParams) {
this._logger.warn.apply(this._logger, [message, ...optionalParams]);
}
error(message, ...optionalParams) {
this._logger.error.apply(this._logger, [message, ...optionalParams]);
}
fatal(message, ...optionalParams) {
this._logger.fatal.apply(this._logger, [message, ...optionalParams]);
}
}
exports.BunyanLogger = BunyanLogger;
//# sourceMappingURL=bunyan_logger.js.map