Skip to content

feat: add configurable log levels via env.logLevel#1507

Merged
xenova merged 13 commits intohuggingface:mainfrom
taronsung:feat/logging-levels-117
Feb 20, 2026
Merged

feat: add configurable log levels via env.logLevel#1507
xenova merged 13 commits intohuggingface:mainfrom
taronsung:feat/logging-levels-117

Conversation

@taronsung
Copy link
Copy Markdown
Contributor

Summary

Adds configurable logging levels to control library verbosity, as requested in #117.

Changes

  • Add LogLevel enum with NONE, ERROR, WARNING, INFO, DEBUG levels
  • Add env.logLevel configuration (defaults to WARNING)
  • Create logger utility that respects the configured log level
  • Update console.log/warn calls in pipelines.js to use logger
  • Export LogLevel and logger from main entry point

Usage

import { env, LogLevel, logger } from '@huggingface/transformers';

// Set log level to suppress all logs
env.logLevel = LogLevel.NONE;

// Set log level to show only errors
env.logLevel = LogLevel.ERROR;

// Set log level to show warnings and above (default)
env.logLevel = LogLevel.WARNING;

// Set log level to show info messages and above
env.logLevel = LogLevel.INFO;

// Set log level to show all messages including debug
env.logLevel = LogLevel.DEBUG;

// Use the logger directly
logger.info('Model loaded successfully');
logger.warn('Deprecated method used');
logger.error('Failed to load model');
logger.debug('Token count:', tokens.length);

Notes

  • This PR focuses on the infrastructure and updates pipelines.js as a starting point
  • Other files with console.warn/error calls can be migrated in follow-up PRs
  • The TextStreamer output in streamers.js was intentionally not changed as it's user-facing streaming output, not logging

Closes #117

@xenova
Copy link
Copy Markdown
Collaborator

xenova commented Jan 30, 2026

