UNPKG

@httpc/kit

Version:

httpc toolbox for building function-based API with minimal code and end-to-end type safety

87 lines (86 loc) 3.9 kB
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 __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); } }; import { singleton } from "tsyringe"; import winston, { format } from "winston"; import { alias, KEY, options } from "../../di"; import { WinstonLogger } from "./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.transports.Console({ format: format.combine( // this._createContext(), process.env.NODE_ENV === "production" ? format.combine() /* noop in production */ : format.colorize(), this._createConsoleFormat(label, lazyProperties)) }); } const logger = winston.createLogger({ defaultMeta: properties, level, transports }); return new WinstonLogger(logger); } // protected _createContext() { // return format((info, opts) => Object.assign(info, opts))({ // environment: process.env.NODE_ENV || "development" // }); // } _createConsoleFormat(label, properties) { return format.combine(format.timestamp(), format.label({ label }), format.splat(), 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([ singleton(), alias(KEY("ILogService")), __param(0, options(undefined)), __metadata("design:paramtypes", [Object]) ], WinstonLogService); export { WinstonLogService };