├── .travis.yml ├── anylogger-debug.js ├── .github └── workflows │ └── nodejs.yml ├── test.html ├── rollup.config.js ├── LICENSE ├── .gitignore ├── package.json ├── README.md └── anylogger-debug.spec.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | -------------------------------------------------------------------------------- /anylogger-debug.js: -------------------------------------------------------------------------------- 1 | import anylogger from 'anylogger' 2 | import debug from 'debug' 3 | 4 | // override anylogger.ext() to make every log method use debug 5 | anylogger.ext = function(logger) { 6 | var method = debug(logger.name) 7 | for (var level in anylogger.levels) { 8 | logger[level] = method 9 | } 10 | logger.enabledFor = debug.enabled.bind(logger, logger.name) 11 | return logger 12 | } 13 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | node-version: [10.x, 12.x] 13 | 14 | steps: 15 | - uses: actions/checkout@v1 16 | - name: Use Node.js ${{ matrix.node-version }} 17 | uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - name: npm install, build, and test 21 | run: | 22 | npm ci 23 | npm run build --if-present 24 | npm test 25 | env: 26 | CI: true 27 | -------------------------------------------------------------------------------- /test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 |

anylogger-debug 1.0.3

9 | anylogger adapter for debug 10 |

Open the devtools console to see logging

11 |

To see logging output, make sure to enable debug mode:

