UNPKG

@ogma/nestjs-module

Version:

A NestJS module for the Ogma logger

191 lines (190 loc) 7.43 kB
"use strict"; 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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OgmaService = void 0; const common_1 = require("@nestjs/common"); const logger_1 = require("@ogma/logger"); const decorators_1 = require("./decorators"); let OgmaService = class OgmaService { constructor(ogma, context, requestContext, traceMethod = 'fine') { this.requestContext = requestContext; this.traceMethod = traceMethod; /** * use Ogma to log at the FINE level * @param message What to print to the Ogma instance * @param context Optional context if you want to change what the original context was */ this.fine = this.verbose; /** * use Ogma to log at the LOG level * @param message What to print to the Ogma instance * @param context Optional context if you want to change what the original context was */ this.log = this.info; this.context = context || ''; this.ogma = ogma !== null && ogma !== void 0 ? ogma : new logger_1.Ogma(); } getRequestId(requestContext) { if (typeof requestContext.getContext !== 'undefined') { return requestContext.getContext().requestId; } return requestContext.requestId; } /** * The [`@ogma/styler`](https://ogma.jaymcdoniel.dev/en/styler) * instance that the logger uses for custom coloring without needing * to manage a new styler instance */ get style() { return this.ogma.style; } /** * Change the log level during runtime * @param the new log level to use */ setLogLevel(level) { this.ogma.setLogLevel(level); return this; } /** * use Ogma to log at the INFO level * @param message What to print to the Ogma instance * @param meta extra metadata for the log */ info(message, meta) { if (typeof meta === 'string') { meta = { context: meta }; } this.printMessage(message, 'info', meta); } /** * use Ogma to log at the ERROR level. You can provide a stack trace as well. * @param message What to print to the Ogma instance * @param meta additional information you can print about the log OR the stack trace to print * @param context a string for the context in which the error log was called. This can be provided as a part of the meta, or as a third parameter to stay in line with Nest's LoggerService */ error(message, trace = '', context = {}) { let meta = {}; if (typeof trace === 'object') { meta = trace; trace = ''; } if (context && typeof context === 'string') { meta.context = context; } else if (context && typeof context === 'object' && Object.keys(context).length) { meta = context; } this.printMessage(message, 'error', meta); if (trace) { this.printMessage(trace, 'error', meta); } } /** * use Ogma to log at the WARN level * @param message What to print to the Ogma instance * @param meta extra metadata for the log */ warn(message, meta) { if (typeof meta === 'string') { meta = { context: meta }; } this.printMessage(message, 'warn', meta); } /** * use Ogma to log at the DEBUG level * @param message What to print to the Ogma instance * @param meta extra metadata for the log */ debug(message, meta) { if (typeof meta === 'string') { meta = { context: meta }; } this.printMessage(message, 'debug', meta); } /** * use Ogma to log at the FATAL level * @param message What to print to the Ogma instance * @param meta extra metadata for the log */ fatal(message, meta) { if (typeof meta === 'string') { meta = { context: meta }; } this.printMessage(message, 'fatal', meta); } /** * use Ogma to log at the SILLY level * @param message What to print to the Ogma instance * @param meta extra metadata for the log */ silly(message, meta) { if (typeof meta === 'string') { meta = { context: meta }; } this.printMessage(message, 'silly', meta); } /** * use Ogma to log at the VERBOSE level * @param message What to print to the Ogma instance * @param meta extra metadata for the log */ verbose(message, meta) { if (typeof meta === 'string') { meta = { context: meta }; } this.printMessage(message, 'verbose', meta); } /** * A configurable method that will print at a different level depending on the configuration * Normally this method will only be called via the `@Log()` decorator, but there's nothing * stopping anyone else from using it too * @param message the message to print * @param meta extra metadata for the logs */ trace(message, meta) { this.printMessage(message, this.traceMethod, meta); } /** * A predefined method for printing errors to the Ogma instance * @param error The error to print. Should be an Error or Exception object * @param meta extra metadata for the log */ printError(error, meta = {}) { var _a; if (!meta.correlationId && this.requestContext && this.context) { meta.correlationId = this.getRequestId(this.requestContext); } meta.context = (_a = meta.context) !== null && _a !== void 0 ? _a : this.context; this.ogma.printError(error, meta); } printMessage(message, levelString, meta = {}) { var _a; meta.context = (_a = meta.context) !== null && _a !== void 0 ? _a : this.context; if (!meta.correlationId && this.requestContext && this.context) { meta.correlationId = this.getRequestId(this.requestContext); } this.ogma[levelString](message, meta); } }; exports.OgmaService = OgmaService; exports.OgmaService = OgmaService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, decorators_1.InjectOgma)()), __param(1, (0, common_1.Optional)()), __param(1, (0, decorators_1.InjectOgmaContext)()), __param(2, (0, common_1.Optional)()), __param(3, (0, decorators_1.InjectTraceMethod)()), __metadata("design:paramtypes", [logger_1.Ogma, String, Object, Object]) ], OgmaService);