Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@equinor/fusion-log
Advanced tools
Log utils for Fusion front-end development
This package provides a set of utilities for logging messages in a TypeScript/JavaScript application within the Fusion Framework. The primary purpose of this code is to provide a consistent and configurable logging mechanism that can be used throughout the application.
The logging utilities in this package are designed to provide a flexible and extensible logging solution. The main components are:
Logger
(abstract class): Defines the base interface and functionality for logging messages with different severity levels (debug, info, warn, error).ConsoleLogger
: A concrete implementation of the Logger
class that logs messages to the console.LogLevel
(enum): Defines the different log levels that can be used to control the verbosity of logging.resolveLogLevel
(function): Resolves a log level from a string or number representation.[!WARNING] Ensure that
FUSION_LOG_LEVEL
is set in your package manager's environment variables before compiling the code for production. Learn more about setting the log level as an environment variable
The ConsoleLogger
class is a concrete implementation of the Logger
class. It logs messages to the console using the appropriate console methods (console.debug
, console.info
, console.warn
, console.error
). The ConsoleLogger
class also supports creating sub-loggers with a new title and optional subtitle.
import { ConsoleLogger, LogLevel } from '@equinor/fusion-log';
// Create a new ConsoleLogger instance
const logger = new ConsoleLogger('MyApplication');
// Set the logging level
logger.level = LogLevel.Debug;
// Log messages at different severity levels
logger.debug('This is a debug message');
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message');
// Create a sub-logger
const subLogger = logger.createSubLogger('SubComponent');
subLogger.debug('This is a debug message from the sub-logger');
The LogLevel
enum represents the different log levels that can be used to control the verbosity of logging. It includes the following levels:
The logging level can be configured using the FUSION_LOG_LEVEL
environment variable. If the environment variable is set, the resolveDefaultLogLevel function will attempt to parse the log level from it. If the parsing fails, it will default to LogLevel.Debug in development environments or LogLevel.Error in production environments.
# .env file
FUSION_LOG_LEVEL=4
# or
FUSION_LOG_LEVEL=debug
Developers can define their own logger either by extending the Logger
class or by implementing the ILogger
interface. Below are examples of both approaches.
Logger
ClassThe
Logger
class is an abstract class that implements theILogger
interface. It provides basic functionality for creating messages withchalk
.
To create a custom logger by extending the Logger
class, you need to create a new class that inherits from Logger
and override the necessary methods.
import { Logger, LogLevel } from '@equinor/fusion-log';
class CustomLogger extends Logger {
constructor(title: string) {
super(title);
// handle messages filtered by logLevel
this.log.subscribe(({lvl,msg}) => {
writeToMyDatabase(lvl, msg);
});
// handle all messages
this._log$.subscribe(({lvl,msg}) => {
writeToMyDatabase(lvl, msg); // custom handling of messages
});
}
// Override the log method to customize logging behavior
protected _createMessage(...msg: unknown[]): unknown[] {
return {
title: this.title,
timestamp: Date.Now(),
message: msg.map(convertMessageToString).join('\n'),
} // custom message
}
}
ILogger
InterfaceThe
ILogger
interface defines the base interface and functionality for logging messages with different severity levels (debug, info, warn, error).
To create a custom logger by implementing the ILogger
interface, you need to create a new class that implements all the methods defined in the ILogger
interface.
The defaultLogLevel
will be resolved by default. If the FUSION_LOG_LEVEL
environment variable is set, the resolveDefaultLogLevel function will attempt to parse the log level from it. If the parsing fails, it will default to LogLevel.Debug
in development environments or LogLevel.Error
in production environments.
import { defaultLogLevel, type ILogger } from '@equinor/fusion-log';
class CustomLogger implements ILogger {
logLevel: LogLevel = defaultLogLevel;
}
The resolveLogLevel
function resolves a log level from a string or number representation. It handles both cases and throws an error if the provided value is not a valid log level.
import { resolveLogLevel } from '@equinor/fusion-log';
logger.logLevel = resolveLogLevel(process.env.MY_LOG_LEVEL);
FAQs
Log utils for Fusion front-end development
We found that @equinor/fusion-log demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.