JSPM

logger-fluent

1.0.1
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 1
    • Score
      100M100P100Q14504F
    • License ISC

    A flexible file logging library with TypeScript support, featuring log rotation, multiple log levels, and automatic directory creation

    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:

    1. Build the library first:

      npm run build
    2. Install 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.js

    Output 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.js

    Output 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.ts

    Alternative - Compile and run:

    # Compile TypeScript to JavaScript
    npx tsc examples/typescript-usage.ts
    
    # Run the compiled JavaScript
    node examples/typescript-usage.js

    Output 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 path
    • logLevel - Minimum log level to write
    • maxSize - Maximum file size before rotation
    • maxFiles - How long to keep old log files
    • datePattern - Date pattern for file rotation
    • separateErrorLogs - 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:

    1. Console output showing the progress of each test
    2. Log files created in the specified directories
    3. 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 wrong

    Troubleshooting

    Common Issues

    1. "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
    2. Permission errors when creating directories

      • Ensure you have write permissions in the project directory
      • The examples will attempt to create fallback directories if needed
    3. TypeScript compilation errors

      • Make sure TypeScript is installed: npm install -g typescript
      • Check that your TypeScript version is compatible (>= 4.0)
    4. 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 -la to 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:

    1. Examine the generated log files to understand the output format
    2. Try modifying the configuration options to see how they affect behavior
    3. Integrate the logger into your own applications
    4. Consider setting up log monitoring and analysis tools

    For more information, check the main project README and API documentation.