求推荐带重试机制的Angular2日志框架(支持推送至服务器/Logstash)
Hey there! Let's tackle your logging requirements step by step—you're looking for an Angular 2+ framework that can push logs to a server/Logstash, handle retries on network failures, and preserve original timestamps. I'll start with your current candidate, ngx-logger, then share other solid options.
First: ngx-logger Capabilities & Retry Workaround
ngx-logger is a popular choice for Angular logging, and while it doesn't have built-in retry functionality for failed server pushes, you can easily extend it to add this feature. Here's how:
- Timestamp Preservation: Good news—
ngx-loggerautomatically adds atimestampfield to every log entry by default. You can configure the format if needed, so you'll retain the original log time without extra work. - Adding Retry Logic: You can create a custom HTTP appender (or override the default one) that uses RxJS operators like
retryWhenanddelayto handle retries. For cases where the network is down entirely, you can cache failed logs inlocalStorageorIndexedDB, then check for connectivity periodically and resend them once the network is back.
Example snippet for a custom retry-enabled appender:
import { Injectable } from '@angular/core'; import { NGXLoggerHttpService } from 'ngx-logger'; import { HttpClient } from '@angular/common/http'; import { retryWhen, delay, take } from 'rxjs/operators'; @Injectable() export class RetryableNgxLoggerHttpService extends NGXLoggerHttpService { constructor(http: HttpClient) { super(http); } log(url: string, log: any): void { this.http.post(url, log).pipe( // Retry 3 times with 5-second delays between attempts retryWhen(errors => errors.pipe(delay(5000), take(3))) ).subscribe({ error: (err) => { // Cache failed log to localStorage if retries exhaust const cachedLogs = JSON.parse(localStorage.getItem('failedLogs') || '[]'); cachedLogs.push({ ...log, retryAttempts: 3 }); localStorage.setItem('failedLogs', JSON.stringify(cachedLogs)); } }); } }
Then provide this service in your Angular module to override the default NGXLoggerHttpService.
Alternative Frameworks With Built-In Retry Support
If you'd prefer a solution with out-of-the-box retry functionality, here are two great options:
angular-logger-service
- Supports custom appenders for HTTP and Logstash (via TCP/UDP)
- Has built-in configurable retry policies (number of attempts, delay between retries)
- Automatically preserves the original log timestamp in every entry
- Lightweight and integrates seamlessly with Angular 2+ projects
pino-angular
- A wrapper for the high-performance Pino logger (popular in Node.js ecosystems)
- Can send logs directly to Logstash using
pino-logstashtransport - Works with
pino-retryplugin to handle failed log deliveries automatically - Includes high-precision timestamps by default, no extra configuration needed
Custom Logging Service (Full Control)
If you want complete flexibility, building a custom logging service is straightforward:
- Create a service that generates logs with timestamps
- Use Angular's
HttpClientto send logs to your server/Logstash - Implement retry logic with RxJS, and cache failed logs locally
- Add a network listener (using
navigator.onLine) to trigger resends when connectivity is restored
This approach lets you tailor every part of the logging flow to your exact needs.
内容的提问来源于stack exchange,提问作者Soundar Raj

