@bytehide/logger
v1.0.12
Published
The ByteHide Logger is a powerful and easy-to-use library that allows you to log information, warnings, and errors in your applications. It is designed to be simple to use and easy to integrate into your projects
Downloads
658
Readme
ByteHide.Logger SDK for JavaScript
ByteHide.Logger is a powerful and flexible logging library for JavaScript and TypeScript applications. It offers advanced features like enriched metadata logging, user identification, duplicate suppression, and more, ensuring a robust logging solution.
Installation
Install ByteHide.Logger using npm or yarn:
npm install @bytehide/logger
or with yarn:
yarn add @bytehide/logger
Features
- Enriched Logging: Include tags, metadata, and correlation IDs.
- Log Level Control: Configure the minimum log level to capture only relevant messages.
- Duplicate Suppression: Avoid logging repetitive messages within a configurable time window.
- Sensitive Data Protection: Mask sensitive information such as passwords or tokens.
- User Identification: Associate logs with specific users.
- Console and Persistence Integration: Log to the console or use custom integrations for storage.
Basic Usage
Initial Setup
Set up ByteHide.Logger with your desired logging settings:
import { Log } from "@bytehide/logger";
// Step 1: Configure the logger
Log.configure({
minimumLevel: 'debug', // Capture logs from this level and above
consoleEnabled: true, // Output logs to the console
maskSensitiveData: ['password', 'token'], // Automatically mask these fields in logs
duplicateSuppressionWindowMs: 1000, // Suppress duplicate logs within 1 second
});
// Step 2: Set the project token for log tracking
Log.setProjectToken('your-project-token');
Log.addMetaContext('AppVersion', '1.2.3');
Log.addMetaContext('Environment', 'Production');
// Step 3: Identify a user globally (optional)
Log.identify('john.doe', '[email protected]', 'abc123');
Log.info('User identification successful', { metadata: { email: '[email protected]' } });
// Step 4: Log messages with additional context
// Basic informational log
Log.info('Application started successfully.');
// Log with correlation ID to track related operations
const correlationId = '1234-abcd-5678-efgh';
Log.debug('Fetching user details', { correlationId, tags: ['service', 'user'] });
// Warning log with metadata
Log.warn('API rate limit reached', {
correlationId,
tags: ['api', 'rate-limit'],
metadata: { userId: 'john.doe', endpoint: '/users/details' },
});
// Error log with critical context
Log.error('Failed to process user update', {
tags: ['critical', 'update-failure'],
correlationId,
metadata: { userId: 'john.doe', operation: 'user.update' },
});
// Step 5: Handle exceptions gracefully
try {
throw new Error('Test Error');
} catch (error) {
Log.critical('An unhandled exception occurred', { tags: ['exception'] }, error);
}
// Step 6: Dynamically enable or disable logging
Log.disable();
Log.info('This log will not appear because logging is disabled.');
Log.enable();
Log.info('Logging is now enabled again.');
// Step 7: Suppress duplicate logs
Log.info("This log will be suppressed Start.");
Log.info("This log will be suppressed.");
Log.info("This log will be suppressed.");
Log.info("This log will be suppressed.");
Log.info("This log will be suppressed.");
Log.info("This log will be suppressed.");
Log.info("This log will be suppressed.");
Log.info("This log will be suppressed.");
Log.info("This log will be suppressed.");
Log.info("This log will be suppressed End.");
// Step 8: Log with context
Log.warn('Database query delay.', {tags: ['performance', 'database'], context: {query: 'SELECT * FROM users WHERE id = 1234'}});
// Optional: Demonstrate the masking of sensitive data
Log.info('Sensitive data example', {
metadata: { password: 'my-secret-password', token: 'abc123' }, // These will be masked
});
// Step 8: Use custom log levels (if configured)
Log.debug('This is a debug message, useful for troubleshooting.');
Log.warn('This is a warning message, indicating potential issues.');
Log.error('This is an error message, reporting failures.');
console.log('Logger setup and example complete.');
Advanced Features
Global Properties
Enrich logs with global properties that are included in every log entry:
Log.addMetaContext('AppVersion', '1.2.3');
Log.addMetaContext('Environment', 'Production');
Correlation IDs
Track operations using correlation IDs:
Log.debug('Processing payment', { correlationId: 'operation-123' });
Log Configuration
Reset the configuration to default values easily:
Log.reset();
License
ByteHide.Logger is licensed under the MIT License.
Happy coding but keep it safe with @bytehide/logger
! 🛡️