1.0.2 • Published 1 year ago

@eklabdev/loggerts4 v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@eklabdev/loggerts4

A modular and extensible TypeScript logging library with support for multiple channels, log levels, and stage 2 decorators.

Features

  • Multiple log levels (DEBUG, INFO, WARN, ERROR, FATAL)
  • Support for both synchronous and asynchronous logging channels
  • Built-in console and file channels
  • TypeScript stage 2 decorators for automatic method logging
  • Extensible channel system for custom logging destinations
  • Metadata support for contextual logging
  • TypeScript 5 compatible

Installation

npm install @eklabdev/loggerts4

Basic Usage

import { Logger, ConsoleChannel, LogLevel } from '@eklabdev/loggerts4';

// Create a logger with a console channel
const logger = new Logger('AppLogger').addChannel(new ConsoleChannel());

// Basic logging
logger.debug('Debug message');
logger.info('Info message');
logger.warn('Warning message', { source: 'system' });
logger.error('Error occurred', new Error('Something went wrong'));

// Logging with metadata
logger.info('User logged in', { userId: '123' }, { sessionId: 'abc' });

Using File Channel

import { Logger, FileChannel } from '@eklabdev/loggerts4';

const logger = new Logger('AppLogger').addChannel(
  new FileChannel('/path/to/logs/app.log', 'FileChannel', {
    append: true,
    encoding: 'utf8',
  })
);

// Logs will be written to the file
logger.info('Application started');

Using Stage 2 Decorators

import { Logger, ConsoleChannel, LogClass, LogSync, LogAsync } from '@eklabdev/loggerts4';

const logger = new Logger('UserService').addChannel(new ConsoleChannel());

@LogClass(logger, { service: 'UserService' })
class UserService {
  @LogSync({
    logStart: true,
    logFinish: true,
    metadata: { operation: 'getUser' },
  })
  getUser(id: string) {
    return { id, name: 'John Doe' };
  }

  @LogAsync({
    logStart: true,
    logFinish: true,
    logError: true,
    errorHandler: error => ({ errorCode: (error as Error).message }),
  })
  async updateUser(id: string, data: any): Promise<void> {
    // Async method implementation
    return Promise.resolve();
  }
}

Creating Custom Channels

import { AsyncChannel, LogEntry } from '@eklabdev/loggerts4';

class DatabaseChannel extends AsyncChannel {
  constructor(private dbConnection: any) {
    super('DatabaseChannel');
  }

  protected async writeAsync(entry: LogEntry): Promise<void> {
    await this.dbConnection.logs.insert({
      timestamp: entry.timestamp,
      level: entry.level,
      message: entry.message,
      data: entry.data,
      metadata: entry.metadata,
    });
  }
}

// Usage
const logger = new Logger('AppLogger').addChannel(new DatabaseChannel(dbConnection));

API Reference

Logger

The main logger class that manages channels and logging operations.

class Logger {
  constructor(name: string, options?: LogOptions);
  addChannel(channel: IChannel): this;
  removeChannel(channelName: string): this;
  setMetadata(metadata: Record<string, any>): this;
  log<T>(level: LogLevel, message: string, data?: T, metadata?: Record<string, any>): void;
  debug<T>(message: string, data?: T, metadata?: Record<string, any>): void;
  info<T>(message: string, data?: T, metadata?: Record<string, any>): void;
  warn<T>(message: string, data?: T, metadata?: Record<string, any>): void;
  error<T>(message: string, data?: T, metadata?: Record<string, any>): void;
  fatal<T>(message: string, data?: T, metadata?: Record<string, any>): void;
}

Channels

ConsoleChannel

Synchronous channel that writes logs to the console.

class ConsoleChannel extends SyncChannel {
  constructor(name?: string, options?: ConsoleChannelOptions);
}

FileChannel

Asynchronous channel that writes logs to a file.

class FileChannel extends AsyncChannel {
  constructor(filePath: string, name?: string, options?: FileChannelOptions);
  initialize(): Promise<void>;
  close(): Promise<void>;
}

Stage 2 Decorators

@LogClass

Class decorator that attaches a logger instance to the class.

function LogClass(logger: Logger, metadata?: Record<string, any>): ClassDecorator;

@LogSync

Method decorator for synchronous methods.

function LogSync(options?: LogOptions): MethodDecorator;

@LogAsync

Method decorator for asynchronous methods.

function LogAsync(options?: LogOptions): MethodDecorator;

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT