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 applicationLogger.level = 'debug';
// Logging inside a functionLogger.debug('Hello World!', 'MyFunction');
// Logging inside a classLogger.info('Hello World!', this.constructor.name);
// Logging without a contextLogger.warn('Hello World!');
// You may also directly import the logging functionsimport { error } from '@spuxx/js-utils';error('Hello World!');
// Access the stored messagesLogger.messages.forEach((message) => console.log(JSON.stringify(message)));Extends
Service<Logger,this>
Constructors
Constructor
protectednew Logger():Logger
Defined in: mixin/service-mixin.ts:25
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
Inherited from
ServiceMixin<Logger>().constructor
Accessors
instance
Get Signature
get
staticinstance():TService
Defined in: mixin/service-mixin.ts:34
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
staticlevel():LogLevel
Defined in: logger/logger.service.ts:67
Returns the Loggers log level.
Example
Logger.setLevel('debug');console.log(Logger.level); // Output: debugReturns
The log level.
messages
Get Signature
get
staticmessages():LogMessage[]
Defined in: logger/logger.service.ts:90
Returns all stored log messages.
Example
console.log(Logger.messages.length); // Output: 0Logger.debug('Hello World!');console.log(Logger.messages.length); // Output: 1Returns
The list of stored log messages.
Methods
debug()
staticdebug(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()
staticdestroy():void
Defined in: mixin/service-mixin.ts:48
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 instanceInherited from
ServiceMixin<Logger>().destroy
error()
staticerror(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()
staticinfo(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()
staticsetLevel(level):void
Defined in: logger/logger.service.ts:78
Sets the Loggers log level.
Parameters
level
The log level to set.
Returns
void
Example
Logger.setLevel('debug');console.log(Logger.level); // Output: debugwarn()
staticwarn(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?
optionalcontext: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
constdebug:any
Defined in: logger/logger.service.ts:170
Logs a debug message.
Param
The message to log.
Param
The context of the message (e.g. the caller).
Example
debug('Hello World!', 'MyFunction');error
consterror:any
Defined in: logger/logger.service.ts:194
Logs an error message.
Param
The message to log.
Param
The context of the message (e.g. the caller).
Example
error('Hello World!', 'MyFunction');info
constinfo:any
Defined in: logger/logger.service.ts:178
Logs an info message.
Param
The message to log.
Param
The context of the message (e.g. the caller).
Example
info("Hello World!", "MyFunction);warn
constwarn:any
Defined in: logger/logger.service.ts:186
Logs a warn message.
Param
The message to log.
Param
The context of the message (e.g. the caller).
Example
warn('Hello World!', 'MyFunction');