Logger utility
These advanced logging functionalities extend the capabilities of the Consola
logger. Together, this gives you structured and versatile logging options suitable for various development needs, from debugging to production monitoring.
Key features
- Consola Integration: Uses the
Consola
logging library for a wide range of logging levels and methods, ensuring compatibility with existing logging practices. - Deprecation Logging: Introduces a custom logging method for deprecated symbols, encouraging best practices and aiding in the gradual phasing out of outdated code.
- Configurable Logging Levels: Includes support for setting log levels, enabling you to fine-tune the verbosity of log output to meet the current environment or debugging needs.
Logger options
Options for creating a logger are extended from ConsolaOptions
, enabling the customization of logger behaviour, such as log level, reporters, and more.
Examples
Creating a logger
The Logger
interface extends the functionality of Consola
, providing support for multiple log levels such as log
, info
, warn
, and error
. Additionally, it introduces a specialized method for logging deprecation warnings. Here's how you can create and use a logger:
const logger = createLogger('exampleLogger');
logger.warn('This is a warning message'); // Outputs: [exampleLogger] This is a warning message
Deprecated method
The deprecated method logs a deprecation warning for a specified symbol, optionally including guidance on alternative usage and the target version for removal. This ensures clear communication about outdated functionality in your codebase.
import { createLogger, LogLevel } from '@genesislcap/foundation-logger';
const logger = createLogger('myLogger', { level: LogLevel.Debug });
logger.deprecated(
'oldFunction',
'Use newFunction instead.',
'v2.0.0'
);
// Outputs: [myLogger] Debug: Deprecated symbol used 'oldFunction'. Use 'newFunction' instead. Will be removed in version v2.0.0.
Considerations
- Use the
deprecated
method to log deprecation warnings, making it easier to transition away from outdated code. - Adjust log levels dynamically to match the environment, minimizing noise in production logs while maximizing debug information during development.
- Migrate to the recommended
createLogger
from@genesislcap/foundation-logger
to take advantage of centralized logging improvements and features.