ve-logger
Version:
A Node.js logger that creates daily log files compatible with Kibana
183 lines (160 loc) • 4.73 kB
JavaScript
/******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ({
/***/ 124:
/***/ ((module) => {
;
module.exports = require("winston");
/***/ }),
/***/ 233:
/***/ ((module) => {
;
module.exports = require("winston-daily-rotate-file");
/***/ }),
/***/ 896:
/***/ ((module) => {
;
module.exports = require("fs");
/***/ }),
/***/ 928:
/***/ ((module) => {
;
module.exports = require("path");
/***/ }),
/***/ 954:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// File: index.js
const fs = __webpack_require__(896);
const path = __webpack_require__(928);
const {
createLogger,
format,
transports
} = __webpack_require__(124);
const {
combine,
timestamp,
printf,
splat,
json
} = format;
__webpack_require__(233);
class KibanaLogger {
constructor(options = {}) {
this.options = {
logDirectory: options.logDirectory || 'logs',
logLevel: options.logLevel || 'info',
appName: options.appName || 'application',
maxSize: options.maxSize || '20m',
maxFiles: options.maxFiles || '14d',
...options
};
// Ensure log directory exists
fs.mkdirSync(this.options.logDirectory, {
recursive: true
});
// Create standard log transport
const standardLogTransport = new transports.DailyRotateFile({
filename: path.join(this.options.logDirectory, `%DATE%-${this.options.appName}.log`),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: this.options.maxSize,
maxFiles: this.options.maxFiles,
level: this.options.logLevel
});
// Create error log transport
const errorLogTransport = new transports.DailyRotateFile({
filename: path.join(this.options.logDirectory, `%DATE%-${this.options.appName}.errorlog`),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: this.options.maxSize,
maxFiles: this.options.maxFiles,
level: 'error'
});
// Use JSON format for Kibana compatibility
const kibanaFormat = combine(timestamp({
format: 'YYYY-MM-DD HH:mm:ss.SSS'
}), splat(), json());
// Create logger instance
this.logger = createLogger({
level: this.options.logLevel,
format: kibanaFormat,
defaultMeta: {
service: this.options.appName,
environment: this.options.environment || 'development'
},
transports: [standardLogTransport, errorLogTransport, new transports.Console({
format: this._getConsoleFormat()
})]
});
}
_getConsoleFormat() {
return combine(format.colorize(), timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}), printf(({
level,
message,
timestamp,
...metadata
}) => {
let metaStr = '';
if (Object.keys(metadata).length > 0) {
metaStr = JSON.stringify(metadata);
}
return `${timestamp} [${level}]: ${message} ${metaStr}`;
}));
}
// Logger methods
log(level, message, meta = {}) {
this.logger.log(level, message, meta);
}
info(message, meta = {}) {
this.logger.info(message, meta);
}
error(message, meta = {}) {
this.logger.error(message, meta);
}
warn(message, meta = {}) {
this.logger.warn(message, meta);
}
debug(message, meta = {}) {
this.logger.debug(message, meta);
}
}
module.exports = KibanaLogger;
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __webpack_require__(954);
/******/ module.exports = __webpack_exports__;
/******/
/******/ })()
;