SeleniumHQ/selenium · error · TypeError
Level must be >= 0
Error message
Level must be >= 0
What it means
Thrown by the Level constructor (logging.js) when a custom log level is created with a negative numeric value. Log levels are ordered by non-negative numeric severity (OFF=Infinity down to ALL=0), so negatives are meaningless. It is a TypeError, not a WebDriver protocol error.
Source
Thrown at javascript/selenium-webdriver/lib/logging.js:79
* APIs exposed by this module for it are non-frozen. This module will be
* updated, possibly breaking backwards-compatibility, once logging is
* officially defined by the
* [W3C WebDriver spec](http://www.w3.org/TR/webdriver/).
*/
/**
* Defines a message level that may be used to control logging output.
*
* @final
*/
class Level {
/**
* @param {string} name the level's name.
* @param {number} level the level's numeric value.
*/
constructor(name, level) {
if (level < 0) {
throw new TypeError('Level must be >= 0')
}
/** @private {string} */
this.name_ = name
/** @private {number} */
this.value_ = level
}
/** This logger's name. */
get name() {
return this.name_
}
/** The numeric log level. */
get value() {
return this.value_
}View on GitHub (pinned to aa36b38e69)
Solutions
- Use a non-negative value: new logging.Level('CUSTOM', 500).
- Clamp derived values: Math.max(0, base - offset).
- Reuse the built-in constants (Level.DEBUG/INFO/WARNING/SEVERE/OFF/ALL) instead of constructing custom levels.
- Validate config input is a finite non-negative number before constructing the Level.
Example fix
// before
const lvl = new logging.Level('trace', base.value - 1000); // can be negative
// after
const lvl = new logging.Level('trace', Math.max(0, base.value - 1000)); Defensive patterns
Strategy: validation
Validate before calling
function makeLevel(name, value) {
if (!Number.isFinite(value) || value < 0) {
throw new TypeError(`level value must be a finite non-negative number, got ${value}`);
}
return new logging.Level(name, value);
} Type guard
function isValidLevelValue(v) {
return typeof v === 'number' && Number.isFinite(v) && v >= 0;
} Try / catch
try {
return new logging.Level(name, derived);
} catch (e) {
if (e instanceof TypeError && /Level must be >= 0/.test(e.message)) {
return new logging.Level(name, Math.max(0, derived));
}
throw e;
} Prevention
- Clamp derived levels with Math.max(0, x).
- Prefer built-in Level constants over custom ones.
- Treat -1 sentinels from config as 'unset' and map to a real level.
When it happens
Trigger: new logging.Level('CUSTOM', -1); computing a level from user input or a delta without clamping, e.g. new Level(name, base.value - offset) where offset exceeds base.
Common situations: Subtracting severities to derive a 'quieter' level and going below zero; reading a level number from env/config that defaults to -1 as a sentinel; porting code from a library whose levels start at 1.
Related errors
- not a string or number
- log_level must be one of: {', '.join(levels)}
- Invalid URL: ${aUrl}
- no capabilities provided for merge
- Capability keys must be strings: ${typeof key}
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/9974a067d1e786b9.
Report an issue: GitHub.