JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1946
  • Score
    100M100P100Q90981F
  • License GPL-3.0-only

Logger for c15t.

Package Exports

  • @c15t/logger

Readme

c15t Banner

@c15t/logger

GitHub stars CI License Discord npm version

A lightweight, customizable logging utility for Node.js and TypeScript applications. Designed for use in c15t CLI and backend applications.

Features

  • ðŸŽĻ Color-coded console output with picocolors
  • 🔧 Configurable log levels (error, warn, info, debug, success)
  • 📝 Custom log handlers
  • ðŸ›Ąïļ Type-safe with TypeScript
  • 🔄 Error logging for Result/ResultAsync types from neverthrow
  • ðŸŽŊ Console redirection functionality
  • ðŸŠķ Lightweight with minimal dependencies

Installation

pnpm add @c15t/logger

Quick Start

import { createLogger } from '@c15t/logger';

// Create a logger instance
const logger = createLogger({
    level: 'info',
    appName: 'my-app',
});

// Log messages at different levels
logger.info('Application started');
logger.debug('Debug information', { userId: 123 });
logger.warn('Warning message');
logger.error('Error occurred', new Error('Something went wrong'));
logger.success('Operation completed successfully');

Configuration

LoggerOptions

interface LoggerOptions {
    /** Whether logging is disabled */
    disabled?: boolean;

    /** The minimum log level to publish */
    level?: 'error' | 'warn' | 'info' | 'debug';

    /** Custom log handler function */
    log?: (level: LogLevel, message: string, ...args: unknown[]) => void;

    /** Custom application name to display in log messages */
    appName?: string;
}

Advanced Usage

Custom Log Handler

const logger = createLogger({
    level: 'info',
    appName: 'my-app',
    log: (level, message, ...args) => {
        // Send logs to external service
        sendToLoggingService({ level, message, args });
    },
});

Extending the Logger

import { extendLogger } from '@c15t/logger';

const baseLogger = createLogger({ level: 'info' });

const extendedLogger = extendLogger(baseLogger, {
    http: (message, ...args) => baseLogger.info(`HTTP: ${message}`, ...args),
    database: (message, ...args) => baseLogger.info(`DB: ${message}`, ...args),
});

extendedLogger.http('GET /api/users');
extendedLogger.database('Query executed in 10ms');

Error Logging with neverthrow

import { logResult, logResultAsync } from '@c15t/logger';
import { ok, err, okAsync } from 'neverthrow';

const result = err(new Error('Something went wrong'));

// Log if the result is an error
logResult(result, logger, 'Operation failed');

// For async results
const asyncResult = okAsync({ data: 'success' });
await logResultAsync(asyncResult, logger, 'Async operation');

API Reference

Core Functions

  • createLogger(options?: LoggerOptions): Logger - Creates a configured logger instance
  • logger - Default logger instance with standard configuration
  • extendLogger<T>(baseLogger: Logger, extensions: T): ExtendedLogger<T> - Extends a logger with additional methods

Utility Functions

  • logResult(result, logger, message) - Logs neverthrow Result if it's an error
  • logResultAsync(result, logger, message) - Logs neverthrow ResultAsync if it's an error
  • formatArgs(args) - Formats arguments for display
  • formatMessage(level, message, args, appName) - Formats a log message with app name and styling

Support

Contributing

  • We're open to all community contributions!
  • Read our Contribution Guidelines
  • Review our Code of Conduct
  • Fork the repository
  • Create a new branch for your feature
  • Submit a pull request
  • All contributions, big or small, are welcome and appreciated!

Security

If you believe you have found a security vulnerability in c15t, we encourage you to responsibly disclose this and NOT open a public issue. We will investigate all legitimate reports.

Our preference is that you make use of GitHub's private vulnerability reporting feature to disclose potential security vulnerabilities in our Open Source Software. To do this, please visit https://github.com/c15t/c15t/security and click the "Report a vulnerability" button.

License

GNU General Public License v3.0


Built with âĪïļ by the consent.io team