├── .github ├── CODEOWNERS └── workflows │ └── npm-publish.yml ├── .gitignore ├── LICENSE ├── README.md ├── default-social.png ├── logger-screenshots.png ├── package-lock.json ├── package.json ├── renovate.json ├── src ├── browser │ ├── _BrowserLogger.js │ ├── browser-globals.js │ └── browser.js ├── node │ ├── _NodeLogger.js │ └── node.js ├── shared │ └── _AbstractLogger.js └── types │ ├── _LogColors.js │ └── _LogLevels.js └── test ├── integration ├── browser-integration-test.js └── node-integration-test.js ├── node ├── browser-logger-test.js └── node-logger-test.js ├── static └── logger-integration │ ├── index.html │ └── readme.html └── utils └── TestServer.js /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @gauntface 2 | -------------------------------------------------------------------------------- /.github/workflows/npm-publish.yml: -------------------------------------------------------------------------------- 1 | on: push 2 | name: Build and Publish 3 | jobs: 4 | all: 5 | name: Build and Publish 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@master 9 | - name: Install 10 | run: npm install 11 | - name: Test 12 | run: npm run test 13 | - name: Publish 14 | if: github.ref == 'refs/heads/main' 15 | uses: mikeal/merge-release@master 16 | env: 17 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 18 | NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build-*/ 2 | dist/ 3 | docs/ 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | 24 | # nyc test coverage 25 | .nyc_output 26 | 27 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 28 | .grunt 29 | 30 | # Bower dependency directory (https://bower.io/) 31 | bower_components 32 | 33 | # node-waf configuration 34 | .lock-wscript 35 | 36 | # Compiled binary addons (http://nodejs.org/api/addons.html) 37 | build/Release 38 | 39 | # Dependency directories 40 | node_modules/ 41 | jspm_packages/ 42 | 43 | # Typescript v1 declaration files 44 | typings/ 45 | 46 | # Optional npm cache directory 47 | .npm 48 | 49 | # Optional eslint cache 50 | .eslintcache 51 | 52 | # Optional REPL history 53 | .node_repl_history 54 | 55 | # Output of 'npm pack' 56 | *.tgz 57 | 58 | # Yarn Integrity file 59 | .yarn-integrity 60 | 61 | # dotenv environment variables file 62 | .env 63 | 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 Matthew Gaunt 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |  2 | 3 | # @gauntface/logger 4 | 5 | `@gauntface/logger` is a simple library aimed at making it easy to add 6 | colored and prefixed logs to a project in both Node and the browser. 7 | 8 |  9 | 10 | ## Usage in Browser 11 | 12 | ```html 13 | 14 | 58 | ``` 59 | 60 | ## Usage in Node 61 | 62 | Using the default logger: 63 | ```javascript 64 | const {logger} = require('@gauntface/logger'); 65 | 66 | logger.debug(`console.debug()`); 67 | logger.info(`console.info()`); 68 | logger.log(`console.log()`); 69 | logger.warn(`console.warn()`); 70 | logger.error(`console.error()`); 71 | 72 | // Customize the default loggers prefix 73 | logger.setPrefix(`Logger Demo`); 74 | logger.log('👋'); 75 | ``` 76 | 77 | Using a custom logger 78 | ```javascript 79 | const {Logger} = require('@gauntface/logger'); 80 | 81 | const simpleLogger = new Logger({ 82 | prefix: 'My App/Demo', 83 | }); 84 | simpleLogger.log(`Example message`, { 85 | message: 'Works just like console.log()' 86 | }); 87 | ``` 88 | 89 | The package.json defines a main and a browser property which 90 | should allow you to use `@gauntface/logger` for importing, if 91 | however you want to explicitly get the browser / node version 92 | please use the following imports: 93 | 94 | - For the browser: 95 | ```javascript 96 | import {logger, Logger, ...} from '@gauntface/logger/src/browser/browser.js'; 97 | ``` 98 | - For node: 99 | ```javascript 100 | import {logger, Logger, ...} from '@gauntface/logger/src/node/node.js'; 101 | ``` 102 | -------------------------------------------------------------------------------- /default-social.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gauntface/logger/2ce01559bac6d9f2bf4684b3055f2332c2b2fd03/default-social.png -------------------------------------------------------------------------------- /logger-screenshots.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gauntface/logger/2ce01559bac6d9f2bf4684b3055f2332c2b2fd03/logger-screenshots.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "publishConfig": { 3 | "access": "public" 4 | }, 5 | "type": "module", 6 | "scripts": { 7 | "ava": "ava ./test/{node,integration}/**/*.js", 8 | "build": "rm -rf build/ && esbuild src/browser/browser-globals.js --bundle --sourcemap --outfile=build/browser-globals.js --format=cjs", 9 | "test": "npm run build && npm run ava" 10 | }, 11 | "devDependencies": { 12 | "ava": "6.1.1", 13 | "esbuild": "0.25.0", 14 | "express": "4.20.0", 15 | "fs-extra": "11.2.0", 16 | "puppeteer": "21.11.0", 17 | "sinon": "17.0.1" 18 | }, 19 | "keywords": [ 20 | "console", 21 | "log", 22 | "logger" 23 | ], 24 | "files": [ 25 | "build/*", 26 | "src/*" 27 | ], 28 | "name": "@gauntface/logger", 29 | "description": "A simple console abstraction with some little extras", 30 | "main": "./src/node/node.js", 31 | "browser": "./src/browser/browser.js", 32 | "version": "3.0.2" 33 | } 34 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "local>gauntface/.github:renovate-config" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /src/browser/_BrowserLogger.js: -------------------------------------------------------------------------------- 1 | import {AbstractLogger} from '../shared/_AbstractLogger.js'; 2 | import * as LogLevels from '../types/_LogLevels.js'; 3 | import * as LogColors from '../types/_LogColors.js'; 4 | 5 | export class BrowserLogger extends AbstractLogger { 6 | /** 7 | * @protected 8 | * @param {LogLevels.LogLevel} logLevel 9 | * @param {string} prefix 10 | * @returns string[] 11 | */ 12 | colorPrefix(logLevel,prefix) { 13 | return [`%c${prefix}`, this.getLevelCSS(logLevel)]; 14 | } 15 | 16 | /** 17 | * @private 18 | * @param {LogLevels.LogLevel} logLevel 19 | * @returns string 20 | */ 21 | getLevelCSS(logLevel) { 22 | /** 23 | * @param {LogColors.RGBColor} color 24 | * @returns string 25 | */ 26 | function getStyles(color) { 27 | return `background: rgb(${color.red}, ${color.green}, ${color.blue}); color: white; padding: 2px 0.5em; border-radius: 0.5em`; 28 | } 29 | 30 | switch(logLevel) { 31 | case LogLevels.DEBUG: 32 | return getStyles(LogColors.DEBUG); 33 | case LogLevels.INFO: 34 | return getStyles(LogColors.INFO); 35 | case LogLevels.WARN: 36 | return getStyles(LogColors.WARN); 37 | case LogLevels.ERROR: 38 | return getStyles(LogColors.ERROR); 39 | case LogLevels.GROUP: 40 | return getStyles(LogColors.GROUP); 41 | case LogLevels.LOG: 42 | default: 43 | return getStyles(LogColors.LOG); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/browser/browser-globals.js: -------------------------------------------------------------------------------- 1 | import * as globals from './browser.js'; 2 | self['gauntface'] = self['gauntface'] || {}; 3 | Object.assign(self['gauntface'], globals); 4 | -------------------------------------------------------------------------------- /src/browser/browser.js: -------------------------------------------------------------------------------- 1 | import {BrowserLogger} from './_BrowserLogger.js'; 2 | import * as LogLevels from '../types/_LogLevels.js'; 3 | 4 | const logger = new BrowserLogger(); 5 | export {BrowserLogger as Logger, logger, LogLevels}; -------------------------------------------------------------------------------- /src/node/_NodeLogger.js: -------------------------------------------------------------------------------- 1 | import {AbstractLogger} from '../shared/_AbstractLogger.js'; 2 | import * as LogLevels from '../types/_LogLevels.js'; 3 | import * as LogColors from '../types/_LogColors.js'; 4 | 5 | export class NodeLogger extends AbstractLogger { 6 | /** 7 | * @protected 8 | * @param {LogLevels.LogLevel} logLevel 9 | * @param {string} prefix 10 | * @returns string[] 11 | */ 12 | colorPrefix(logLevel,prefix) { 13 | const color = this.getANSIColor(logLevel); 14 | return [`${color}${prefix}${resetAnsi()}`]; 15 | } 16 | 17 | /** 18 | * @private 19 | * @param {LogLevels.LogLevel} logLevel 20 | * @returns Function 21 | */ 22 | getANSIColor(logLevel) { 23 | switch(logLevel) { 24 | case LogLevels.DEBUG: 25 | return toAnsi(LogColors.DEBUG); 26 | case LogLevels.INFO: 27 | return toAnsi(LogColors.INFO); 28 | case LogLevels.WARN: 29 | return toAnsi(LogColors.WARN); 30 | case LogLevels.ERROR: 31 | return toAnsi(LogColors.ERROR); 32 | case LogLevels.GROUP: 33 | return toAnsi(LogColors.GROUP); 34 | case LogLevels.LOG: 35 | default: 36 | return toAnsi(LogColors.LOG); 37 | } 38 | } 39 | } 40 | 41 | function toAnsi({red, green, blue}) { 42 | return `\x1b[38;2;${red};${green};${blue}m` 43 | } 44 | 45 | function resetAnsi() { 46 | return `\x1b[0m`; 47 | } 48 | -------------------------------------------------------------------------------- /src/node/node.js: -------------------------------------------------------------------------------- 1 | import {NodeLogger} from './_NodeLogger.js'; 2 | import * as LogLevels from '../types/_LogLevels.js'; 3 | 4 | const logger = new NodeLogger(); 5 | export {NodeLogger as Logger}; 6 | export {LogLevels}; 7 | export {logger}; -------------------------------------------------------------------------------- /src/shared/_AbstractLogger.js: -------------------------------------------------------------------------------- 1 | import * as LogLevels from '../types/_LogLevels.js'; 2 | 3 | /** 4 | * @typedef {{ prefix?: string|Object}} LoggerOpts 5 | */ 6 | 7 | export const DEFAULT_PREFIXES = { 8 | [LogLevels.DEBUG]: '🐛', 9 | [LogLevels.INFO]: 'ℹ️', 10 | [LogLevels.LOG]: '💬', 11 | [LogLevels.WARN]: '⚠️', 12 | [LogLevels.ERROR]: '☠️', 13 | [LogLevels.GROUP]: '🧵', 14 | }; 15 | 16 | /** 17 | * @abstract 18 | */ 19 | export class AbstractLogger { 20 | /** 21 | * @property {LoggerOpts} opts? 22 | * @private 23 | */ 24 | /** 25 | * @property {LogLevel} currentLogLevel 26 | * @private 27 | */ 28 | 29 | /** 30 | * 31 | * @param {LoggerOpts|void} opts 32 | */ 33 | constructor(opts) { 34 | this.opts = opts || {}; 35 | this.setPrefix(this.opts.prefix); 36 | this.currentLogLevel = this.getDefaultLogLevel(); 37 | } 38 | 39 | /** 40 | * @param {string|object} prefix 41 | */ 42 | setPrefix(prefix) { 43 | if (!prefix) { 44 | this.opts.prefix = DEFAULT_PREFIXES; 45 | } else if (typeof prefix == 'object') { 46 | this.opts.prefix = {}; 47 | for (const k of Object.keys(DEFAULT_PREFIXES)) { 48 | this.opts.prefix[k] = prefix[k] || DEFAULT_PREFIXES[k]; 49 | } 50 | } else { 51 | this.opts.prefix = prefix; 52 | } 53 | } 54 | 55 | /** 56 | * @param {LogLevels.LogLevel} logLevel 57 | */ 58 | setLogLevel(logLevel) { 59 | this.currentLogLevel = logLevel; 60 | } 61 | 62 | /** 63 | * @param {...any} args 64 | */ 65 | debug(...args) { 66 | this.print(console.debug, LogLevels.DEBUG, args); 67 | } 68 | 69 | /** 70 | * @param {...any} args 71 | */ 72 | info(...args) { 73 | this.print(console.info, LogLevels.INFO, args); 74 | } 75 | 76 | /** 77 | * @param {...any} args 78 | */ 79 | log(...args) { 80 | this.print(console.log, LogLevels.LOG, args); 81 | } 82 | 83 | /** 84 | * @param {...any} args 85 | */ 86 | warn(...args) { 87 | this.print(console.warn, LogLevels.WARN, args); 88 | } 89 | 90 | /** 91 | * @param {...any} args 92 | */ 93 | error(...args) { 94 | this.print(console.error, LogLevels.ERROR, args); 95 | } 96 | 97 | /** 98 | * @param {...any} args 99 | */ 100 | group(...args) { 101 | this.print(console.group, LogLevels.GROUP, args); 102 | } 103 | 104 | /** 105 | * @param {...any} args 106 | */ 107 | groupCollapsed(...args) { 108 | this.print(console.groupCollapsed, LogLevels.GROUP, args); 109 | } 110 | 111 | groupEnd() { 112 | console.groupEnd(); 113 | } 114 | 115 | /** 116 | * @private 117 | * @param {Function} consoleFunc 118 | * @param {LogLevels.LogLevel} logLevel 119 | * @param {any[]} args 120 | * @returns 121 | */ 122 | print(consoleFunc, logLevel, args) { 123 | if (this.currentLogLevel > logLevel) { 124 | return; 125 | } 126 | 127 | consoleFunc(...this.getArgs(logLevel, args)); 128 | } 129 | 130 | /** 131 | * @private 132 | * @param {LogLevels.LogLevel} logLevel 133 | * @param {any[]} args 134 | * @returns any[] 135 | */ 136 | getArgs(logLevel, args) { 137 | const prefix = this.getPrefix(logLevel); 138 | if (prefix) { 139 | return [...prefix, ...args]; 140 | } 141 | return args; 142 | } 143 | 144 | /** 145 | * @private 146 | * @param {LogLevels.LogLevel} logLevel 147 | * @returns string[] 148 | */ 149 | getPrefix(logLevel) { 150 | let pre = this.opts.prefix; 151 | if (!pre) { 152 | pre = DEFAULT_PREFIXES; 153 | } 154 | 155 | let s = pre; 156 | if (typeof s == 'object') { 157 | s = pre[logLevel] 158 | } 159 | return this.colorPrefix(logLevel, s); 160 | } 161 | 162 | /** 163 | * @protected 164 | * @returns LogLevels.LogLevel 165 | */ 166 | getDefaultLogLevel() { 167 | return LogLevels.DEBUG; 168 | } 169 | 170 | /** 171 | * @protected 172 | * @abstract 173 | * @param {LogLevels.LogLevel} level 174 | * @param {string} prefix 175 | * @returns []string 176 | * colorPrefix(level, prefix); 177 | */ 178 | } 179 | -------------------------------------------------------------------------------- /src/types/_LogColors.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @typedef {{red: number, green: number, blue: number}} RGBColor 3 | */ 4 | export const DEBUG = { 5 | // #636e72 6 | red: 99, 7 | green: 110, 8 | blue: 114, 9 | }; 10 | export const INFO = { 11 | // #487eb0 12 | red: 72, 13 | green: 126, 14 | blue: 176, 15 | }; 16 | export const LOG = { 17 | // #4cd137 18 | red: 76, 19 | green: 209, 20 | blue: 55, 21 | }; 22 | export const WARN = { 23 | // #e1b12c 24 | red: 225, 25 | green: 177, 26 | blue: 44, 27 | }; 28 | export const ERROR = { 29 | // #e74c3c 30 | red: 231, 31 | green: 76, 32 | blue: 60, 33 | }; 34 | export const GROUP = { 35 | // #00a8ff 36 | red: 0, 37 | green: 168, 38 | blue: 255, 39 | }; 40 | -------------------------------------------------------------------------------- /src/types/_LogLevels.js: -------------------------------------------------------------------------------- 1 | export const DEBUG = 0; 2 | export const INFO = 1; 3 | export const LOG = 2; 4 | export const WARN = 3; 5 | export const ERROR = 4; 6 | export const GROUP = 5; 7 | export const SILENCE = 6; 8 | 9 | /** 10 | * @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6} LogLevel 11 | */ 12 | -------------------------------------------------------------------------------- /test/integration/browser-integration-test.js: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import test from 'ava'; 3 | import puppeteer from 'puppeteer'; 4 | 5 | import {TestServer} from '../utils/TestServer.js'; 6 | 7 | let server; 8 | let serverAddress; 9 | let browser; 10 | let page; 11 | 12 | async function setupServer() { 13 | server = new TestServer(path.resolve()); 14 | serverAddress = await server.start(); 15 | } 16 | 17 | async function setupPuppeteer() { 18 | browser = await puppeteer.launch({args: ['--no-sandbox']}); 19 | page = await browser.newPage(); 20 | } 21 | 22 | test.before(async () => { 23 | await Promise.all([ 24 | setupServer(), 25 | setupPuppeteer(), 26 | ]); 27 | }); 28 | 29 | test.after.always(async () => { 30 | await Promise.all([ 31 | browser.close(), 32 | server.close(), 33 | ]); 34 | }); 35 | 36 | test.serial('log in browser', async (t) => { 37 | page.on('error', msg => { 38 | t.fail(msg.message); 39 | }); 40 | 41 | const messageData = []; 42 | page.on('console', msg => { 43 | messageData.push({ 44 | type: msg.type(), 45 | text: msg.text(), 46 | }); 47 | }); 48 | 49 | const response = await page.goto(`${serverAddress}/test/static/logger-integration/`, { 50 | waitUntil: 'networkidle0', 51 | }); 52 | t.deepEqual(response.status(), 200); 53 | 54 | t.deepEqual(messageData.length, 34); 55 | 56 | t.deepEqual(messageData, [ 57 | { 58 | type: 'debug', 59 | text: `%c🐛 background: rgb(99, 110, 114); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, debug`, 60 | }, 61 | { 62 | type: 'info', 63 | text: '%cℹ️ background: rgb(72, 126, 176); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, info', 64 | }, 65 | { 66 | type: 'log', 67 | text: '%c💬 background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log', 68 | }, 69 | { 70 | type: 'warning', 71 | text: '%c⚠️ background: rgb(225, 177, 44); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, warn', 72 | }, 73 | { 74 | type: 'error', 75 | text: '%c☠️ background: rgb(231, 76, 60); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, error', 76 | }, 77 | { 78 | type: 'startGroup', 79 | text: '%c🧵 background: rgb(0, 168, 255); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, group 1', 80 | }, 81 | { 82 | type: 'log', 83 | text: '%c💬 background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log group 1', 84 | }, 85 | { 86 | type: 'startGroup', 87 | text: '%c🧵 background: rgb(0, 168, 255); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, group 2', 88 | }, 89 | { 90 | type: 'log', 91 | text: '%c💬 background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log group 2', 92 | }, 93 | { 94 | type: 'endGroup', 95 | text: 'console.groupEnd', 96 | }, 97 | { 98 | type: 'endGroup', 99 | text: 'console.groupEnd', 100 | }, 101 | { 102 | type: 'startGroupCollapsed', 103 | text: '%c🧵 background: rgb(0, 168, 255); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, groupCollapsed 1', 104 | }, 105 | { 106 | type: 'log', 107 | text: '%c💬 background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log group 1', 108 | }, 109 | { 110 | type: 'startGroupCollapsed', 111 | text: '%c🧵 background: rgb(0, 168, 255); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, groupCollapsed 2', 112 | }, 113 | { 114 | type: 'log', 115 | text: '%c💬 background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log group 2', 116 | }, 117 | { 118 | type: 'endGroup', 119 | text: 'console.groupEnd', 120 | }, 121 | { 122 | type: 'endGroup', 123 | text: 'console.groupEnd', 124 | }, 125 | 126 | { 127 | type: 'debug', 128 | text: `%ccustom-logger-test background: rgb(99, 110, 114); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, debug`, 129 | }, 130 | { 131 | type: 'info', 132 | text: '%ccustom-logger-test background: rgb(72, 126, 176); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, info', 133 | }, 134 | { 135 | type: 'log', 136 | text: '%ccustom-logger-test background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log', 137 | }, 138 | { 139 | type: 'warning', 140 | text: '%ccustom-logger-test background: rgb(225, 177, 44); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, warn', 141 | }, 142 | { 143 | type: 'error', 144 | text: '%ccustom-logger-test background: rgb(231, 76, 60); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, error', 145 | }, 146 | { 147 | type: 'startGroup', 148 | text: '%ccustom-logger-test background: rgb(0, 168, 255); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, group 1', 149 | }, 150 | { 151 | type: 'log', 152 | text: '%ccustom-logger-test background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log group 1', 153 | }, 154 | { 155 | type: 'startGroup', 156 | text: '%ccustom-logger-test background: rgb(0, 168, 255); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, group 2', 157 | }, 158 | { 159 | type: 'log', 160 | text: '%ccustom-logger-test background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log group 2', 161 | }, 162 | { 163 | type: 'endGroup', 164 | text: 'console.groupEnd', 165 | }, 166 | { 167 | type: 'endGroup', 168 | text: 'console.groupEnd', 169 | }, 170 | { 171 | type: 'startGroupCollapsed', 172 | text: '%ccustom-logger-test background: rgb(0, 168, 255); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, groupCollapsed 1', 173 | }, 174 | { 175 | type: 'log', 176 | text: '%ccustom-logger-test background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log group 1', 177 | }, 178 | { 179 | type: 'startGroupCollapsed', 180 | text: '%ccustom-logger-test background: rgb(0, 168, 255); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, groupCollapsed 2', 181 | }, 182 | { 183 | type: 'log', 184 | text: '%ccustom-logger-test background: rgb(76, 209, 55); color: white; padding: 2px 0.5em; border-radius: 0.5em hello, log group 2', 185 | }, 186 | { 187 | type: 'endGroup', 188 | text: 'console.groupEnd', 189 | }, 190 | { 191 | type: 'endGroup', 192 | text: 'console.groupEnd', 193 | }, 194 | ]); 195 | }); 196 | -------------------------------------------------------------------------------- /test/integration/node-integration-test.js: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import test from 'ava'; 3 | import fs from 'fs-extra'; 4 | 5 | // Note this is from build/test/... 6 | const rootDir = path.resolve(); 7 | 8 | function getPackage() { 9 | return fs.readJson(path.join(rootDir, 'package.json')); 10 | } 11 | 12 | test.serial('should be able to require Logger from package.json', async (t) => { 13 | const pkg = await getPackage(); 14 | const nodePath = path.join(rootDir, pkg.main); 15 | await fs.access(nodePath); 16 | const {Logger} = await import(nodePath); 17 | t.truthy(Logger); 18 | 19 | const instance = new Logger({prefix: 'logger-test'}); 20 | t.truthy(instance); 21 | 22 | instance.debug('hello, debug'); 23 | instance.info('hello, info'); 24 | instance.log('hello, log'); 25 | instance.warn('hello, warn'); 26 | instance.error('hello, error'); 27 | 28 | instance.group('hello, group 1'); 29 | instance.log('hello, log group 1'); 30 | instance.group('hello, group 2'); 31 | instance.log('hello, log group 2'); 32 | instance.groupEnd(); 33 | instance.groupEnd(); 34 | 35 | instance.groupCollapsed('hello, groupCollapsed 1'); 36 | instance.log('hello, log group 1'); 37 | instance.groupCollapsed('hello, groupCollapsed 2'); 38 | instance.log('hello, log group 2'); 39 | instance.groupEnd(); 40 | instance.groupEnd(); 41 | }); 42 | 43 | test.serial('should be able to find browser from package.json', async (t) => { 44 | const pkg = await getPackage(); 45 | 46 | const browserPath = path.join(rootDir, pkg.browser); 47 | await fs.access(browserPath); 48 | t.pass(); 49 | }); 50 | -------------------------------------------------------------------------------- /test/node/browser-logger-test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import sinon from 'sinon'; 3 | import {Logger} from '../../src/browser/browser.js'; 4 | 5 | test.beforeEach((t) => { 6 | t.context.sandbox = sinon.createSandbox(); 7 | }); 8 | 9 | test.afterEach.always((t) => { 10 | t.context.sandbox.restore(); 11 | }); 12 | 13 | const methods = [ 14 | 'debug', 15 | 'info', 16 | 'log', 17 | 'warn', 18 | 'error', 19 | 'group', 20 | 'groupCollapsed', 21 | ]; 22 | methods.forEach(m => { 23 | // Broken out into individual tests since ava uses console.log() 24 | // which can causes the call count to be wrong. 25 | test.serial(`logger.${m} with default prefixes`, (t) => { 26 | const s = t.context.sandbox.spy(console, m); 27 | const MSG = 'hello default prefixes'; 28 | const logger = new Logger(); 29 | 30 | logger[m](MSG); 31 | logger.groupEnd(); 32 | 33 | t.deepEqual(s.callCount, 1); 34 | t.deepEqual(s.getCall(0).args[2], MSG); 35 | }); 36 | 37 | test.serial(`logger.${m} with custom prefix string`, (t) => { 38 | const s = t.context.sandbox.spy(console, m); 39 | const PREFIX = 'logger-test'; 40 | const MSG = 'hello test prefix'; 41 | const logger = new Logger({ 42 | prefix: PREFIX, 43 | }); 44 | 45 | logger[m](MSG); 46 | logger.groupEnd(); 47 | 48 | t.deepEqual(s.callCount, 1); 49 | t.notDeepEqual(s.getCall(0).args[0].indexOf(PREFIX), -1); 50 | t.deepEqual(s.getCall(0).args[2], MSG); 51 | }); 52 | }); 53 | 54 | test.serial('nest groups', (t) => { 55 | const logSpy = t.context.sandbox.spy(console, 'group'); 56 | const logEndSpy = t.context.sandbox.spy(console, 'groupEnd'); 57 | 58 | const MSG = 'hello, group'; 59 | 60 | const logger = new Logger(); 61 | logger.group(MSG); 62 | logger.info('Level 1'); 63 | logger.group(MSG); 64 | logger.info('Level 2'); 65 | logger.groupEnd(); 66 | logger.groupEnd(); 67 | 68 | t.deepEqual(logSpy.callCount, 2); 69 | t.deepEqual(logSpy.getCall(0).args[2], MSG); 70 | t.deepEqual(logEndSpy.callCount, 2); 71 | }); 72 | 73 | test.serial('nest collapsed groups', (t) => { 74 | const logSpy = t.context.sandbox.spy(console, 'groupCollapsed'); 75 | const logEndSpy = t.context.sandbox.spy(console, 'groupEnd'); 76 | 77 | const MSG = 'hello, groupCollapsed'; 78 | 79 | const logger = new Logger(); 80 | logger.groupCollapsed(MSG); 81 | logger.info('Level 1'); 82 | logger.groupCollapsed(MSG); 83 | logger.info('Level 2'); 84 | logger.groupEnd(); 85 | logger.groupEnd(); 86 | 87 | t.deepEqual(logSpy.callCount, 2); 88 | t.deepEqual(logSpy.getCall(0).args[2], MSG); 89 | t.deepEqual(logEndSpy.callCount, 2); 90 | }); 91 | -------------------------------------------------------------------------------- /test/node/node-logger-test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import sinon from 'sinon'; 3 | import {Logger, LogLevels} from '../../src/node/node.js'; 4 | import {DEFAULT_PREFIXES} from '../../src/shared/_AbstractLogger.js'; 5 | 6 | test.beforeEach((t) => { 7 | t.context.sandbox = sinon.createSandbox(); 8 | }); 9 | 10 | test.afterEach.always((t) => { 11 | t.context.sandbox.restore(); 12 | }); 13 | 14 | const methods = [ 15 | 'debug', 16 | 'info', 17 | 'log', 18 | 'warn', 19 | 'error', 20 | 'group', 21 | 'groupCollapsed', 22 | ]; 23 | methods.forEach(m => { 24 | // Broken out into individual tests since ava uses console.log() 25 | // which can causes the call count to be wrong. 26 | test.serial(`logger.${m} with default prefixes`, (t) => { 27 | const s = t.context.sandbox.spy(console, m); 28 | const MSG = 'hello default prefixes'; 29 | const logger = new Logger(); 30 | 31 | logger[m](MSG); 32 | logger.groupEnd(); 33 | 34 | t.deepEqual(s.callCount, 1); 35 | t.deepEqual(s.getCall(0).args[1], MSG); 36 | }); 37 | 38 | test.serial(`logger.${m} with custom prefix string`, (t) => { 39 | const s = t.context.sandbox.spy(console, m); 40 | const PREFIX = 'logger-test'; 41 | const MSG = 'hello test prefix'; 42 | const logger = new Logger({ 43 | prefix: PREFIX, 44 | }); 45 | 46 | logger[m](MSG); 47 | logger.groupEnd(); 48 | 49 | t.deepEqual(s.callCount, 1); 50 | t.notDeepEqual(s.getCall(0).args[0].indexOf(PREFIX), -1); 51 | t.deepEqual(s.getCall(0).args[1], MSG); 52 | }); 53 | }); 54 | 55 | test.serial('nest groups', (t) => { 56 | const logSpy = t.context.sandbox.spy(console, 'group'); 57 | const logEndSpy = t.context.sandbox.spy(console, 'groupEnd'); 58 | 59 | const MSG = 'hello, group'; 60 | 61 | const logger = new Logger(); 62 | logger.group(MSG); 63 | logger.info('Level 1'); 64 | logger.group(MSG); 65 | logger.info('Level 2'); 66 | logger.groupEnd(); 67 | logger.groupEnd(); 68 | 69 | t.deepEqual(logSpy.callCount, 2); 70 | t.deepEqual(logSpy.getCall(0).args[1], MSG); 71 | t.deepEqual(logEndSpy.callCount, 2); 72 | }); 73 | 74 | test.serial('nest collapsed groups', (t) => { 75 | const logSpy = t.context.sandbox.spy(console, 'groupCollapsed'); 76 | const logEndSpy = t.context.sandbox.spy(console, 'groupEnd'); 77 | 78 | const MSG = 'hello, groupCollapsed'; 79 | 80 | const logger = new Logger(); 81 | logger.groupCollapsed(MSG); 82 | logger.info('Level 1'); 83 | logger.groupCollapsed(MSG); 84 | logger.info('Level 2'); 85 | logger.groupEnd(); 86 | logger.groupEnd(); 87 | 88 | t.deepEqual(logSpy.callCount, 2); 89 | t.deepEqual(logSpy.getCall(0).args[1], MSG); 90 | t.deepEqual(logEndSpy.callCount, 2); 91 | }); 92 | 93 | test.serial('use custom object prefix via setPrefix()', (t) => { 94 | const logSpy = t.context.sandbox.spy(console, 'log'); 95 | const debugSpy = t.context.sandbox.spy(console, 'debug'); 96 | 97 | const PREFIX = 'custom-prefix'; 98 | const MSG = 'hello, custom prefix'; 99 | 100 | const logger = new Logger(); 101 | logger.setPrefix({ 102 | [LogLevels.LOG]: PREFIX, 103 | }); 104 | 105 | t.deepEqual(logger.opts.prefix, Object.assign(DEFAULT_PREFIXES, { 106 | [LogLevels.LOG]: 'custom-prefix' 107 | })); 108 | 109 | logger.log(MSG); 110 | logger.debug(MSG); 111 | 112 | t.deepEqual(logSpy.callCount, 1); 113 | t.notDeepEqual(logSpy.getCall(0).args[0].indexOf(PREFIX), -1); 114 | t.deepEqual(logSpy.getCall(0).args[1], MSG); 115 | 116 | t.deepEqual(debugSpy.callCount, 1); 117 | t.deepEqual(debugSpy.getCall(0).args[0].indexOf(PREFIX), -1); 118 | t.deepEqual(debugSpy.getCall(0).args[1], MSG); 119 | }); 120 | 121 | test.serial('use custom object prefix via constructor', (t) => { 122 | const logSpy = t.context.sandbox.spy(console, 'log'); 123 | const debugSpy = t.context.sandbox.spy(console, 'debug'); 124 | 125 | const PREFIX = 'custom-prefix'; 126 | const MSG = 'hello, custom prefix'; 127 | 128 | const logger = new Logger({ 129 | prefix: { 130 | [LogLevels.LOG]: PREFIX, 131 | }, 132 | }); 133 | 134 | t.deepEqual(logger.opts.prefix, Object.assign(DEFAULT_PREFIXES, { 135 | [LogLevels.LOG]: 'custom-prefix' 136 | })); 137 | 138 | logger.log(MSG); 139 | logger.debug(MSG); 140 | 141 | t.deepEqual(logSpy.callCount, 1); 142 | t.notDeepEqual(logSpy.getCall(0).args[0].indexOf(PREFIX), -1); 143 | t.deepEqual(logSpy.getCall(0).args[1], MSG); 144 | 145 | t.deepEqual(debugSpy.callCount, 1); 146 | t.deepEqual(debugSpy.getCall(0).args[0].indexOf(PREFIX), -1); 147 | t.deepEqual(debugSpy.getCall(0).args[1], MSG); 148 | }); -------------------------------------------------------------------------------- /test/static/logger-integration/index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |