The ByteHide Logs 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
npm install @bytehide/logsByteHide.Logs 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.
Install ByteHide.Logs using npm or yarn:
npm install @bytehide/logs
or with yarn:
yarn add @bytehide/logs
- 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.
Set up ByteHide.Logs with your desired logging settings:
``javascript
import { Log } from "@bytehide/logs";
// Step 1: Configure the logs
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', 'admin@admin.com', 'abc123');
Log.info('User identification successful', { metadata: { email: 'admin@admin.com' } });
// 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('Logs setup and example complete.');
`
Enrich logs with global properties that are included in every log entry:
`javascript`
Log.addMetaContext('AppVersion', '1.2.3');
Log.addMetaContext('Environment', 'Production');
Track operations using correlation IDs:
`javascript`
Log.debug('Processing payment', { correlationId: 'operation-123' });
Reset the configuration to default values easily:
`javascript`
Log.reset();
ByteHide.Logs is licensed under the MIT License.
---
Happy coding but keep it safe with @bytehide/logs`! 🛡️