Reputation: 21784
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?
Upvotes: 0
Views: 458
Reputation: 628
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
}
Upvotes: 1