@httpc/kit
Version:
httpc toolbox for building function-based API with minimal code and end-to-end type safety
113 lines (112 loc) • 5.25 kB
JavaScript
;
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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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 __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WinstonLogService = void 0;
const tsyringe_1 = require("tsyringe");
const winston_1 = __importStar(require("winston"));
const di_1 = require("../../di");
const WinstonLogger_1 = require("./WinstonLogger");
let WinstonLogService = class WinstonLogService {
constructor(options) {
this.options = options;
}
createLogger(label, properties) {
let lazyProperties;
if (this.options?.properties) {
if (typeof this.options.properties === "function") {
lazyProperties = this.options.properties;
properties = { ...lazyProperties(label, properties), ...properties };
}
else {
properties = { ...this.options.properties, ...properties };
}
}
let { level } = properties || {};
if (!level && this.options?.level) {
if (typeof this.options.level === "string") {
level = this.options.level;
}
else {
level = this.options.level(label, properties);
}
}
if (!level) {
level = process.env.LOG_LEVEL || (process.env.NODE_ENV === "production" ? "info" : "silly");
}
let transports = typeof this.options?.transports === "function"
? this.options.transports(label, properties)
: this.options?.transports;
if (!transports) {
transports = new winston_1.default.transports.Console({
format: winston_1.format.combine(
// this._createContext(),
process.env.NODE_ENV === "production" ? winston_1.format.combine() /* noop in production */ : winston_1.format.colorize(), this._createConsoleFormat(label, lazyProperties))
});
}
const logger = winston_1.default.createLogger({
defaultMeta: properties,
level,
transports
});
return new WinstonLogger_1.WinstonLogger(logger);
}
// protected _createContext() {
// return format((info, opts) => Object.assign(info, opts))({
// environment: process.env.NODE_ENV || "development"
// });
// }
_createConsoleFormat(label, properties) {
return winston_1.format.combine(winston_1.format.timestamp(), winston_1.format.label({ label }), winston_1.format.splat(), winston_1.format.printf(info => {
const { level, timestamp, label, message, ...meta } = info;
if (properties) {
Object.assign(meta, properties(label), meta);
}
let extra = "";
if (Object.keys(meta).length > 0) {
extra = " " + JSON.stringify(meta);
}
return `${info.level}\t${info.timestamp}\t[${info.label}]\t${info.message}${extra}`;
}));
}
};
WinstonLogService = __decorate([
(0, tsyringe_1.singleton)(),
(0, di_1.alias)((0, di_1.KEY)("ILogService")),
__param(0, (0, di_1.options)(undefined)),
__metadata("design:paramtypes", [Object])
], WinstonLogService);
exports.WinstonLogService = WinstonLogService;