Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@equinor/fusion-log

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@equinor/fusion-log - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

42

dist/esm/ConsoleLogger.js
import chalk from 'chalk';
import Logger from './Logger';
import { LogLevel } from './static';
/**
* Provides a console-based logger implementation that supports different log levels.
*
* The `ConsoleLogger` class implements the `ILogger` interface and allows for logging messages
* to the console with different log levels (debug, info, warn, error). The logger can be
* configured with a title and an optional subtitle to provide context for the log messages.
*
* The logger also supports creating sub-loggers with a new title and optional subtitle, which
* inherit the logging level from the parent logger.
*
* Example usage:
* ```typescript
* const logger = new ConsoleLogger('MainLogger');
* logger.info('This is an info message');
* logger.warn('This is a warning message');
* logger.error('This is an error message');
*
* // supports multiple messages
* logger.debug('This is a debug message', 'This is an additional message');
*
* const subLogger = logger.createSubLogger('SubLogger');
* subLogger.debug('This is a debug message from the sub-logger');
* ```
*/
export class ConsoleLogger extends Logger {
/**
* Constructs a new ConsoleLogger instance.
* @param title The main title of the logger, used to identify the source of the log messages.
* @param subtitle An optional subtitle for additional context, such as a specific component or functionality within the source.
*/
constructor(title, subtitle) {

@@ -26,2 +55,7 @@ super();

}
/**
* Generates the formatted message to log, including the title, subtitle (if any), and the messages provided.
* @param msg The messages or objects to include in the log.
* @returns An array of unknown, representing the formatted log message.
*/
_createMessage(lvl, ...msg) {

@@ -44,2 +78,10 @@ return this._formatMessage(lvl, this._formatTitle(lvl), ...msg);

}
/**
* Creates a new `ConsoleLogger` instance with a specific title and subtitle, and an optional log level.
*
* @param title - The title of the new logger, which will be prefixed to the log messages.
* @param subtitle - The subtitle of the new logger, which will be appended to the log messages.
* @param logLevel - The log level for the new logger. If not provided, the log level of the parent logger will be used.
* @returns A new ConsoleLogger instance as an ILogger.
*/
createSubLogger(title, subtitle, logLevel) {

@@ -46,0 +88,0 @@ const logger = new ConsoleLogger(`${this.title}::${title}`, subtitle !== null && subtitle !== void 0 ? subtitle : this.subtitle);

@@ -5,2 +5,13 @@ import { Subject } from 'rxjs';

import { version } from './version';
/**
* Defines an abstract base class for a logger implementation that provides common logging functionality.
*
* The `Logger` class implements the `ILogger` interface and provides the following features:
* - Configurable log level to control which messages are emitted
* - Separate observables for raw and filtered log messages
* - Convenience methods for logging at different levels (debug, info, warn, error)
* - Ability to create sub-loggers with the same configuration
*
* Concrete implementations of the `Logger` class must implement the `_createMessage` and `createSubLogger` abstract methods.
*/
export class Logger {

@@ -10,4 +21,8 @@ constructor() {

this.version = version;
// raw log, will emit all messages
this._log$ = new Subject();
}
/**
* The observable that emits log messages that are at a level greater than the logger's level.
*/
get log() {

@@ -14,0 +29,0 @@ return this._log$.pipe(filter((x) => this.level >= x.lvl));

30

dist/esm/resolve-log-level.js
import { LogLevel } from './static';
/**
* Resolves a log level from a string representation.
*
* @param key - The string representation of the log level.
* @returns The corresponding `LogLevel` value, or `undefined` if the input string does not match any known log level.
* @throws Error if the input string does not match any known log level.
*/
const resolveLogLevelFromString = (key) => {

@@ -6,11 +13,30 @@ if (typeof key !== 'string') {

}
// Convert the input key to lowercase for case-insensitive comparison
const keyLowerCase = key.toLowerCase();
const logLevelKey = Object.keys(LogLevel).find((x) => x.toLowerCase() === keyLowerCase);
const logLevelKey = Object.keys(LogLevel).find(
// Match log level key in a case-insensitive manner
(x) => x.toLowerCase() === keyLowerCase);
if (!logLevelKey) {
// Throw error if no matching log level key is found
throw new Error(`Failed to parse LogLevel from string: ${key}.`);
}
// Return the corresponding LogLevel value
return LogLevel[logLevelKey];
};
/**
* Resolves the log level from the provided value, which can be a string or a number.
*
* If the value is a number, it checks if it is a valid log level. If it is, the function returns the log level.
* If the value is not a valid log level, the function throws an error.
*
* If the value is a string, the function resolves the log level from the string using the `resolveLogLevelFromString` function.
*
* @param value - The value to resolve the log level from, can be a string or a number.
* @returns The resolved log level.
* @throws {Error} If the provided value is not a valid log level.
*/
export const resolveLogLevel = (value) => {
// Convert the level to a number
const logLevel = Number(value);
// Check if the number is a valid log level
if (Object.values(LogLevel).includes(logLevel)) {

@@ -20,6 +46,8 @@ return logLevel;

else if (isNaN(logLevel)) {
// Resolve log level from string if level is NaN
return resolveLogLevelFromString(value);
}
// Throw an error if the level is invalid
throw Error(`Invalid log level: ${value}`);
};
//# sourceMappingURL=resolve-log-level.js.map
import { resolveLogLevel } from './resolve-log-level';
/**
* Defines the different log levels that can be used to control the verbosity of logging.
*
* - `None`: No logging will be performed.
* - `Error`: Due to a more serious problem, the software has not been able to perform some function.
* - `Warning`: An indication that something unexpected happened, or indicative of some problem in the near future.
* - `Info`: Confirmation that things are working as expected.
* - `Debug`: Detailed information, typically of interest only when diagnosing problems.
*/
export var LogLevel;

@@ -10,12 +19,26 @@ (function (LogLevel) {

})(LogLevel || (LogLevel = {}));
/**
* Resolves the default log level based on the `FUSION_LOG_LEVEL` environment variable.
*
* If the `FUSION_LOG_LEVEL` environment variable is set, it will attempt to parse the log level from it.
* If the parsing fails, it will return `LogLevel.Debug` in development environments, or `LogLevel.Error` in production environments.
*
* If the `FUSION_LOG_LEVEL` environment variable is not set, it will default to `LogLevel.Error`.
*
* @returns {LogLevel} The resolved default log level.
*/
const resolveDefaultLogLevel = () => {
// Check if FUSION_LOG_LEVEL is set in environment variables
const envLogLevel = process.env.FUSION_LOG_LEVEL;
if (envLogLevel) {
try {
// Attempt to parse the log level from the environment variable
return resolveLogLevel(envLogLevel);
}
catch (error) {
// If parsing fails, return Debug level in development, otherwise Error level
return process.env.NODE_ENV === 'development' ? LogLevel.Debug : LogLevel.Error;
}
}
// Default to Error level if FUSION_LOG_LEVEL is not set
return LogLevel.Error;

@@ -22,0 +45,0 @@ };

3

dist/esm/version.js

@@ -1,2 +0,3 @@

export const version = '1.0.0';
// Generated by genversion.
export const version = '1.0.1';
//# sourceMappingURL=version.js.map
import { ILogger } from './Logger.interface';
import Logger from './Logger';
import { LogLevel } from './static';
/**
* Provides a console-based logger implementation that supports different log levels.
*
* The `ConsoleLogger` class implements the `ILogger` interface and allows for logging messages
* to the console with different log levels (debug, info, warn, error). The logger can be
* configured with a title and an optional subtitle to provide context for the log messages.
*
* The logger also supports creating sub-loggers with a new title and optional subtitle, which
* inherit the logging level from the parent logger.
*
* Example usage:
* ```typescript
* const logger = new ConsoleLogger('MainLogger');
* logger.info('This is an info message');
* logger.warn('This is a warning message');
* logger.error('This is an error message');
*
* // supports multiple messages
* logger.debug('This is a debug message', 'This is an additional message');
*
* const subLogger = logger.createSubLogger('SubLogger');
* subLogger.debug('This is a debug message from the sub-logger');
* ```
*/
export declare class ConsoleLogger extends Logger {
protected readonly title: string;
protected readonly subtitle?: string | undefined;
/**
* Constructs a new ConsoleLogger instance.
* @param title The main title of the logger, used to identify the source of the log messages.
* @param subtitle An optional subtitle for additional context, such as a specific component or functionality within the source.
*/
constructor(title: string, subtitle?: string | undefined);
/**
* Generates the formatted message to log, including the title, subtitle (if any), and the messages provided.
* @param msg The messages or objects to include in the log.
* @returns An array of unknown, representing the formatted log message.
*/
protected _createMessage(lvl: LogLevel, ...msg: unknown[]): unknown[];
protected _formatTitle(_lvl: LogLevel): string;
protected _formatMessage(lvl: LogLevel, ...msg: unknown[]): unknown[];
/**
* Creates a new `ConsoleLogger` instance with a specific title and subtitle, and an optional log level.
*
* @param title - The title of the new logger, which will be prefixed to the log messages.
* @param subtitle - The subtitle of the new logger, which will be appended to the log messages.
* @param logLevel - The log level for the new logger. If not provided, the log level of the parent logger will be used.
* @returns A new ConsoleLogger instance as an ILogger.
*/
createSubLogger(title: string, subtitle?: string, logLevel?: LogLevel): ILogger;
}
import { Subject, type Observable } from 'rxjs';
import { LogLevel } from './static';
import { ILogger } from './Logger.interface';
/**
* Defines an abstract base class for a logger implementation that provides common logging functionality.
*
* The `Logger` class implements the `ILogger` interface and provides the following features:
* - Configurable log level to control which messages are emitted
* - Separate observables for raw and filtered log messages
* - Convenience methods for logging at different levels (debug, info, warn, error)
* - Ability to create sub-loggers with the same configuration
*
* Concrete implementations of the `Logger` class must implement the `_createMessage` and `createSubLogger` abstract methods.
*/
export declare abstract class Logger implements ILogger {
level: LogLevel;
readonly version = "1.0.0";
readonly version = "1.0.1";
protected readonly _log$: Subject<{

@@ -11,2 +22,5 @@ lvl: LogLevel;

}>;
/**
* The observable that emits log messages that are at a level greater than the logger's level.
*/
get log(): Observable<{

@@ -13,0 +27,0 @@ lvl: LogLevel;

import { LogLevel } from './static';
/**
* Defines the interface for a logger that can be used to log messages at different severity levels.
*/
export interface ILogger {
/**
* Logging level indicating the severity of messages to log.
* @see {@link LogLevel}
*/
level: LogLevel;
/**
* Logs a debug message. Debug messages are detailed information, considered useful only during development and debugging.
* @param msg The messages or objects to log.
*/
debug(...msg: unknown[]): void;
/**
* Logs an informational message. Info messages convey general information about the application's execution.
* @param msg The messages or objects to log.
*/
info(...msg: unknown[]): void;
/**
* Logs a warning message. Warning messages indicate potential issues or important, but not critical, information.
* @param msg The messages or objects to log.
*/
warn(...msg: unknown[]): void;
/**
* Logs an error message. Error messages indicate a failure from which the application may not be able to recover.
* @param msg The messages or objects to log.
*/
error(...msg: unknown[]): void;
/**
* Creates a new sub-logger with a specific title.
*
* @param args - Additional arguments to pass to the sub-logger creation.
* @returns A new `ILogger` instance representing the sub-logger.
*/
createSubLogger(...args: unknown[]): ILogger;
}
import { LogLevel } from './static';
/**
* Resolves the log level from the provided value, which can be a string or a number.
*
* If the value is a number, it checks if it is a valid log level. If it is, the function returns the log level.
* If the value is not a valid log level, the function throws an error.
*
* If the value is a string, the function resolves the log level from the string using the `resolveLogLevelFromString` function.
*
* @param value - The value to resolve the log level from, can be a string or a number.
* @returns The resolved log level.
* @throws {Error} If the provided value is not a valid log level.
*/
export declare const resolveLogLevel: (value: string | number) => LogLevel;

@@ -0,8 +1,17 @@

/**
* Defines the different log levels that can be used to control the verbosity of logging.
*
* - `None`: No logging will be performed.
* - `Error`: Due to a more serious problem, the software has not been able to perform some function.
* - `Warning`: An indication that something unexpected happened, or indicative of some problem in the near future.
* - `Info`: Confirmation that things are working as expected.
* - `Debug`: Detailed information, typically of interest only when diagnosing problems.
*/
export declare enum LogLevel {
None = 0,
Error = 1,
Warning = 2,
Info = 3,
None = 0,// No logging will be performed.
Error = 1,// Due to a more serious problem, the software has not been able to perform some function.
Warning = 2,// An indication that something unexpected happened, or indicative of some problem in the near future.
Info = 3,// Confirmation that things are working as expected.
Debug = 4
}
export declare const defaultLogLevel: LogLevel;

@@ -1,1 +0,1 @@

export declare const version = "1.0.0";
export declare const version = "1.0.1";
{
"name": "@equinor/fusion-log",
"version": "1.0.0",
"version": "1.0.1",
"description": "Log utils for Fusion front-end development",

@@ -45,4 +45,4 @@ "keywords": [

"@types/node": "^20.11.14",
"typescript": "^5.4.2",
"vitest": "^1.6.0"
"typescript": "^5.5.3",
"vitest": "^2.0.1"
},

@@ -49,0 +49,0 @@ "peerDependencies": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc