Skip to content
/ slog Public

A lightweight, colorful, and feature-rich TypeScript logger with zero dependencies.

License

Notifications You must be signed in to change notification settings

je-es/slog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


logo


CI Test Coverage Github Repo Issues GitHub Repo stars

  • Quick Start πŸ”₯

    A lightweight, colorful, and feature-rich TypeScript logger with zero dependencies.

    • Setup

      install space first.

      # install
      space i @je-es/slog
      // import
      import { Logger } from '@je-es/slog';
    line
    • Usage

      • Basic Logging
        // Create a logger instance
        const logger = new Logger('info', false);
        
        // Log at different levels
        logger.debug({ userId: 123 }, 'User lookup');
        logger.info({ status: 'ready' }, 'Server initialized');
        logger.warn({ memory: '85%' }, 'High memory usage');
        logger.error({ code: 'ECONNREFUSED' }, 'Database connection failed');
        logger.fatal({ error: 'OutOfMemory' }, 'Critical system error');
        
        // Output (JSON format):
        // {"timestamp":"2024-12-04T01:35:12.665Z","level":"INFO","message":"Server initialized","status":"ready"}
      line
      • Pretty Mode (Human-Readable)
        // Enable pretty mode for colorful, readable logs
        const logger = new Logger('info', true);
        
        logger.info({ userId: 123 }, 'User authenticated');
        // Output: 01:35:12 ● User authenticated userId:123
        
        logger.warn({ disk: '90%' }, 'Low disk space');
        // Output: 01:35:12 ⚠ Low disk space disk:90%
        
        logger.error({ code: 500 }, 'Internal error');
        // Output: 01:35:12 βœ– Internal error code:500
      line
      • HTTP Request Logging (Morgan-Style)
        const logger = new Logger('info', true);
        
        // Automatically formats HTTP request logs
        logger.info({
            method: 'GET',
            path: '/api/users',
            status: 200,
            duration: 45
        });
        // Output: 01:35:12 GET /api/users 200 45ms (colored)
      line
      • Child Loggers with Prefixes
        const logger = new Logger('info', false);
        
        // Create namespaced loggers for different services
        const apiLogger = logger.child('API');
        const dbLogger = logger.child('Database');
        
        apiLogger.info({ endpoint: '/users' }, 'Request received');
        // Output: {"timestamp":"...","level":"INFO","message":"[API] Request received","endpoint":"/users"}
        
        // Nested child loggers
        const userService = apiLogger.child('Users');
        userService.info({ userId: 123 }, 'User created');
        // Output: {"timestamp":"...","level":"INFO","message":"[API:Users] User created","userId":123}
      line
      • Log Levels & Filtering
        // Set minimum log level (debug < info < warn < error < fatal)
        const logger = new Logger('warn', false);
        
        logger.debug({ test: 1 }, 'Debug msg');  // ❌ Won't log
        logger.info({ test: 2 }, 'Info msg');    // ❌ Won't log
        logger.warn({ test: 3 }, 'Warning msg'); // βœ… Will log
        logger.error({ test: 4 }, 'Error msg');  // βœ… Will log
        logger.fatal({ test: 5 }, 'Fatal msg');  // βœ… Will log
      line
      • Special Formatting (Pretty Mode)
        const logger = new Logger('info', true);
        
        // Route registration
        logger.info({ method: 'GET', path: '/api/users' }, 'Route added');
        // Output: 01:35:12 β†’ GET    /api/users
        
        logger.info({ method: ['GET', 'POST'], path: '/api/auth' }, 'Route added');
        // Output: 01:35:12 β†’ GET|POST /api/auth
        
        // Database connection
        logger.info({ name: 'PostgreSQL' }, 'βœ” Database connected');
        // Output: 01:35:12 βœ“ Database connected (PostgreSQL)
        
        // Server startup
        logger.info({ url: 'http://localhost:3000' }, 'Server started');
        // Output: 01:35:12 βœ“ Server started at http://localhost:3000
      line
      • String Messages
        const logger = new Logger('info', false);
        
        // You can pass a string directly as the data parameter
        logger.info('Simple message');
        // Output: {"timestamp":"...","level":"INFO","message":"Simple message"}
        
        // Works in pretty mode too
        const prettyLogger = new Logger('info', true);
        prettyLogger.info('Application started');
        // Output: 01:35:12 ● Application started


About

A lightweight, colorful, and feature-rich TypeScript logger with zero dependencies.

Topics

Resources

License

Stars

Watchers

Forks