preact-context
preact-context copied to clipboard
Make warnings and logging configurable.
- Added interface
Logwith methodwarn - Added interface
Optionswith fields:providerOptional:Booleanlog:Log
- Replaced
noopEmitterwith emitter factorycreateDefaultEmitter - Added argument of type
OptionstocreateDefaultEmitter - Added argument of type
OptionstocreateContext- Default
providerOptionaloption tofalse - Default
logoption toconsoleif it exists
- Default
createContextcreates adefaultEmitterby callingcreateDefaultEmitterand passing the options to it- Use the
defaultEmitterwhen there is no emitter - Replace
console.warnwith calls tolog.warn
Fixes #24
So I noticed an issue... when context would be created with an options object but without a log option set, it would not have used console... Fixed that, squashed commits then force-pushed. I think it should be ok now... Let me know if you need more changes.
EDIT: Mmm I messed up the commit message.. it is duplicated... anyway maybe you can live with it?
Looks good. Can you also make the logger a global?
Hi @valotas
Can you describe how you would make it global?
And would it be an idea to just use https://npmjs.com/package/ulog ?
I wrote this package one day to solve 'the logger problem' for all my packages in one go. It is optimized to be very small (~1kB) and very configurable. Using ulog I would implement the logging like this:
import ulog from "ulog";
const log = ulog('preact-context')
// ...
log.warn('message');
The library user can then use ulog to configure the logging. E.g. setting a key in localStorage log=info would enable info logging throughout the codebase. Setting debug=preact-context would enable debug mode for all loggers named 'preact-context' etc. It also allows injecting formatters etc.
Of course that would mean an extra dependency...
@valotas Can you share your ideas for the logging?
Hi @Download, excuse me for the late reply. I would export an options = { warn: console.warn } object. This is the preact way, so I believe it is good to follow the same pattern
@valotas
In the meantime I built a logging library especially for libraries. It is only ~360 bytes minified and gzipped. Would it be an option?