https://github.com/andrejewski/console-hook
Hook into the console
https://github.com/andrejewski/console-hook
Last synced: 7 months ago
JSON representation
Hook into the console
- Host: GitHub
- URL: https://github.com/andrejewski/console-hook
- Owner: andrejewski
- License: isc
- Created: 2016-08-01T12:41:52.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-09-23T21:24:25.000Z (over 9 years ago)
- Last Synced: 2025-05-13T21:51:39.547Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Console Hook
Hook into the `console` for transparent log recording in production and log capture for testing.
```sh
npm install console-hook
```
Works in the browser too, just grab `console-hook/index.js`.
## Examples
### Intercept all `console` method calls
```js
var Hook = require('console-hook');
var myHook = Hook().attach((method, args) => {
// method is the console[method] string
// args is the arguments object passed to console[method]
});
// okay, we're done playing with the console stuffs
myHook.detach();
```
### Intercept all `console` method calls and don't call `console`
```js
var Hook = require('console-hook');
var silence = true; // could be `isProduction`
var myHook = Hook(console, silence).attach((method, args) => {
// method is the console[method] string
// args is the arguments object passed to console[method]
});
// okay, we're done playing with the console stuffs
myHook.detach();
```
### Intercept only `console.error` calls
```js
var Hook = require('console-hook');
var myHook = Hook().attach('error', (method, args) => {
// method is the console[method] string, always "error"
// args is the arguments object passed to console[method]
});
// okay, we're done playing with the console stuffs
myHook.detach();
```
### Use another `console`-like Logger
```js
// if you have an Ember app and already use Ember.Logger
var Hook = require('console-hook');
var myHook = Hook(Ember.Logger).attach((method, args) => {
// method is the console[method] string
// args is the arguments object passed to console[method]
});
// okay, we're done playing with the console stuffs
myHook.detach();
```
## Contributing
Contributions are incredibly welcome as long as they are standardly applicable
and pass the tests (or break bad ones). Tests are written in Mocha and
assertions are done with the Node.js core `assert` module.
```bash
# running tests
npm run test
```
Follow me on [Twitter](https://twitter.com/compooter) for updates or just for
the lolz and please check out my other [repositories](https://github.com/andrejewski)
if I have earned it. I thank you for reading.