Override `log()` functions with global ones

Open
#886 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
javascript
Domain
tooling

Research direction

Start in src/common.js, reading setup(env) and the logging path around the existing self.log and createDebug.log selection. Compare the reported patch with the current behavior and verify that a user-defined global debug.log takes precedence over namespace-specific log functions while the default logger preserves existing behavior.

Written by the indexing model from the issue text.

Description

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch debug@4.3.4 for the project I'm working on.

As stated in #873, docs says that if setting a global debug.log() function, it will override the ones later specified at namespaces. This patch checks that debug.log() is different from the default one, and if so, then it has been defined by the user and will use it unconditionally.

Here is the diff that solved my problem:

diff --git a/node_modules/debug/src/common.js b/node_modules/debug/src/common.js
index e3291b2..e90e700 100644
--- a/node_modules/debug/src/common.js
+++ b/node_modules/debug/src/common.js
@@ -5,6 +5,8 @@
  */
 
 function setup(env) {
+	const globalLog = env.log;
+
 	createDebug.debug = createDebug;
 	createDebug.default = createDebug;
 	createDebug.coerce = coerce;
@@ -109,7 +111,9 @@ function setup(env) {
 			// Apply env-specific formatting (colors, etc.)
 			createDebug.formatArgs.call(self, args);
 
-			const logFn = self.log || createDebug.log;
+			const logFn = globalLog !== createDebug.log
+				? createDebug.log
+				: (self.log || createDebug.log);
 			logFn.apply(self, args);
 		}
 

This issue body was partially generated by patch-package.

Dominant language
JavaScript
Stars
11.5k
Forks
992
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from debug-js/debug

All issues in debug-js/debug

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.