Package Exports
- logger-fluent
- logger-fluent/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (logger-fluent) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Logger Fluent
This directory contains example scripts that demonstrate how to use the logger-fluent npm library after it has been built.
Prerequisites
Before running any examples, make sure to:
Build the library first:
npm run buildInstall dependencies (if not already done):
npm install
Available Examples
1. Basic JavaScript Usage (basic-usage.js)
A comprehensive JavaScript example that demonstrates:
- Default logger usage
- Custom logger configuration
- All log levels (debug, info, warn, error)
- Log level filtering
- Metadata logging
- Proper logger cleanup
To run:
node examples/basic-usage.jsOutput directories:
./logs/- Default logger output./temp/- Custom logger output./temp/warn-only/- Log level filtering example
2. Multi-Type Logging (multi-type-usage.js)
NEW! Demonstrates the enhanced multi-type logging capabilities:
- Console-only logging
- File-only logging (default behavior)
- Combined console + file logging
- Backward compatibility with
createFileLogger - New utility methods (
getLoggerTypes(),hasLoggerType())
To run:
node examples/multi-type-usage.jsOutput directories:
./temp-multi/file-only/- File-only logger output./temp-multi/combined/- Combined logger file output (also shows in console)./temp-multi/legacy/- Backward compatibility test./temp-multi/default/- Default behavior test
3. TypeScript Usage (typescript-usage.ts)
A TypeScript example showcasing:
- Full type safety with logger options
- Typed metadata interfaces
- Multiple specialized loggers
- Advanced configuration patterns
- IntelliSense support
To run with ts-node:
# Install ts-node globally if not already installed
npm install -g ts-node
# Run the TypeScript example
ts-node examples/typescript-usage.tsAlternative - Compile and run:
# Compile TypeScript to JavaScript
npx tsc examples/typescript-usage.ts
# Run the compiled JavaScript
node examples/typescript-usage.jsOutput directories:
./logs/- Default logger output./temp-ts/- Main TypeScript example output./temp-ts/api/- API logger output./temp-ts/database/- Database logger output./temp-ts/security/- Security logger output
What the Examples Demonstrate
Core Features
- ✅ File logging with automatic directory creation
- ✅ Log rotation (daily rotation with size limits)
- ✅ Multiple log levels (debug, info, warn, error)
- ✅ Separate error log files
- ✅ Custom log directories
- ✅ Metadata logging (structured data)
- ✅ Log level filtering
- ✅ Proper resource cleanup
Configuration Options
type- NEW! Output type(s):'console','file', or['console', 'file'](default:'file')logDir- Custom log directory pathlogLevel- Minimum log level to writemaxSize- Maximum file size before rotationmaxFiles- How long to keep old log filesdatePattern- Date pattern for file rotationseparateErrorLogs- Whether to create separate error files
TypeScript Benefits
- Full type safety for all logger methods
- IntelliSense support in your IDE
- Compile-time error checking
- Better refactoring capabilities
Expected Output
After running the examples, you should see:
- Console output showing the progress of each test
- Log files created in the specified directories
- Formatted log entries with timestamps and levels
Sample Log File Content
[2024-01-15 10:30:45.123] INFO: General information about application flow
[2024-01-15 10:30:45.124] WARN: Something unexpected happened
[2024-01-15 10:30:45.125] ERROR: Something went wrongTroubleshooting
Common Issues
"Cannot find module '../lib/index'"
- Make sure you've built the library first:
npm run build - Check that the
lib/directory exists and contains the compiled files
- Make sure you've built the library first:
Permission errors when creating directories
- Ensure you have write permissions in the project directory
- The examples will attempt to create fallback directories if needed
TypeScript compilation errors
- Make sure TypeScript is installed:
npm install -g typescript - Check that your TypeScript version is compatible (>= 4.0)
- Make sure TypeScript is installed:
No log files created
- Check console output for error messages
- Verify that the directories have write permissions
- Log files may take a moment to appear due to buffering
Debugging Tips
- Check the console output for detailed information about what's happening
- Look for error messages that indicate configuration issues
- Verify file permissions in the target directories
- Use
ls -lato check if log files were created with the correct timestamps
Integration with Your Project
To use logger-fluent in your own project:
// JavaScript
const {
createLogger,
createFileLogger,
defaultLogger,
} = require('logger-fluent');
// TypeScript
import {
createLogger,
createFileLogger,
defaultLogger,
LoggerOptions,
} from 'logger-fluent';
// Create a console-only logger
const consoleLogger = createLogger({
type: 'console',
logLevel: 'debug',
});
// Create a combined console + file logger
const multiLogger = createLogger({
type: ['console', 'file'],
logDir: './my-app-logs',
logLevel: 'info',
maxSize: '50m',
maxFiles: '30d',
});
// Create a file-only logger (backward compatible)
const fileLogger = createFileLogger({
logDir: './my-app-logs',
logLevel: 'info',
});
// Use the logger
multiLogger.info('Application started');
multiLogger.error('Something went wrong', { error: 'details here' });
// Check logger configuration
console.log('Logger types:', multiLogger.getLoggerTypes());
console.log('Has console output:', multiLogger.hasLoggerType('console'));
console.log('Has file output:', multiLogger.hasLoggerType('file'));Next Steps
After running these examples:
- Examine the generated log files to understand the output format
- Try modifying the configuration options to see how they affect behavior
- Integrate the logger into your own applications
- Consider setting up log monitoring and analysis tools
For more information, check the main project README and API documentation.