Hi @taronsung 👋 Thanks for the PR! I do like this feature, and it's definitely something I think we can implement. Since we're in the process of a relatively large refactor (v4: https://github.com/huggingface/transformers.js/tree/v4), could you update the base to be that branch? From the looks of it, it'd just entail moving the new files into the packages/transformers folder... all else should stay mostly similar.

We could also take inspiration from the python transformers library... maybe implementing methods like warning_once (for example) to only log once in case of repeated calls. WDYT? Edit: Actually, maybe not worth it because browser runtimes typically collapse duplicate logs anyway.

Add LogLevel enum and env.logLevel configuration to control library verbosity.
Create logger utility that respects the configured log level.
Replace console.log/warn calls in pipelines with logger methods.

Rebased onto v4 branch and moved files to packages/transformers folder.

Closes huggingface#117
@taronsung taronsung force-pushed the feat/logging-levels-117 branch from 58097d9 to af0f0d4 Compare January 30, 2026 22:34
@taronsung taronsung changed the base branch from main to v4 January 30, 2026 22:34
Copy link
Copy Markdown
Collaborator

@nico-martin nico-martin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some commits to the PR. Most of it is just more console. that I wanted to replace with logger., but I also added a mapping between our own logLevels and the ONNX_ENV.logLevel.

Comment thread packages/transformers/src/transformers.js Outdated
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@nico-martin nico-martin self-assigned this Feb 13, 2026
@nico-martin nico-martin changed the base branch from v4 to main February 18, 2026 13:27
@xenova xenova self-assigned this Feb 18, 2026
Comment thread packages/transformers/src/env.js Outdated
Comment thread packages/transformers/src/transformers.js
@xenova
Copy link
Copy Markdown
Collaborator

xenova commented Feb 19, 2026

main comment is here; next, for some reason I'm unable to merge main into this branch. @taronsung could you rebase? alternatively, @nico-martin if you have permission, could you ensure the PR is on latest main? thanks!

@nico-martin
Copy link
Copy Markdown
Collaborator

@xenova, I merged origin/main into this branch so its now up to date with main. Imo its ready to be merged :D

// Set default log level, but allow overriding through session options
logSeverityLevel: DEFAULT_LOG_LEVEL,
// Set default log severity level, but allow overriding through session options
logSeverityLevel: getOnnxLogSeverityLevel(env.logLevel ?? 30),
Copy link
Copy Markdown
Collaborator

@xenova xenova Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might as well use our own log level dict and then use LogLevel.WARNING here (instead of a magic number)

/** @type {import('onnxruntime-common').Env} */
const ONNX_ENV = ONNX?.env;
ONNX_ENV.logLevel = LOG_LEVELS[DEFAULT_LOG_LEVEL];
ONNX_ENV.logLevel = ONNX_LOG_LEVEL_NAMES[getOnnxLogSeverityLevel(env.logLevel ?? 30)];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Comment thread packages/transformers/src/env.js Outdated
Copy link
Copy Markdown
Collaborator

@xenova xenova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for iterating!

@xenova xenova merged commit a9cd934 into huggingface:main Feb 20, 2026
6 of 7 checks passed
@fs-eire
Copy link
Copy Markdown
Contributor

fs-eire commented Mar 4, 2026

Just saw this PR. I need to mention that ONNX_ENV.logLevel only takes effect when creating the OrtEnv, which is a singleton that lives forever. If set ONNX_ENV.logLevel to "warning" and create a session and then change it to "verbose", it won't work.

Also, if setting ONNX_ENV.logLevel to "warning" but setting logSeverityLevel in session options to 0 (verbose), it will not output any logs and will cause the inference become slower. The reason is logSeverityLevel setting to 0 will cause the log string to be created and push to the log sink (which is avoid if setting logSeverityLevel to 3), but the actual logger won't output the log according to ONNX_ENV.logLevel.

A little bit confusing but need to be careful for this part. @xenova

@xenova
Copy link
Copy Markdown
Collaborator

xenova commented Mar 5, 2026

Thanks for the info @fs-eire 👍 Do you have any suggestions to improve this? Luckily it shouldn't affect most users, and the default we have chosen are much better than what they used to be (outputting far too many logs).

This is similar to a problem I described to @nico-martin, which makes session-level settings a bit more difficult to implement (since ORT uses global settings for many things, like proxying). Ref: #1479

@fs-eire
Copy link
Copy Markdown
Contributor

fs-eire commented Mar 5, 2026

based on the current limitation, we probably have something a "production" mode and a "diagnose" mode, where in production mode we can set ORT env log level to error, and in diagnose mode we set ORT env log level to verbose (a little bit slower but it's fine since it's diagnose mode). This should match most users' expectation.

@nico-martin
Copy link
Copy Markdown
Collaborator

It's not ideal, but I think it's quite rare for ORT variables to be changed by the user.
My main point with #1479 is not mecessarily about ORT variables, but about session specific variables. Like remoteHost, remotePathTemplate, etc. Those are things I am concerned because they are not linked to the environment, but rather to a specific session. Especially ehen the ecosystem grows and more dependencies use Transformers.js as a dependency. Then all of a sudden the user has no idea why installing the feature-extraction-package influences the simple-web-llm-package (one might set a different remoteHost).

TLDR: I can live with certain global dependencies that we cannot eliminate for good reasons. :D

AaronRohrbacher pushed a commit to AaronRohrbacher/transformers.js that referenced this pull request Apr 16, 2026
* feat: add configurable log levels via env.logLevel

Add LogLevel enum and env.logLevel configuration to control library verbosity.
Create logger utility that respects the configured log level.
Replace console.log/warn calls in pipelines with logger methods.

Rebased onto v4 branch and moved files to packages/transformers folder.

Closes huggingface#117

* added logger to backends/onnx.js

* added mapping from logLevel to ONNX Runtime log level

* replaced console.log with logger

* fixed doc builder

* improved jsdoc for logLevel

* removed .venv folder

* removed logger export

* changed how log levels are handled

* 30 to LogLevel.WARNING

* 30 to LogLevel.WARNING

* Remove old comments

---------

Co-authored-by: Nico Martin <mail@nico.dev>
Co-authored-by: Joshua Lochner <admin@xenova.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] Logging Level and Progress Bar for Model Downloads

5 participants