You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求推荐带重试机制的Angular2日志框架(支持推送至服务器/Logstash)

Angular 2+ Logging Framework Recommendations (With Server/Logstash Push & Retry Support)

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-logger automatically adds a timestamp field 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 retryWhen and delay to handle retries. For cases where the network is down entirely, you can cache failed logs in localStorage or IndexedDB, 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-logstash transport
    • Works with pino-retry plugin 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:

  1. Create a service that generates logs with timestamps
  2. Use Angular's HttpClient to send logs to your server/Logstash
  3. Implement retry logic with RxJS, and cache failed logs locally
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:48:57