@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
204 lines • 8.66 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (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 (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const path = __importStar(require("path"));
const winston_1 = __importDefault(require("winston"));
const illegal_argument_error_1 = require("../../errors/illegal-argument-error");
const cluster_1 = require("./cluster");
class Logger {
constructor(app) {
this.loggerName = 'default';
this.container = new winston_1.default.Container();
this.defaultChannels = {
console: {
driver: 'console'
}
};
this.defaultChannelName = 'console';
this.defaultDrivers = new Map([
['console', winston_1.default.transports.Console],
['file', winston_1.default.transports.File],
['http', winston_1.default.transports.Http],
['stream', winston_1.default.transports.Stream],
['dailyFile', require('winston-daily-rotate-file')],
]);
this.customDrivers = new Map();
this.defaultFormat = (format) => format.combine(format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}), format.splat(), format.printf((info) => `[${info.timestamp}] [${info.level.toUpperCase()}] - ${info.message}`));
this.app = app;
}
log(level, message, ...meta) {
return this.channel().log(level, message, ...meta);
}
error(message, ...meta) {
return this.channel().error(message, ...meta);
}
warn(message, ...meta) {
return this.channel().warn(message, ...meta);
}
info(message, ...meta) {
return this.channel().info(message, ...meta);
}
debug(message, ...meta) {
return this.channel().debug(message, ...meta);
}
addChannel(channelName, channelConfig) {
if (this.defaultChannels[channelName])
return;
this.defaultChannels[channelName] = channelConfig;
}
getLogger() {
return this.channel();
}
getContainer() {
return this.container;
}
resolveDefaultChannel() {
const defaultChannelName = this.getDefaultChannelName();
return this.resolve(defaultChannelName);
}
isDefaultDriverSupported(defaultDriverName) {
return this.defaultDrivers.has(defaultDriverName);
}
isCustomDriverSupported(customDriverName) {
return this.customDrivers.has(customDriverName);
}
channel(channelName) {
if (channelName)
return this.resolve(channelName);
else
return this.resolveDefaultChannel();
}
resolve(channelName) {
if (!this.container.has(channelName)) {
const formatCall = this.getFormat(channelName);
this.container.add(channelName, {
transports: this.getTransports(channelName),
format: formatCall(winston_1.default.format),
levels: this.getLevels(),
level: this.getLevel(),
});
}
return this.container.get(channelName);
}
getTransports(channelName) {
const _config = this.getChannelConfigure(channelName);
(_config === null || _config === void 0 ? void 0 : _config.format) && delete _config.format;
if (!_config)
throw new illegal_argument_error_1.IllegalArgumentError(`Logger channel [${channelName}] is not defined.`);
const { driver: driverName } = _config, restOpts = __rest(_config, ["driver"]);
if (this.app.isCluster && this.app.isWorker) {
return [new Logger.Cluster(this.loggerName, channelName, this.app, restOpts)];
}
if (this.isComposeChannel(_config)) {
return this.composeDriverCreator(_config);
}
if (this.isCustomDriverSupported(driverName)) {
return this.callCustomDriverCreator(_config);
}
if (this.isDefaultDriverSupported(driverName)) {
switch (driverName) {
case 'file':
return this.defaultDriverCreator(Object.assign(Object.assign({}, _config), { filename: path.resolve(this.app.appPath, '../../logs', _config.filename || 'app.log') }));
case 'dailyFile':
return this.defaultDriverCreator(Object.assign(Object.assign({}, _config), { filename: path.resolve(this.app.appPath, '../../logs', _config.filename || '%DATE%.log') }));
default:
return this.defaultDriverCreator(_config);
}
}
throw new illegal_argument_error_1.IllegalArgumentError(`Logger Driver [${driverName}] is not supported.`);
}
getFormat(channelName) {
var _a;
const topFormat = this.app.get('config').get('logger.format');
const channelFormat = this.app.get('config').get(`logger.channels.${channelName}.format`);
const defaultChanellFormat = (_a = this.defaultChannels[channelName]) === null || _a === void 0 ? void 0 : _a.format;
return channelFormat || topFormat || defaultChanellFormat || this.defaultFormat;
}
getLevels() {
return this.app.get('config').get('logger.levels');
}
getLevel() {
return this.app.get('config').get('logger.level', 'info');
}
getChannelConfigure(channelName) {
var _a;
const cc = this.app.get('config').get(`logger.channels.${channelName}`, {});
const dc = (_a = this.defaultChannels[channelName]) !== null && _a !== void 0 ? _a : {};
return Object.assign(Object.assign({}, dc), cc);
}
getDefaultChannelName() {
return this.app.get('config').get('logger.default', this.defaultChannelName);
}
isComposeChannel(channel) {
const { driver } = channel;
return driver === 'compose';
}
callCustomDriverCreator(config) {
var _a;
const { driver: driverName } = config, restOpts = __rest(config, ["driver"]);
const [DriverInstance, defaultOptions] = (_a = this.customDrivers.get(driverName)) !== null && _a !== void 0 ? _a : [];
return [new DriverInstance(Object.assign(Object.assign({}, defaultOptions), restOpts))];
}
addCustomDriver(channelName, Driver, defaultOptions = {}) {
this.customDrivers.set(channelName, [Driver, defaultOptions]);
return this;
}
composeDriverCreator(channel) {
const { channels } = channel;
if (!this.isComposeChannel(channel))
return [];
let res = [];
for (const _channel of channels) {
const transports = this.getTransports(_channel);
res = res.concat(transports);
}
return res;
}
defaultDriverCreator(options) {
const { driver: driverName } = options, restOpts = __rest(options, ["driver"]);
const DriverInstance = this.defaultDrivers.get(driverName);
return [new DriverInstance(restOpts)];
}
}
exports.Logger = Logger;
Logger.Cluster = cluster_1.ClusterTransport;
//# sourceMappingURL=logger.js.map