Skip to content

Use log level to manage logging verbosity #261#313

Open
thompson-tomo wants to merge 8 commits intothlorenz:masterfrom
thompson-tomo:feature/#261_configurableLogLevels
Open

Use log level to manage logging verbosity #261#313
thompson-tomo wants to merge 8 commits intothlorenz:masterfrom
thompson-tomo:feature/#261_configurableLogLevels

Conversation

@thompson-tomo
Copy link
Contributor

Closes #261

This makes the logging level configurable to reduce verbosity at low levels

@thompson-tomo thompson-tomo force-pushed the feature/#261_configurableLogLevels branch from 111e24d to b4c9291 Compare February 21, 2026 02:45
@AndrewSouthpaw
Copy link
Collaborator

Sorry, looks like some conflicts need to be resolved now.

@thompson-tomo
Copy link
Contributor Author

No worries @AndrewSouthpaw i have resolved the conflicts.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds configurable logging verbosity to doctoc so users can reduce noisy output (e.g., only show “Found nothing …” in more verbose modes), addressing #261.

Changes:

  • Introduces --loglevel/-l support via the loglevel dependency and routes most CLI output through log levels.
  • Adjusts stdout-related tests/fixtures to account for loglevel-controlled output.
  • Updates docs to describe log level configuration.

Reviewed changes

Copilot reviewed 6 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
doctoc.js Adds loglevel, wires --loglevel, and shifts many console.* calls to level-based logging.
lib/file.js Moves directory scan messages to debug/trace to reduce default noise.
package.json Adds loglevel dependency.
package-lock.json Locks loglevel dependency.
test/transform-stdout.js Updates/extends stdout tests to cover default vs trace logging.
test/fixtures/stdout_trace.log New expected stdout output for trace logging.
test/fixtures/stdout_info.log New expected stdout output for default info logging.
test/transform.js Switches helper output to use logger (but currently missing logger import).
README.md Documents --loglevel usage and supported levels.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

thompson-tomo and others added 2 commits March 1, 2026 22:10
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 9 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

doctoc.js:160

  • There are still several console.error(...) calls in argument validation / fatal error paths (e.g., TOC title padding and --stdout misuse), while other errors were migrated to log.error. This makes logging behavior inconsistent and bypasses the new --loglevel plumbing. Consider switching these remaining console.error calls to log.error (or centralizing through printUsageAndExit(true) where appropriate) for consistent output behavior.
var padBeforeTitle = argv.toctitlepaddingbefore;
if (padBeforeTitle && isNaN(padBeforeTitle) || padBeforeTitle < 0) { console.error('Padding before title specified is not a positive number: ' + padBeforeTitle), printUsageAndExit(true); }
else if (padBeforeTitle && padBeforeTitle > 1) { console.error('Padding before title: ' + padBeforeTitle + ' is not currently supported as greater than 1'), printUsageAndExit(true); }
else if (padBeforeTitle || notitle) { padTitle = true; }

var maxHeaderLevel = argv.m || argv.maxlevel;

var logLevel = argv.l || argv.loglevel || "info";

log.setLevel(logLevel, false);

if (maxHeaderLevel && isNaN(maxHeaderLevel)) { log.error('Max. heading level specified is not a number: ' + maxHeaderLevel), printUsageAndExit(true); }

var minHeaderLevel = argv.minlevel || 1;
if (minHeaderLevel && isNaN(minHeaderLevel) || minHeaderLevel < 0) { log.error('Min. heading level specified is not a positive number: ' + minHeaderLevel), printUsageAndExit(true); }
else if (minHeaderLevel && minHeaderLevel > 2) { log.error('Min. heading level: ' + minHeaderLevel + ' is not currently supported as greater than 2'), printUsageAndExit(true); }
if (maxHeaderLevel && maxHeaderLevel < minHeaderLevel) { log.error('Max. heading level: ' + maxHeaderLevel + ' is less than the defined Min. heading level: ' + minHeaderLevel), printUsageAndExit(true); }

if (argv._.length > 1 && stdOut) {
  console.error('--stdout cannot be used to process multiple files/directories. Use --dryrun instead.');
  process.exitCode = 2;
  return;
}

for (var i = 0; i < argv._.length; i++) {
  var target = cleanPath(argv._[i]),
    stat = fs.statSync(target);

  if (stat.isDirectory() && stdOut) {
    console.error('--stdout cannot be used to process a directory. Use --dryrun instead.');
    process.exitCode = 2;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Collaborator

@AndrewSouthpaw AndrewSouthpaw left a comment

Choose a reason for hiding this comment

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

A couple small things to address, but otherwise lgtm

catch (e) {
log.error('Unknown log level: ' + logLevel);
log.error('Supported options: trace, debug, info, warn, error');
log.setLevel("info", false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably throw from here, rather than quietly continue operation. I think invalid flags usually trigger an abort from the command. WDYT?

Copy link
Contributor Author

@thompson-tomo thompson-tomo Mar 3, 2026

Choose a reason for hiding this comment

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

I have no strong opinion, the reason why I went with a default is that the setting has 0 impact on the output hence if we continue the produced output wouldn't be any different.

Comment on lines +149 to +154
if (maxHeaderLevel && isNaN(maxHeaderLevel)) { log.error('Max. heading level specified is not a number: ' + maxHeaderLevel), printUsageAndExit(true); }

if (maxHeaderLevel && maxHeaderLevel < minHeaderLevel) { console.error('Max. heading level: ' + maxHeaderLevel + ' is less than the defined Min. heading level: ' + minHeaderLevel), printUsageAndExit(true); }
var minHeaderLevel = argv.minlevel || 1;
if (minHeaderLevel && isNaN(minHeaderLevel) || minHeaderLevel < 0) { log.error('Min. heading level specified is not a positive number: ' + minHeaderLevel), printUsageAndExit(true); }
else if (minHeaderLevel && minHeaderLevel > 2) { log.error('Min. heading level: ' + minHeaderLevel + ' is not currently supported as greater than 2'), printUsageAndExit(true); }
if (maxHeaderLevel && maxHeaderLevel < minHeaderLevel) { log.error('Max. heading level: ' + maxHeaderLevel + ' is less than the defined Min. heading level: ' + minHeaderLevel), printUsageAndExit(true); }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you explain the changes here? They seem unrelated to supporting different log levels. (It's fine to roll them into the same PR, seems like a net improvement.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There isn't any change other than the logger used. diff is bigger than expected due to it shifting lines down because of the log level validation.

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.

less noisy output

3 participants