ng-safelogger v1.0.5
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
Option | Description | Required |
---|---|---|
enableLogging | Enables or disables logging (boolean). | Yes |
apiUrl | Endpoint where the logs are sent. | Yes |
apiKey | Authentication key for the API. | No |
publicKeyUrl | URL to obtain the RSA public key for encryption. | No |
appName | Name of the application generating the logs. | Yes |
appEnv | Environment or build configuration (dev/prod). | Yes |
appBuildId | Unique 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.