UNPKG

@ngworker/lumberjack

Version:

Lumberjack is a versatile Angular logging library, specifically designed to be extended and customized

194 lines (184 loc) 6.59 kB
import { HttpClient, provideHttpClient } from '@angular/common/http'; import * as i0 from '@angular/core'; import { InjectionToken, inject, NgZone, Injectable, makeEnvironmentProviders } from '@angular/core'; import { lumberjackLogDriverToken, lumberjackLogDriverConfigToken } from '@ngworker/lumberjack'; import { pipe, retry, catchError, throwError, Subscription } from 'rxjs'; const lumberjackHttpDriverConfigToken = new InjectionToken('__LUMBERJACK_HTTP_DRIVER_CONFIG__'); class LumberjackHttpDriverError extends Error { constructor(message = 'LumberjackHttpDriverError') { super(message); this.name = 'LumberjackHttpDriverError'; // Non-standard V8 function for maintaining a stack trace const ErrorWithCaptureStackTrace = Error; ErrorWithCaptureStackTrace.captureStackTrace?.(this, this.constructor); } } const retryWithDelay = (maxRetries, delayMs) => pipe(retry({ count: maxRetries, delay: delayMs }), catchError(() => throwError(() => new LumberjackHttpDriverError(`Failed after ${maxRetries} retries.`)))); /** * The HTTP driver transports logs to the configured web API log store using the * POST HTTP verb. * * It sends the formatted log and the log including the optional log payload. */ class LumberjackHttpDriver { static driverIdentifier = 'LumberjackHttpDriver'; #http = inject(HttpClient); #ngZone = inject(NgZone); #subscriptions = new Subscription(); config = inject(lumberjackHttpDriverConfigToken); ngOnDestroy() { this.#subscriptions.unsubscribe(); } /** * Send critical log to the log store. * * @param param0 The log and its text representation. */ logCritical({ formattedLog, log }) { this.#sendLog(formattedLog, log); } /** * Send debug log to the log store. * * @param param0 The log and its text representation. */ logDebug({ formattedLog, log }) { this.#sendLog(formattedLog, log); } /** * Send error log to the log store. * * @param param0 The log and its text representation. */ logError({ formattedLog, log }) { this.#sendLog(formattedLog, log); } /** * Send info log to the log store. * * @param param0 The log and its text representation. */ logInfo({ formattedLog, log }) { this.#sendLog(formattedLog, log); } /** * Send trace log to the log store. * * @param param0 The log and its text representation. */ logTrace({ formattedLog, log }) { this.#sendLog(formattedLog, log); } /** * Send warning log to the log store. * * @param param0 The log and its text representation. */ logWarning({ formattedLog, log }) { this.#sendLog(formattedLog, log); } /** * Send log to the log store. * * This is done outside of the `NgZone` as there's no need this to trigger * change detection. * * Failed HTTP requests are retried according to the configured retry options. * * When the last retry fails, the driver will throw a `LumberjackHttpDriverError` * and the log being sent gets discarded. * * @param formattedLog The log's text representation. * @param log The log. */ #sendLog(formattedLog, log) { const { origin, retryOptions, storeUrl } = this.config; const httpLog = { formattedLog, origin, log }; this.#ngZone.runOutsideAngular(() => { this.#subscriptions.add(this.#http .post(storeUrl, httpLog) .pipe(retryWithDelay(retryOptions.maxRetries, retryOptions.delayMs)) // HTTP requests complete after the response is received, so there's no need to unsubscribe. .subscribe(() => { // No-op })); }); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.0", ngImport: i0, type: LumberjackHttpDriver, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.0.0", ngImport: i0, type: LumberjackHttpDriver }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.0", ngImport: i0, type: LumberjackHttpDriver, decorators: [{ type: Injectable }] }); const lumberjackHttpDriverProvider = { provide: lumberjackLogDriverToken, useClass: LumberjackHttpDriver, multi: true, }; function makeLumberjackHttpConfiguration(kind, providers) { return { kind, providers: makeEnvironmentProviders(providers), }; } function withHttpConfig(config) { return makeLumberjackHttpConfiguration('config', [ { provide: lumberjackHttpDriverConfigToken, deps: [lumberjackLogDriverConfigToken], useFactory: (logDriverConfig) => ({ ...logDriverConfig, identifier: LumberjackHttpDriver.driverIdentifier, ...config, }), }, ]); } function withHttpOptions(options) { return makeLumberjackHttpConfiguration('options', [ { provide: lumberjackHttpDriverConfigToken, deps: [lumberjackLogDriverConfigToken], useFactory: (logDriverConfig) => ({ ...logDriverConfig, identifier: LumberjackHttpDriver.driverIdentifier, ...options, }), }, ]); } /** * Returns the [dependency-injection providers](https://angular.io/guide/glossary#provider) * * for the `LumberjackHttpDriver` and its `LumberjackHttpDriverConfig`. * @usageNotes * * The function is useful when you want to bootstrap an application using * the `bootstrapApplication` function and want to make available the `LumberjackHttpDriver` providers. * * ```typescript * bootstrapApplication(RootComponent, { * providers: [ * provideLumberjack({...}), * provideLumberjackHttpDriver({...}) * ] * }); * ``` * * @publicApi */ function provideLumberjackHttpDriver(configuration, ...features) { return [ provideHttpClient(...features), makeEnvironmentProviders([lumberjackHttpDriverProvider]), configuration.providers, ]; } /* * Public API surface of @ngworker/lumberjack/http-driver */ /** * Generated bundle index. Do not edit. */ export { LumberjackHttpDriver, LumberjackHttpDriverError, provideLumberjackHttpDriver, withHttpConfig, withHttpOptions }; //# sourceMappingURL=ngworker-lumberjack-http-driver.mjs.map