12 |
13 |   localStorage.setItem('debug', '*')
14 | 
15 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import replace from 'rollup-plugin-re' 2 | import pkg from './package.json' 3 | 4 | export default [ 5 | { 6 | input: pkg.src, 7 | output: [ 8 | // commonjs build 9 | { file: pkg.main, format: 'cjs', strict: false }, 10 | ], 11 | external: [ 'anylogger', 'debug' ], 12 | }, 13 | { 14 | input: pkg.src, 15 | output: [ 16 | // browser-friendly build 17 | { file: pkg.iife, format: 'iife', strict: false, globals: { anylogger: 'anylogger', debug: 'debug' } }, 18 | ], 19 | external: [ 'anylogger', 'debug' ], 20 | plugins: [ 21 | // remove import bloat from iife bundle 22 | replace({ 23 | patterns: [ 24 | { 25 | match: /anylogger-debug/, 26 | test: 'import anylogger from \'anylogger\'', 27 | replace: '', 28 | }, { 29 | match: /anylogger-debug/, 30 | test: 'import debug from \'debug\'', 31 | replace: '', 32 | }, 33 | ] 34 | }) 35 | ], 36 | }, 37 | ]; 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Stijn de Witt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | /anylogger-debug.min.js 63 | /anylogger-debug.cjs.js 64 | /anylogger-debug.esm.js 65 | /anylogger-debug.iife.js 66 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "anylogger-debug", 3 | "version": "1.0.3", 4 | "description": "Anylogger adapter for debug", 5 | "src": "./anylogger-debug.js", 6 | "main": "./anylogger-debug.cjs.js", 7 | "iife": "./anylogger-debug.iife.js", 8 | "min": "./anylogger-debug.min.js", 9 | "spec": "./anylogger-debug.spec.js", 10 | "files": [ 11 | "anylogger-debug.js", 12 | "anylogger-debug.cjs.js", 13 | "anylogger-debug.iife.js", 14 | "anylogger-debug.min.js", 15 | "anylogger-debug.spec.js", 16 | "test.html" 17 | ], 18 | "unpkg": "anylogger-debug.min.js", 19 | "scripts": { 20 | "build": "npm run test -s && npm run minify -s && npm run docs -s", 21 | "docs": "cross-env NODE_ENV=production node build.js docs", 22 | "minify": "cross-env NODE_ENV=production node build.js minify", 23 | "package": "cross-env NODE_ENV=production rollup -c", 24 | "prepare": "npm run build", 25 | "test": "npm run package -s && mocha anylogger-debug.spec.js" 26 | }, 27 | "repository": { 28 | "type": "git", 29 | "url": "git+https://github.com/download/anylogger-debug.git" 30 | }, 31 | "keywords": [ 32 | "anylogger", 33 | "debug", 34 | "anylogger-debug", 35 | "log" 36 | ], 37 | "author": "Stijn de Witt", 38 | "license": "MIT", 39 | "bugs": { 40 | "url": "https://github.com/download/anylogger-debug/issues" 41 | }, 42 | "homepage": "https://github.com/download/anylogger-debug#readme", 43 | "dependencies": { 44 | "anylogger": "^1.0.6", 45 | "debug": "^4.3.1" 46 | }, 47 | "peerDependencies": { 48 | "anylogger": "^1.0.1", 49 | "debug": "^4.1.1" 50 | }, 51 | "devDependencies": { 52 | "chai": "^4.2.0", 53 | "cross-env": "^7.0.2", 54 | "gzip-size": "^6.0.0", 55 | "mocha": "^8.2.1", 56 | "rollup": "^2.33.3", 57 | "rollup-plugin-re": "^1.0.7", 58 | "sinon": "^9.2.1", 59 | "uglify-js": "^3.11.6" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # anylogger-debug 1.0.3 2 | ### Anylogger adapter for debug 3 | 4 | [![npm](https://img.shields.io/npm/v/anylogger-debug.svg)](https://npmjs.com/package/anylogger-debug) 5 | [![license](https://img.shields.io/npm/l/anylogger-debug.svg)](https://opensource.org/licenses/MIT) 6 | [![travis](https://img.shields.io/travis/Download/anylogger-debug.svg)](https://travis-ci.org/Download/anylogger-debug) 7 | ![mind BLOWN](https://img.shields.io/badge/mind-BLOWN-ff69b4.svg) 8 | 9 | . 10 | 11 | ## What is this? 12 | This is an [anylogger](https://npmjs.com/package/anylogger) adapter for [debug](https://npmjs.com/package/debug). 13 | 14 | This package is meant for application projects that are using libraries using 15 | `anylogger`. By including this adapter in your project, all libraries using 16 | `anylogger` will automatically start to use `debug` as their logging framework. 17 | 18 | ## Download 19 | 20 | * [anylogger-debug.js](https://unpkg.com/anylogger-debug@1.0.3/anylogger-debug.js) 21 | (fully commented source ~5kB) 22 | * [anylogger-debug.min.js](https://unpkg.com/anylogger-debug@1.0.3/anylogger-debug.min.js) 23 | (minified 133 bytes, gzipped ~[121](#gzip-size) bytes) 24 | 25 | 26 | ## CDN 27 | 28 | *index.html* 29 | ```html 30 | 31 | 32 | 33 | 39 | ``` 40 | 41 | ## Install 42 | 43 | Install this adapter, as well as both `anylogger` and `debug`: 44 | 45 | ```sh 46 | npm install --save anylogger-debug anylogger debug 47 | ``` 48 | 49 | ## Include in your application project 50 | This package is meant for application projects. If you are writing a library to 51 | be NPM installed into some other project, most likely you should not include 52 | any adapter, but instead just use `anylogger` directly. 53 | 54 | The `anylogger-debug` adapter will modify the `anylogger` factory in such a way 55 | that the loggers it creates will be logging to `debug`. 56 | 57 | > When using `debug`, all logging is supressed by default. As such, you should make sure to activate debug mode with the environment variable or localStorage key [as usual](https://www.npmjs.com/package/debug#usage) before expecting to see any output. 58 | 59 | To activate the adapter, include it in your application entry point. 60 | 61 | ### Require 62 | 63 | *main.js* 64 | ```js 65 | require('anylogger-debug') 66 | ``` 67 | 68 | ### Import 69 | 70 | *main.js* 71 | ```js 72 | import 'anylogger-debug' 73 | ``` 74 | 75 | ## Logging in the application project 76 | In your application module code, only use anylogger to stay framework 77 | independent: 78 | 79 | *my-module.js* 80 | ```js 81 | import anylogger from 'anylogger' 82 | const log = anylogger('my-module') 83 | log('Logging is simple!') 84 | ``` 85 | 86 | This is helpful if you ever decide to factor out the application module into a 87 | separate library. 88 | 89 | ## log configuration in the application project 90 | 91 | Because `anylogger` is simply using `debug` below the surface, you can use 92 | all the normal configuration mechanisms available for `debug`. 93 | 94 | If you need to control log settings programmatically, just import `debug` and 95 | use it directly: 96 | 97 | *main.js* 98 | ```js 99 | // ... 100 | import debug from 'debug' 101 | debug.enable('my-module') 102 | // ... 103 | ``` 104 | 105 | > I suggest using `debug`'s build-in configuration mechanism via environment variables and localstorage lookup keys instead of doing it from code. 106 | 107 | ## Issues 108 | 109 | Add an issue in this project's 110 | [issue tracker](https://github.com/download/anylogger-debug/issues) 111 | to let me know of any problems you find, or questions you may have. 112 | 113 | 114 | ## Copyright 115 | 116 | © 2020 by [Stijn de Witt](https://stijndewitt.com). Some rights reserved. 117 | 118 | 119 | ## License 120 | 121 | Licensed under the [MIT Open Source license](https://opensource.org/licenses/MIT). 122 | 123 | ## gzip-size 124 | The GZIP algorithm is available in different flavours and with different 125 | possible compression settings. The sizes quoted in this README have been 126 | measured using [gzip-size](https://npmjs.com/package/gzip-size) 127 | by [Sindre Sorhus](https://github.com/sindresorhus), your mileage may vary. 128 | -------------------------------------------------------------------------------- /anylogger-debug.spec.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect 2 | var sinon = require('sinon') 3 | var adapter = require('./anylogger-debug.cjs') 4 | var anylogger = require('anylogger') 5 | var debug = require('debug') 6 | 7 | var sandbox = sinon.createSandbox(); 8 | 9 | describe('anylogger([name, [options]]) => log', function() { 10 | beforeEach(function(){ 11 | // spy on the debug.log method 12 | sandbox.spy(debug, 'log') 13 | }) 14 | 15 | afterEach(function(){ 16 | // clear any loggers that were created 17 | Object.keys(anylogger()).forEach(function(key){ 18 | delete anylogger()[key] 19 | }) 20 | // restore original console methods 21 | sandbox.restore() 22 | }) 23 | 24 | 25 | it('is a function', function(){ 26 | expect(anylogger).to.be.a('function') 27 | }) 28 | 29 | it('returns an object mapping names to loggers when called without arguments', function(){ 30 | var result = anylogger() 31 | expect(result).to.be.an('object') 32 | expect(Object.keys(result)).to.deep.eq([]) 33 | anylogger('test') 34 | result = anylogger() 35 | expect(result).to.be.an('object') 36 | expect(Object.keys(result)).to.deep.eq(['test']) 37 | }) 38 | 39 | it('returns a named logger when called with a name', function(){ 40 | var name = 'test' 41 | var result = anylogger(name) 42 | expect(result).to.be.a('function') 43 | expect(result.name).to.equal(name) 44 | }) 45 | 46 | it('returns the same logger when called multiple times with the same name', function(){ 47 | var name = 'test' 48 | var expected = anylogger(name) 49 | var actual = anylogger(name) 50 | expect(actual).to.equal(expected) 51 | }) 52 | 53 | it('calls anylogger.new when a new logger named "test" is created', function(){ 54 | sandbox.spy(anylogger, 'new') 55 | expect(anylogger.new.callCount).to.equal(0) 56 | anylogger('test') 57 | expect(anylogger.new.callCount).to.equal(1) 58 | }) 59 | 60 | it('calls anylogger.ext when a new logger named "test" is created', function(){ 61 | sandbox.spy(anylogger, 'ext') 62 | expect(anylogger.ext.callCount).to.equal(0) 63 | anylogger('test') 64 | expect(anylogger.ext.callCount).to.equal(1) 65 | }) 66 | 67 | it('does not call anylogger.new on subsequent calls with the same name', function(){ 68 | sandbox.spy(anylogger, 'new') 69 | expect(anylogger.new.callCount).to.equal(0) 70 | anylogger('test') 71 | expect(anylogger.new.callCount).to.equal(1) 72 | anylogger('test') 73 | expect(anylogger.new.callCount).to.equal(1) 74 | }) 75 | 76 | it('calls anylogger.new when a new logger named "toString" is created', function(){ 77 | sandbox.spy(anylogger, 'new') 78 | expect(anylogger.new.callCount).to.equal(0) 79 | anylogger('toString') 80 | expect(anylogger.new.callCount).to.equal(1) 81 | }) 82 | 83 | it('does not call anylogger.new on subsequent calls with "toString" as argument', function(){ 84 | sandbox.spy(anylogger, 'new') 85 | expect(anylogger.new.callCount).to.equal(0) 86 | anylogger('toString') 87 | expect(anylogger.new.callCount).to.equal(1) 88 | anylogger('toString') 89 | expect(anylogger.new.callCount).to.equal(1) 90 | }) 91 | 92 | it('accepts an optional options argument', function(){ 93 | var name = 'test' 94 | var options = { level: 'info' } 95 | var result = anylogger(name, options) 96 | expect(result).to.be.a('function') 97 | }) 98 | 99 | describe('log', function(){ 100 | it('is a function', function(){ 101 | var name = 'test' 102 | var log = anylogger(name) 103 | expect(log).to.be.a('function') 104 | }) 105 | 106 | it('has a name that matches the name given to anylogger', function(){ 107 | var name = 'test' 108 | var log = anylogger(name) 109 | expect(log.name).to.equal(name) 110 | }) 111 | 112 | it('has a method `trace`', function(){ 113 | var log = anylogger('test') 114 | expect(log).to.have.property('trace') 115 | expect(log.trace).to.be.a('function') 116 | }) 117 | 118 | it('has a method `debug`', function(){ 119 | var log = anylogger('test') 120 | expect(log).to.have.property('debug') 121 | expect(log.debug).to.be.a('function') 122 | }) 123 | 124 | it('has a method `log`', function(){ 125 | var log = anylogger('test') 126 | expect(log).to.have.property('log') 127 | expect(log.log).to.be.a('function') 128 | }) 129 | 130 | it('has a method `info`', function(){ 131 | var log = anylogger('test') 132 | expect(log).to.have.property('info') 133 | expect(log.info).to.be.a('function') 134 | }) 135 | 136 | it('has a method `warn`', function(){ 137 | var log = anylogger('test') 138 | expect(log).to.have.property('warn') 139 | expect(log.warn).to.be.a('function') 140 | }) 141 | 142 | it('has a method `error`', function(){ 143 | var log = anylogger('test') 144 | expect(log).to.have.property('error') 145 | expect(log.error).to.be.a('function') 146 | }) 147 | 148 | it('has a method `enabledFor`', function(){ 149 | try { 150 | var log = anylogger('test') 151 | expect(log).to.have.property('enabledFor') 152 | expect(log.enabledFor).to.be.a('function') 153 | expect(log.enabledFor()).to.equal(false) 154 | debug.enable('test') 155 | expect(log.enabledFor()).to.equal(true) 156 | } 157 | finally { 158 | debug.disable() 159 | } 160 | }) 161 | 162 | it('can be invoked to log a message', function(){ 163 | try { 164 | debug.enable('test') 165 | var log = anylogger('test') 166 | log('message') 167 | expect(debug.log.callCount).to.equal(1) 168 | } 169 | finally { 170 | debug.disable() 171 | } 172 | }) 173 | 174 | it('can be invoked with a level name as first argument to log a message at that level', function(){ 175 | try { 176 | // debug.enable('test') 177 | var log = anylogger('test') 178 | var out = { 179 | log: debug.log 180 | } 181 | sandbox.spy(log, 'log') 182 | sandbox.spy(log, 'info') 183 | log('info', 'message') 184 | expect(log.log.callCount).to.equal(0) 185 | expect(log.info.callCount).to.equal(1) 186 | } 187 | finally { 188 | debug.disable() 189 | } 190 | }) 191 | 192 | it('by default, prints no messages to the console', function(){ 193 | var log = anylogger('test') 194 | expect(debug.log.callCount).to.equal(0) 195 | log('message') 196 | expect(debug.log.callCount).to.equal(0) 197 | }) 198 | 199 | it('when enabled via debug, prints messages to the console', function(){ 200 | try { 201 | debug.enable('test') 202 | var log = anylogger('test') 203 | expect(debug.log.callCount).to.equal(0) 204 | log('message') 205 | expect(debug.log.callCount).to.equal(1) 206 | } 207 | finally { 208 | debug.disable() 209 | } 210 | }) 211 | 212 | 213 | }) 214 | }) 215 | --------------------------------------------------------------------------------