Socket
Book a DemoInstallSign in
Socket

@eclipse-emfcloud/model-logger

Package Overview
Dependencies
Maintainers
8
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eclipse-emfcloud/model-logger

Logger for ModelManager

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
8
Weekly downloads
 
Created
Source

Model Logger

This module provides a flexible logging factory that allows both default console logging and custom logging implementations.

Default behavior

The logger outputs messages to the console by default. To use the logger, import the getLogger function and create a logger instance with an optional name:

import { getLogger } from '@eclipse-emfcloud/model-logger';

export class AddressBook {
  // Retrieve the logger
  logger = getLogger('AddressBook');

  addAddress = (newAddress: Address): boolean => {
    const addresses = this.getAddressBook();

    if (!addresses.find((address) => compareAddresses(address, newAddress))) {
      logger.debug('Adding a new address');
      // [AddressBook]: Adding a new address
      addresses.push(newAddress);
      return true;
    } else {
      logger.error('Trying to add an existing address');
      // [AddressBook]: Trying to add an existing address
    }

    return false;
  };
}

The logger supports five log levels similar to the standard Console interface methods: log, error, debug, warn and info.

Override behavior

You can override the default logging behavior to implement custom logging strategies, such as logging to both console and file system.

To customize the logger, override the LOGGER_FACTORY.getLogger method with your implementation:

// index.ts
import * as fs from 'fs';
import path from 'path';
import { LOGGER_FACTORY } from '@eclipse-emfcloud/model-logger';

const logFilePath = path.resolve(__dirname, 'logs.txt');

// User-specific logger
LOGGER_FACTORY.getLogger = (name?: string) => ({
  error: (...messages: unknown[]) => {
    console.error(name ? `${name}:` : '', ...messages);
    fs.appendFileSync(
      logFilePath,
      `[error${name ? ` - ${name}:` : ''}]: ${messages.join(' ')}\n`
    );
  },
  warn: (...messages: unknown[]) => {
    console.warn(name ? `${name}:` : '', ...messages);
    fs.appendFileSync(
      logFilePath,
      `[warn${name ? ` - ${name}:` : ''}]: ${messages.join(' ')}\n`
    );
  },
  info: (...messages: unknown[]) => {
    console.info(name ? `${name}:` : '', ...messages);
    fs.appendFileSync(
      logFilePath,
      `[info${name ? ` - ${name}:` : ''}]: ${messages.join(' ')}\n`
    );
  },
  log: (...messages: unknown[]) => {
    console.log(name ? `${name}:` : '', ...messages);
    fs.appendFileSync(
      logFilePath,
      `[log${name ? ` - ${name}:` : ''}]: ${messages.join(' ')}\n`
    );
  },
  debug: (...messages: unknown[]) => {
    console.debug(name ? `${name}:` : '', ...messages);
    fs.appendFileSync(
      logFilePath,
      `[debug${name ? ` - ${name}:` : ''}]: ${messages.join(' ')}\n`
    );
  },
});

After overriding the factory, all subsequent calls to getLogger() will return your custom implementation while maintaining the named logger functionality.

FAQs

Package last updated on 30 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.