Skip to content

Logger

The Logger service provides basic logging functionalities.

Classes

Logger

Defined in: logger/logger.service.ts:56

Represents a logger service that provides logging functionality. Messages will be logged to console and also persisted for the duration of the service’s lifetime.

Example

// Set the log level while bootstrapping your application
Logger.level = 'debug';
// Logging inside a function
Logger.debug('Hello World!', 'MyFunction');
// Logging inside a class
Logger.info('Hello World!', this.constructor.name);
// Logging without a context
Logger.warn('Hello World!');
// You may also directly import the logging functions
import { error } from '@spuxx/js-utils';
error('Hello World!');
// Access the stored messages
Logger.messages.forEach((message) => console.log(JSON.stringify(message)));

Extends

Constructors

Constructor

protected new Logger(): Logger

Defined in: mixin/service-mixin.ts:24

Service classes should not be instantiated directly. Instead, access the instance property to get the existing singleton instance or to create a new one if it does not yet exist.

Returns

Logger

Inherited from

ServiceMixin<Logger>().constructor

Accessors

instance
Get Signature

get static instance(): TService

Defined in: mixin/service-mixin.ts:33

Returns the instance of the service.

Example
const myService = MyService.instance;
myService.doSomething();
Returns

TService

The instance of the service.

Inherited from

ServiceMixin<Logger>().instance

level
Get Signature

get static level(): LogLevel

Defined in: logger/logger.service.ts:67

Returns the Loggers log level.

Example
Logger.setLevel('debug');
console.log(Logger.level); // Output: debug
Returns

LogLevel

The log level.

messages
Get Signature

get static messages(): LogMessage[]

Defined in: logger/logger.service.ts:90

Returns all stored log messages.

Example
console.log(Logger.messages.length); // Output: 0
Logger.debug('Hello World!');
console.log(Logger.messages.length); // Output: 1
Returns

LogMessage[]

The list of stored log messages.

Methods

debug()

static debug(message, context?): void

Defined in: logger/logger.service.ts:101

Logs a debug message.

Parameters
message

string

The message to log.

context?

string

The context of the message (e.g. the caller).

Returns

void

Example
Logger.debug('Hello World!', 'MyFunction');
destroy()

static destroy(): void

Defined in: mixin/service-mixin.ts:47

Destroys the existing instance of the service.

Returns

void

Example
const myService = MyService.instance;
MyService.destroy();
const myNewService = MyService.instance; // This will be a new instance
Inherited from

ServiceMixin<Logger>().destroy

error()

static error(message, context?): void

Defined in: logger/logger.service.ts:140

Logs an error message.

Parameters
message

string

The message to log.

context?

string

The context of the message (e.g. the caller).

Returns

void

Example
Logger.error('Hello World!', 'MyFunction');
info()

static info(message, context?): void

Defined in: logger/logger.service.ts:114

Logs an info message.

Parameters
message

string

The message to log.

context?

string

The context of the message (e.g. the caller).

Returns

void

Example
Logger.info("Hello World!", "MyFunction);
setLevel()

static setLevel(level): void

Defined in: logger/logger.service.ts:78

Sets the Loggers log level.

Parameters
level

LogLevel

The log level to set.

Returns

void

Example
Logger.setLevel('debug');
console.log(Logger.level); // Output: debug
warn()

static warn(message, context?): void

Defined in: logger/logger.service.ts:127

Logs a warn message.

Parameters
message

string

The message to log.

context?

string

The context of the message (e.g. the caller).

Returns

void

Example
Logger.warn('Hello World!', 'MyFunction');

Interfaces

LogMessage

Defined in: logger/logger.service.ts:13

Represents a log message that can be logged to the console and persisted.

Properties

context?

optional context: string

Defined in: logger/logger.service.ts:29

The context of the message (e.g. the caller).

date

date: Date

Defined in: logger/logger.service.ts:25

The date and time when the message was created.

level

level: LogLevel

Defined in: logger/logger.service.ts:21

The LogLevel of the message.

text

text: string

Defined in: logger/logger.service.ts:17

The message text.

Type Aliases

LogLevel

LogLevel = "debug" | "info" | "warn" | "error"

Defined in: logger/logger.service.ts:8

Represents the log level of a message. The log level determines the severity of the message and also controls the output channel (e.g. stdout, stderr).

Variables

debug()

const debug: (message, context?) => void

Defined in: logger/logger.service.ts:170

Logs a debug message.

Logs a debug message.

Parameters

message

string

The message to log.

context?

string

The context of the message (e.g. the caller).

Returns

void

Example

Logger.debug('Hello World!', 'MyFunction');

Param

The message to log.

Param

The context of the message (e.g. the caller).

Example

debug('Hello World!', 'MyFunction');

error()

const error: (message, context?) => void

Defined in: logger/logger.service.ts:194

Logs an error message.

Logs an error message.

Parameters

message

string

The message to log.

context?

string

The context of the message (e.g. the caller).

Returns

void

Example

Logger.error('Hello World!', 'MyFunction');

Param

The message to log.

Param

The context of the message (e.g. the caller).

Example

error('Hello World!', 'MyFunction');

info()

const info: (message, context?) => void

Defined in: logger/logger.service.ts:178

Logs an info message.

Logs an info message.

Parameters

message

string

The message to log.

context?

string

The context of the message (e.g. the caller).

Returns

void

Example

Logger.info("Hello World!", "MyFunction);

Param

The message to log.

Param

The context of the message (e.g. the caller).

Example

info("Hello World!", "MyFunction);

warn()

const warn: (message, context?) => void

Defined in: logger/logger.service.ts:186

Logs a warn message.

Logs a warn message.

Parameters

message

string

The message to log.

context?

string

The context of the message (e.g. the caller).

Returns

void

Example

Logger.warn('Hello World!', 'MyFunction');

Param

The message to log.

Param

The context of the message (e.g. the caller).

Example

warn('Hello World!', 'MyFunction');