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.
- 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, anderrorlog levels. -
TypeScript Support: Fully typed with
.d.tsfiles included for better integration in TypeScript projects.
You can install Log-Tonic via npm:
npm install log-tonicInitialize 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
});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"');-
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
prefixandsuffixto wrap your log messages (e.g.,{ prefix: '--> INFO: ', suffix: ' <-- END' }).
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
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.
Log-Tonic is licensed under the Apache License, Version 2.0. See the LICENSE file for more details.