simple-logger

A lightweight, flexible logging system for ContentSmith that supports namespace-based logging with different log levels.

View on GitHub

Simple Logger

A lightweight, flexible logging system for ContentSmith that supports namespace-based logging with different log levels.

Features

Usage

Basic Usage

import { LogManagerImpl } from '@coursebook/simple-logger';

// Get the logger instance
const logManager = LogManagerImpl.getInstance();

// Create a logger for your component
const logger = logManager.getLogger('myapp:component');

// Log at different levels
logger.trace('Detailed debugging');
logger.info('General information');
logger.warn('Warning message');
logger.error('Error occurred', { details: 'error info' });

Setting Log Levels

// Set level for specific namespace
logManager.setLogLevel('myapp:component', 'info');

// Set level for all components in 'myapp'
logManager.setLogLevel('myapp:', 'warn');

// Use regex pattern
logManager.setLogLevel(/test:\d+/, 'trace');

// Set default level for everything
logManager.setLogLevel('*', 'info');

Namespace Patterns

Log Levels

  1. trace - Verbose debugging information
  2. info - General information about operation progress
  3. warn - Warning messages for potentially problematic situations
  4. error - Error messages for operation failures

When you set a log level, all levels of equal or higher severity will be logged:

Enable/Disable Logging

// Disable all logging
logManager.disable();

// Re-enable logging
logManager.enable();

Object Logging

Objects are automatically pretty-printed:

logger.info('Processing config', {
  server: 'localhost',
  port: 3000
});

// Output:
// [INFO] [myapp:component] Processing config {
//   "server": "localhost",
//   "port": 3000
// }

Example Component Integration

export class FileManager {
  private logger: Logger;
  
  constructor() {
    this.logger = LogManagerImpl.getInstance().getLogger('filemanager');
  }
  
  async readFile(path: string): Promise<Buffer> {
    this.logger.trace('Reading file:', path);
    try {
      const content = await readFile(path);
      this.logger.info('Successfully read file:', path);
      return content;
    } catch (error) {
      this.logger.error('Failed to read file:', path, error);
      throw error;
    }
  }
}

Installation

Installing from NPM (After Publishing)

Once published to NPM, the package can be installed using:

npm install @coursebook/simple-logger

This template is particularly useful for creating packages that are intended to be used locally so read the instructions below for local development.

Local Development (Without Publishing to NPM)

There are three ways to use this package locally:

  1. Clone this repository, install dependencies, build the package, and create a global symlink:

    git clone <repository-url>
    cd simple-logger/packages/simple-logger
    # Install dependencies and build the package
    npm install
    npm run build
    # Create a global symlink
    npm link
    

    Note: You can unlink the package later using npm unlink.

  2. In your other project where you want to use this package:

    npm link @coursebook/simple-logger
    
  3. Import the package in your project:

    import { LogManagerImpl } from '@coursebook/simple-logger';
    

Option 2: Using local path

In your other project’s package.json, add this package as a dependency using the local path:

{
  "dependencies": {
    "@coursebook/simple-logger": "file:/path/to/simple-logger"
  }
}

You can use absolute or relative paths with the file: protocol.

Then run npm install in your project.

Now you can import the package in your project as usual.

Option 3: Using a local tarball (npm pack)

  1. Follow option 1 but instead of using npm link, create a tarball of the package:

    npm pack
    

    This will generate a file like coursebook-simple-logger-1.0.0.tgz. (Or whatever version you have.) You can find the tarball in the same directory as your package.json.

  2. In your other project, install the tarball:

    npm install /absolute/path/to/simple-logger/coursebook-simple-logger-1.0.0.tgz
    

    Or, if you copy the tarball into your project directory:

    npm install ./coursebook-simple-logger-1.0.0.tgz
    

This method installs the package exactly as it would be published to npm, making it ideal for final testing. After this installation, you must have the package in your node_modules directory, and you can import it as usual. You will also see the package in your package.json file as a dependency:

{
  "dependencies": {
    "@coursebook/simple-logger": "file:coursebook-simple-logger-1.0.0.tgz"
  }
}