log-tonic
v1.0.1
Published
A powerful and flexible logging utility for Node.js and TypeScript projects.
Downloads
4
Maintainers
Readme
Log-Tonic
Log-Tonic is a powerful and flexible logging utility for Node.js and TypeScript projects. It allows you to easily manage and format log messages across different features or modules within your application, with customizable prefixes, suffixes, and time formats.
Features
- Easy Initialization: Initialize once with global settings for your entire application.
- Feature-Specific Logging: Create loggers tailored for specific features or modules.
- Customizable Message Format: Add optional prefixes and suffixes to log messages for better readability.
- Support for Multiple Log Levels: Includes support for
debug
,info
, anderror
log levels. - TypeScript Support: Fully typed with
.d.ts
files included for better integration in TypeScript projects.
Installation
You can install Log-Tonic via npm:
npm install log-tonic
Usage
1. Initialize the Logger
Initialize the logger at the start of your application with the desired configuration:
import { LoggerFactory } from 'log-tonic';
LoggerFactory.initialize({
level: 'debug',
appName: 'MyApp',
timeFormat: 'yyyy-MM-dd HH:mm:ss', // Custom time format using date-fns
messageFormat: { prefix: '--> INFO: ', suffix: ' <-- END' } // Optional custom prefix and suffix
});
2. Create Feature-Specific Loggers
Create a logger for a specific feature or module within your application:
const authLogger = LoggerFactory.createLogger('AuthService');
authLogger.info('User login successful');
authLogger.error('User login failed');
authLogger.debug('User login attempt with username "admin"');
3. Available Configuration Options
- level: Sets the global log level. Available options are
debug
,info
,error
. - appName: The name of your application (default is
MyApp
). - timeFormat: The format for timestamps, using date-fns format tokens (e.g.,
yyyy-MM-dd HH:mm:ss
). - messageFormat: Optionally set
prefix
andsuffix
to wrap your log messages (e.g.,{ prefix: '--> INFO: ', suffix: ' <-- END' }
).
Example
const paymentLogger = LoggerFactory.createLogger('PaymentService');
paymentLogger.info('Payment processed successfully');
paymentLogger.error('Payment failed');
paymentLogger.debug('Processing payment for user ID 123');
This example will produce logs formatted like:
2024-08-11 13:45:30 [MyApp] [PaymentService] INFO: --> INFO: Payment processed successfully <-- END
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/my-feature
). - Commit your changes (
git commit -m 'Add my feature'
). - Push to the branch (
git push origin feature/my-feature
). - Open a pull request.
License
Log-Tonic is licensed under the Apache License, Version 2.0. See the LICENSE file for more details.