1.0.5 β€’ Published 4 months ago

ng-safelogger v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

NG SafeLogger

NG SafeLogger is an Angular package that securely captures and sends error logs to a configurable endpoint. It encrypts logs before sending, avoids duplicates, and temporarily stores logs in localStorage if the connection is unavailable.

πŸ“Œ Features

  • πŸš€ Automatic error capturing in Angular
  • πŸ” RSA encryption of logs before sending
  • πŸ”„ Prevents duplicates and reduces log spam
  • πŸ’Ύ Local storage of logs when offline, auto-retries later
  • ⏳ Batch sending every 30 seconds if new errors arrive
  • πŸ” User action tracking before an error occurs

πŸ“¦ Installation

npm install ng-safelogger

πŸš€ Usage in Angular

1️⃣ Import and Configure in main.ts

import { HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { provideSafeLogger, ErrorLoggerInterceptor } from 'ng-safelogger';

bootstrapApplication(AppComponent, {
  providers: [
    provideHttpClient(withInterceptorsFromDi()),
    
    { provide: HTTP_INTERCEPTORS, useClass: ErrorLoggerInterceptor, multi: true },
    provideSafeLogger({
      enableLogging: true,
      apiUrl: 'https://my-api-logs.com/errors',
      apiKey: 'abcdef-12345',
      publicKeyUrl: 'https://my-api-logs.com/public-key',
      appName: 'MyApp',
      appEnv: 'production',
      appBuildId: 'abcdef123456'
    }),
  ],
});

2️⃣ Capture Custom Errors

If you want to manually capture errors:

import { ErrorLoggerService } from 'ng-safelogger';

constructor(private errorLogger: ErrorLoggerService) {}

try {
  throw new Error('Testing error');
} catch (error) {
  this.errorLogger.handleError(error);
}

πŸ”‘ Configuration

OptionDescriptionRequired
enableLoggingEnables or disables logging (boolean).Yes
apiUrlEndpoint where the logs are sent.Yes
apiKeyAuthentication key for the API.No
publicKeyUrlURL to obtain the RSA public key for encryption.No
appNameName of the application generating the logs.Yes
appEnvEnvironment or build configuration (dev/prod).Yes
appBuildIdUnique build identifier.Yes

πŸ”’ Security and Privacy

  • Removes sensitive data such as emails, IDs, credit card info, and phone numbers before sending logs.
  • Logs are encrypted via RSA before being sent to the API.

❓ FAQs

πŸ”Ή What if the logging API is unavailable?
➑️ The logs are stored in localStorage and resent when the app restarts or upon navigation.

πŸ”Ή How do I disable log capturing?
➑️ Set enableLogging: false in the initialization provider.

πŸ”Ή Can I send logs manually?
➑️ Yes, just call this.errorLogger.handleError(error) from anywhere in your code.


πŸ“ License

This project is licensed under the MIT License. See the LICENSE file for more information.


πŸš€ Developed by Dyango RodrΓ­guez | πŸ’‘ Contributions and improvements welcome.