logo

Logger

The Logger class provides an isomorphic and consistent way to log messages to the console.

  • The level determines which messages are logged, the default level is warn on non-production environments and error on production environments.
  • The status determines if the logger is enabled or disabled, the default status is on on non-test environments and off on test environments.
  • The colors are on by default on non-browser environments and match the browser console colors.
  • The separator is a string used to join the arguments passed to the logger methods, the default separator is a -> symbol.

Properties

  • colors (boolean): Determines if the colors are enabled or disabled. Colors are on by default on non-browser environments and match the browser console colors.
  • level (LoggerLevel): Determines which messages are logged. It can be verbose, debug, info, warn or error.
  • name (string): Used to get the level and status from the environment variables.
  • separator (string): A string used to join the arguments passed to the logger methods.
  • status (LoggerStatus): Determines if the logger is enabled or disabled.

Getters

  • isDisabled (boolean): Checks if the logger is disabled. Returns true if the status of the logger is ‘off’, false otherwise.
  • isEnabled (boolean): Checks if the logger is enabled. Returns true if the status of the logger is ‘on’, false otherwise.

Levels

  • verbose: This is the lowest level of logs, used for extended information. It is typically not enabled in a production environment.
  • debug: This level is used for information that may be helpful in debugging, but is not necessary under normal operation.
  • info: This level is used for informational messages that highlight the progress of the application at coarse-grained level.
  • warn: This level is used for potentially harmful situations. The system can still function but requires attention.
  • error: This level is used for error events that might still allow the application to continue running.

Log to the console

The verbose, debug, info, warn, and error methods can be used to log messages to the console.

import { Logger } from '@aracna/core';

const logger = new Logger('logger');

// will log "warn" as a warning
logger.warn('warn');

// will log "error" as an error
logger.error('error');

Set the level

The setLevel method is used to set the level of the logger.

import { Logger } from '@aracna/core';

const logger = new Logger('logger');
logger.setLevel('verbose');

// will log "verbose" as a verbose log
logger.verbose('verbose log');

Disable or enable the logger

The disable and enable methods are used to disable or enable the logger.

import { Logger } from '@aracna/core';

const logger = new Logger('logger');
logger.disable();

// will not log
logger.warn('warn log');

logger.enable();

// will log
logger.warn('warn log');

Disable or enable the ANSI colors

The disableColors and enableColors methods are used to disable or enable the ANSI colors.

import { Logger } from '@aracna/core';

const logger = new Logger('logger');

logger.enableColors();

// will log "warn" with ANSI colors
logger.warn('warn log');

logger.disableColors();

// will log "warn" without ANSI colors
logger.warn('warn log');

Use level and status from the environment

The level and status properties of the logger can be set using environment variables.

  • The level property can be set using the formula LOGGER_<NAME>_LEVEL, where <NAME> is the name property of the logger.
  • The status property can be set using the formula LOGGER_<NAME>_STATUS, where <NAME> is the name property of the logger.
import { Logger } from '@aracna/core';

process.env.LOGGER_APP_LEVEL = 'verbose';
process.env.LOGGER_APP_STATUS = 'on';

const logger = new Logger('APP');

// will log "verbose"
logger.verbose('verbose log');