@equinor/fusion-log

中文文档
2.0.8发布于 2026/11/12ISCTypeScriptES Module近 30 天下载 1.3万

Log utils for Fusion front-end development

原始使用文档
文档目录8 节
  1. Key Exports
  2. Quick Start
  3. Log Levels
  4. Configure via Environment Variable
  5. Sub-Loggers
  6. Custom Loggers
  7. Extending Logger
  8. Implementing ILogger

@equinor/fusion-log

Structured, level-filtered logging utilities for Fusion Framework applications.

@equinor/fusion-log gives you a thin, RxJS-backed logging abstraction with a ready-made console implementation and helpers for resolving log levels from environment variables. Use it whenever you need consistent, configurable log output across modules.

Key Exports

ExportKindPurpose
ConsoleLoggerClassColour-coded console logger with title prefixes and sub-logger support.
LoggerAbstract classBase class to extend when building custom log destinations.
ILoggerInterfaceContract satisfied by every logger — use for dependency injection.
LogLevelEnumSeverity constants (None, Error, Warning, Info, Debug).
defaultLogLevelConstantThe level resolved at startup from FUSION_LOG_LEVEL.
resolveLogLevelFunctionParse a string or number into a type-safe LogLevel.

Quick Start

import { ConsoleLogger, LogLevel } from '@equinor/fusion-log';

const logger = new ConsoleLogger('MyApp');
logger.level = LogLevel.Debug;

logger.debug('bootstrapping modules…');
logger.info('application ready');
logger.warn('deprecated API called');
logger.error('failed to fetch config', err);

Log Levels

Higher numeric values are more verbose. Setting logger.level to a given level enables that level and all levels below it.

LevelValueWhen to use
None0Disable all logging.
Error1An operation failed and the caller should be aware.
Warning2Something unexpected that may need investigation.
Info3Routine confirmation (e.g. "module initialised").
Debug4Verbose diagnostics for development only.

Configure via Environment Variable

Set FUSION_LOG_LEVEL before your bundler compiles the code so the default level is inlined at build time.

# .env
FUSION_LOG_LEVEL=4        # numeric
# or
FUSION_LOG_LEVEL=debug    # case-insensitive name

[!WARNING] Ensure that FUSION_LOG_LEVEL is set in your package manager's environment variables before compiling the code for production.

Resolution rules (applied once at module load):

  1. If FUSION_LOG_LEVEL is set and parsable → use the parsed value.
  2. If parsing fails and NODE_ENV === 'development' → fall back to Debug.
  3. Otherwise → fall back to Error.
  4. If the variable is not set → default to Error.

You can also resolve a level at runtime with resolveLogLevel:

import { resolveLogLevel } from '@equinor/fusion-log';

const level = resolveLogLevel(process.env.MY_LOG_LEVEL ?? 'error');
logger.level = level;

Sub-Loggers

Create child loggers to add component-level context. The child's title is prefixed with the parent's title, and the log level is inherited unless overridden.

const router = logger.createSubLogger('Router');
router.debug('navigated to /dashboard');
// console output: "MyApp::Router  navigated to /dashboard"

const auth = logger.createSubLogger('Auth', undefined, LogLevel.Error);
auth.info('token refreshed'); // suppressed — level is Error

Custom Loggers

Extending Logger

Subclass Logger to route log entries to any destination while reusing level filtering and the RxJS observable pipeline.

import { Logger, LogLevel, type ILogger } from '@equinor/fusion-log';

class RemoteLogger extends Logger {
  constructor(private readonly endpoint: string) {
    super();
    // subscribe to the filtered stream
    this.log.subscribe(({ lvl, msg }) => {
      fetch(this.endpoint, {
        method: 'POST',
        body: JSON.stringify({ level: lvl, messages: msg }),
      });
    });
  }

  protected _createMessage(_lvl: LogLevel, ...msg: unknown[]): unknown[] {
    return msg;
  }

  createSubLogger(title: string): ILogger {
    const sub = new RemoteLogger(this.endpoint);
    sub.level = this.level;
    return sub;
  }
}

Implementing ILogger

If you don't need the RxJS pipeline, implement ILogger directly:

import { type ILogger, LogLevel, defaultLogLevel } from '@equinor/fusion-log';

class NoopLogger implements ILogger {
  level: LogLevel = defaultLogLevel;
  debug() {}
  info() {}
  warn() {}
  error() {}
  createSubLogger(): ILogger {
    return new NoopLogger();
  }
}

版本与发布记录

@equinor/fusion-log 共发布 41 个版本,首次发布于 2024/05/22;过去 12 个月发布了 16 个版本。

版本发布日期距今
2.0.8latest2026/11/121 天前
2.0.8-next.0next2026/11/112 天前
2.0.72026/10/1727 天前
2.0.7-trusted-publishing-20261015-9dfe7602cd2a49a8a9df00d62c26e7304a82a85atrusted-publishing-202610152026/10/1629 天前
2.0.62026/10/121 个月前
2.0.52026/09/151 个月前

其他发布通道:apploader → 1.1.5-apploader-be21a6b60eb13eee8bf869e75002d8634e9c50a9,cli-search-index → 1.1.8-cli-search-index.0,msal-v5 → 1.1.8-msal-v5.0,trusted-publishing-20261015 → 2.0.7-trusted-publishing-20261015-9dfe7602cd2a49a8a9df00d62c26e7304a82a85a,next → 2.0.8-next.0。

关于 @equinor/fusion-log 的常见问题

@equinor/fusion-log 是什么?

Log utils for Fusion front-end development

如何安装 @equinor/fusion-log?

在项目目录运行 npm install @equinor/fusion-log,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:rxjs(>=7)、chalk(>=5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@equinor/fusion-log 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@equinor/fusion-log 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@equinor/fusion-log 可以商用吗?

@equinor/fusion-log 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@equinor/fusion-log 的下载量有多少?

根据 npm 官方下载统计,@equinor/fusion-log 在 2026/10/14 – 2026/11/12 这 30 天内被下载 12,845 次,日均约 428 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@equinor/fusion-log 还在维护吗?

最新版本 2.0.8 发布于 2026/11/12(1 天前);至今共发布 41 个版本,其中过去 12 个月发布了 16 个;首次发布于 2024/05/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。