How should I implement verbose logging in an npm package?
up vote
0
down vote
favorite
I have created an npm package (for internal use) that can be used as follows. Note the verboseLogging
option.
import * as q from 'q'
var client = q.createClient({verboseLogging: true})
I have implemented the verboseLogging
as plenty of console.log(...)
statements.
This works well when the consumer of the package is a NodeJS command line application, but not as well when the consumer of the package is a browser.
When I consume this package in a browser application, I have needed to do something like the following to get the logs displayed to the user:
const originalConsoleLog = console.log;
console.log = function() {
// extract log message and save somewhere to be shown in DOM
originalConsoleLog.apply(this, arguments)
}
Is there maybe a better way to enable logging in an npm package that is accessible to both command line apps and browser apps?
node.js logging npm
add a comment |
up vote
0
down vote
favorite
I have created an npm package (for internal use) that can be used as follows. Note the verboseLogging
option.
import * as q from 'q'
var client = q.createClient({verboseLogging: true})
I have implemented the verboseLogging
as plenty of console.log(...)
statements.
This works well when the consumer of the package is a NodeJS command line application, but not as well when the consumer of the package is a browser.
When I consume this package in a browser application, I have needed to do something like the following to get the logs displayed to the user:
const originalConsoleLog = console.log;
console.log = function() {
// extract log message and save somewhere to be shown in DOM
originalConsoleLog.apply(this, arguments)
}
Is there maybe a better way to enable logging in an npm package that is accessible to both command line apps and browser apps?
node.js logging npm
1
what issue is it causing?
– vibhor1997a
17 hours ago
@vibhor1997a: The only issue is that I must monkey patch console.log
– user1283776
16 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have created an npm package (for internal use) that can be used as follows. Note the verboseLogging
option.
import * as q from 'q'
var client = q.createClient({verboseLogging: true})
I have implemented the verboseLogging
as plenty of console.log(...)
statements.
This works well when the consumer of the package is a NodeJS command line application, but not as well when the consumer of the package is a browser.
When I consume this package in a browser application, I have needed to do something like the following to get the logs displayed to the user:
const originalConsoleLog = console.log;
console.log = function() {
// extract log message and save somewhere to be shown in DOM
originalConsoleLog.apply(this, arguments)
}
Is there maybe a better way to enable logging in an npm package that is accessible to both command line apps and browser apps?
node.js logging npm
I have created an npm package (for internal use) that can be used as follows. Note the verboseLogging
option.
import * as q from 'q'
var client = q.createClient({verboseLogging: true})
I have implemented the verboseLogging
as plenty of console.log(...)
statements.
This works well when the consumer of the package is a NodeJS command line application, but not as well when the consumer of the package is a browser.
When I consume this package in a browser application, I have needed to do something like the following to get the logs displayed to the user:
const originalConsoleLog = console.log;
console.log = function() {
// extract log message and save somewhere to be shown in DOM
originalConsoleLog.apply(this, arguments)
}
Is there maybe a better way to enable logging in an npm package that is accessible to both command line apps and browser apps?
node.js logging npm
node.js logging npm
asked 17 hours ago
user1283776
3,0262152104
3,0262152104
1
what issue is it causing?
– vibhor1997a
17 hours ago
@vibhor1997a: The only issue is that I must monkey patch console.log
– user1283776
16 hours ago
add a comment |
1
what issue is it causing?
– vibhor1997a
17 hours ago
@vibhor1997a: The only issue is that I must monkey patch console.log
– user1283776
16 hours ago
1
1
what issue is it causing?
– vibhor1997a
17 hours ago
what issue is it causing?
– vibhor1997a
17 hours ago
@vibhor1997a: The only issue is that I must monkey patch console.log
– user1283776
16 hours ago
@vibhor1997a: The only issue is that I must monkey patch console.log
– user1283776
16 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
One of possible solutions is to check if window
variable id defined. Since it only presents in a browser, you can try something like this:
if (typeof window === 'undefined') {
// we're server side, let console.log as is
} else {
// browser environment, DOM is accessible, additional logging goes here
}
New contributor
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
One of possible solutions is to check if window
variable id defined. Since it only presents in a browser, you can try something like this:
if (typeof window === 'undefined') {
// we're server side, let console.log as is
} else {
// browser environment, DOM is accessible, additional logging goes here
}
New contributor
add a comment |
up vote
1
down vote
One of possible solutions is to check if window
variable id defined. Since it only presents in a browser, you can try something like this:
if (typeof window === 'undefined') {
// we're server side, let console.log as is
} else {
// browser environment, DOM is accessible, additional logging goes here
}
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
One of possible solutions is to check if window
variable id defined. Since it only presents in a browser, you can try something like this:
if (typeof window === 'undefined') {
// we're server side, let console.log as is
} else {
// browser environment, DOM is accessible, additional logging goes here
}
New contributor
One of possible solutions is to check if window
variable id defined. Since it only presents in a browser, you can try something like this:
if (typeof window === 'undefined') {
// we're server side, let console.log as is
} else {
// browser environment, DOM is accessible, additional logging goes here
}
New contributor
New contributor
answered 17 hours ago
Anton Pastukhov
554
554
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407837%2fhow-should-i-implement-verbose-logging-in-an-npm-package%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
what issue is it causing?
– vibhor1997a
17 hours ago
@vibhor1997a: The only issue is that I must monkey patch console.log
– user1283776
16 hours ago