first commit

This commit is contained in:
DanielRamirezGe
2020-01-14 20:43:08 -06:00
parent 3557894f7f
commit 5eecfbf6cd
26326 changed files with 2434803 additions and 0 deletions
+615
View File
@@ -0,0 +1,615 @@
# Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
## [2.11.0](https://github.com/nuxt/consola/compare/v2.10.0...v2.11.0) (2019-11-10)
### Features
* **browser:** add support of formatted strings ([#66](https://github.com/nuxt/consola/issues/66)) ([920f313](https://github.com/nuxt/consola/commit/920f313dba322c34ccd1b2f08afba59122c3b8e7))
### Bug Fixes
* typecheck type and tag before normalize ([1984deb](https://github.com/nuxt/consola/commit/1984deb0a5214a3aa82dab972ec76af20ba14d1b))
* **types:** reporter in remove methods are optional ([#70](https://github.com/nuxt/consola/issues/70)) ([a17cdb1](https://github.com/nuxt/consola/commit/a17cdb1a423e41076c58692130955d5a9f5e36ba))
### [2.10.1](https://github.com/nuxt/consola/compare/v2.10.0...v2.10.1) (2019-08-05)
## [2.10.0](https://github.com/nuxt/consola/compare/v2.9.0...v2.10.0) (2019-08-05)
### Bug Fixes
* add missing typescript declaration for level, stdout and stderr ([#58](https://github.com/nuxt/consola/issues/58)) ([a149dbb](https://github.com/nuxt/consola/commit/a149dbb))
### Features
* improve typescript type definitions ([#57](https://github.com/nuxt/consola/issues/57)) ([80eefd8](https://github.com/nuxt/consola/commit/80eefd8))
## [2.9.0](https://github.com/nuxt/consola/compare/v2.8.0...v2.9.0) (2019-06-18)
### Features
* count spam log ([197a6b3](https://github.com/nuxt/consola/commit/197a6b3))
## [2.8.0](https://github.com/nuxt/consola/compare/v2.7.1...v2.8.0) (2019-06-18)
### Features
* spam preventation ([7da806b](https://github.com/nuxt/consola/commit/7da806b))
### [2.7.1](https://github.com/nuxt/consola/compare/v2.7.0...v2.7.1) (2019-05-26)
### Bug Fixes
* **browser:** hide `:` with tag and normal log ([8250d5a](https://github.com/nuxt/consola/commit/8250d5a))
## [2.7.0](https://github.com/nuxt/consola/compare/v2.6.2...v2.7.0) (2019-05-26)
### Bug Fixes
* correctly calculate line width when using grave accent ([bad52bd](https://github.com/nuxt/consola/commit/bad52bd))
### Features
* always hide right side on width < 80 ([07d8246](https://github.com/nuxt/consola/commit/07d8246))
* improve basic logs ([ea6ce59](https://github.com/nuxt/consola/commit/ea6ce59))
* **browser:** fancier logs ([b64f337](https://github.com/nuxt/consola/commit/b64f337))
* hide time from basic logger as it is commonly used in CI environments ([68c3bae](https://github.com/nuxt/consola/commit/68c3bae))
* smart hide time and tag when there is no space ([00a375f](https://github.com/nuxt/consola/commit/00a375f))
### [2.6.2](https://github.com/nuxt/consola/compare/v2.6.1...v2.6.2) (2019-05-15)
### Bug Fixes
* transpile browser dist to ES5 ([1f81eea](https://github.com/nuxt/consola/commit/1f81eea)), closes [nuxt/nuxt.js#5743](https://github.com/nuxt/consola/issues/5743)
### [2.6.1](https://github.com/nuxt/consola/compare/v2.6.0...v2.6.1) (2019-05-08)
### Bug Fixes
* **browser:** use `console.warn` when possible ([#49](https://github.com/nuxt/consola/issues/49)) ([e386ede](https://github.com/nuxt/consola/commit/e386ede))
# [2.6.0](https://github.com/nuxt/consola/compare/v2.5.8...v2.6.0) (2019-04-12)
### Features
* expose constructor and reporters ([3a8f662](https://github.com/nuxt/consola/commit/3a8f662))
## [2.5.8](https://github.com/nuxt/consola/compare/v2.5.7...v2.5.8) (2019-03-29)
### Bug Fixes
* **types:** allow passing extra arguments ([#46](https://github.com/nuxt/consola/issues/46)) ([d29fc46](https://github.com/nuxt/consola/commit/d29fc46))
## [2.5.7](https://github.com/nuxt/consola/compare/v2.5.6...v2.5.7) (2019-03-19)
### Bug Fixes
* **formatting:** fix formatting when multiple back-quotes ([#44](https://github.com/nuxt/consola/issues/44)) ([669a12e](https://github.com/nuxt/consola/commit/669a12e))
## [2.5.6](https://github.com/nuxt/consola/compare/v2.5.5...v2.5.6) (2019-02-25)
### Bug Fixes
* **ts:** revert export consola ts declarations ([#43](https://github.com/nuxt/consola/issues/43)) ([6bd4f85](https://github.com/nuxt/consola/commit/6bd4f85))
<a name="2.4.1"></a>
## [2.4.1](https://github.com/nuxt/consola/compare/v2.4.0...v2.4.1) (2019-02-12)
### Bug Fixes
* **ts:** set type "any" ([#40](https://github.com/nuxt/consola/issues/40)) ([ea9d551](https://github.com/nuxt/consola/commit/ea9d551))
<a name="2.4.0"></a>
# [2.4.0](https://github.com/nuxt/consola/compare/v2.3.2...v2.4.0) (2019-02-05)
### Bug Fixes
* **esm:** fix esm compatibility ([8ddecc3](https://github.com/nuxt/consola/commit/8ddecc3))
* **consola:** return `this` in setReporters ([544a887](https://github.com/nuxt/consola/commit/544a887))
* **types:** set message type to "any" ([#39](https://github.com/nuxt/consola/issues/39)) ([ff97b09](https://github.com/nuxt/consola/commit/ff97b09)), closes [#38](https://github.com/nuxt/consola/issues/38)
### Features
* **types:** update types ([d0d7455](https://github.com/nuxt/consola/commit/d0d7455))
<a name="2.3.2"></a>
## [2.3.2](https://github.com/nuxt/consola/compare/v2.3.1...v2.3.2) (2019-01-06)
### Bug Fixes
* **types:** add some of the missing types ([#35](https://github.com/nuxt/consola/issues/35)) ([5e3e69b](https://github.com/nuxt/consola/commit/5e3e69b))
* ignore winston dep in webpack ([#37](https://github.com/nuxt/consola/issues/37)) ([e534a28](https://github.com/nuxt/consola/commit/e534a28))
<a name="2.3.1"></a>
## [2.3.1](https://github.com/nuxt/consola/compare/v2.3.0...v2.3.1) (2019-01-02)
### Bug Fixes
* bypass webpack for lazy required version of winston ([500b509](https://github.com/nuxt/consola/commit/500b509))
<a name="2.3.0"></a>
# [2.3.0](https://github.com/nuxt/consola/compare/v2.2.6...v2.3.0) (2018-11-19)
### Bug Fixes
* **isLogObj:** handle non-standard error objects ([8748c81](https://github.com/nuxt/consola/commit/8748c81))
### Features
* browser reporter improvements ([591d0b4](https://github.com/nuxt/consola/commit/591d0b4)), closes [#31](https://github.com/nuxt/consola/issues/31)
* **fancy:** look like jest traces ([ecae238](https://github.com/nuxt/consola/commit/ecae238))
<a name="2.2.6"></a>
## [2.2.6](https://github.com/nuxt/consola/compare/v2.2.5...v2.2.6) (2018-11-14)
### Bug Fixes
* **json-reporter:** add a default value to the constructor ([#33](https://github.com/nuxt/consola/issues/33)) ([c59db36](https://github.com/nuxt/consola/commit/c59db36))
<a name="2.2.5"></a>
## [2.2.5](https://github.com/nuxt/consola/compare/v2.2.4...v2.2.5) (2018-11-14)
### Bug Fixes
* expose typescript typings ([f0398ed](https://github.com/nuxt/consola/commit/f0398ed))
<a name="2.2.4"></a>
## [2.2.4](https://github.com/nuxt/consola/compare/v2.2.3...v2.2.4) (2018-11-08)
### Bug Fixes
* use basic reporter only for ci and test environments ([33220e4](https://github.com/nuxt/consola/commit/33220e4))
<a name="2.2.3"></a>
## [2.2.3](https://github.com/nuxt/consola/compare/v2.2.2...v2.2.3) (2018-11-07)
### Bug Fixes
* **fancy:** honor logObj.icon ([d56fa38](https://github.com/nuxt/consola/commit/d56fa38))
<a name="2.2.2"></a>
## [2.2.2](https://github.com/nuxt/consola/compare/v2.2.1...v2.2.2) (2018-11-04)
### Bug Fixes
* update std-env to 2.1.1 ([32a9c67](https://github.com/nuxt/consola/commit/32a9c67))
<a name="2.2.1"></a>
## [2.2.1](https://github.com/nuxt/consola/compare/v2.2.0...v2.2.1) (2018-11-04)
### Bug Fixes
* remove file:// from error stack traces ([ff24b69](https://github.com/nuxt/consola/commit/ff24b69))
<a name="2.2.0"></a>
# [2.2.0](https://github.com/nuxt/consola/compare/v2.1.1...v2.2.0) (2018-11-04)
### Bug Fixes
* correctly handle falsy values ([367fb19](https://github.com/nuxt/consola/commit/367fb19))
### Features
* support formatOptions. resolves [#29](https://github.com/nuxt/consola/issues/29). ([7ed640f](https://github.com/nuxt/consola/commit/7ed640f))
<a name="2.1.1"></a>
## [2.1.1](https://github.com/nuxt/consola/compare/v2.1.0...v2.1.1) (2018-11-03)
### Bug Fixes
* add legacy ready and start levels for more backward compatibility ([f54b5c2](https://github.com/nuxt/consola/commit/f54b5c2))
<a name="2.1.0"></a>
# [2.1.0](https://github.com/nuxt/consola/compare/v2.0.9...v2.1.0) (2018-11-03)
### Features
* add aliases ([cbea7bd](https://github.com/nuxt/consola/commit/cbea7bd))
* mockTypes for easy mocking ([a332890](https://github.com/nuxt/consola/commit/a332890))
<a name="2.0.9"></a>
## [2.0.9](https://github.com/nuxt/consola/compare/v2.0.8...v2.0.9) (2018-11-03)
<a name="2.0.8"></a>
## [2.0.8](https://github.com/nuxt/consola/compare/v2.0.7...v2.0.8) (2018-11-03)
<a name="2.0.7"></a>
## [2.0.7](https://github.com/nuxt/consola/compare/v2.0.6...v2.0.7) (2018-11-02)
### Bug Fixes
* always use computed values for stdout/stderr ([f91abc0](https://github.com/nuxt/consola/commit/f91abc0))
<a name="2.0.6"></a>
## [2.0.6](https://github.com/nuxt/consola/compare/v2.0.5...v2.0.6) (2018-11-02)
<a name="2.0.5"></a>
## [2.0.5](https://github.com/nuxt/consola/compare/v2.0.4...v2.0.5) (2018-11-02)
<a name="2.0.4"></a>
## [2.0.4](https://github.com/nuxt/consola/compare/v2.0.3...v2.0.4) (2018-11-02)
### Bug Fixes
* **fancy:** remove extra icons ([b66fde0](https://github.com/nuxt/consola/commit/b66fde0))
<a name="2.0.3"></a>
## [2.0.3](https://github.com/nuxt/consola/compare/v2.0.2...v2.0.3) (2018-11-02)
### Bug Fixes
* **pkg:** exclude src from package ([4b1fb7d](https://github.com/nuxt/consola/commit/4b1fb7d))
* use live console._stdout bindings for default stream ([d9573c3](https://github.com/nuxt/consola/commit/d9573c3))
<a name="2.0.2"></a>
## [2.0.2](https://github.com/nuxt/consola/compare/v2.0.1...v2.0.2) (2018-11-02)
### Bug Fixes
* **error:** always strip first line from stack ([3afa9aa](https://github.com/nuxt/consola/commit/3afa9aa))
<a name="2.0.1"></a>
## [2.0.1](https://github.com/nuxt/consola/compare/v2.0.0...v2.0.1) (2018-11-02)
### Bug Fixes
* **fancy:** use proper color for log paths ([7c75283](https://github.com/nuxt/consola/commit/7c75283))
<a name="2.0.0"></a>
# [2.0.0](https://github.com/nuxt/consola/compare/v2.0.0-2...v2.0.0) (2018-11-02)
<a name="2.0.0-2"></a>
# [2.0.0-2](https://github.com/nuxt/consola/compare/v2.0.0-1...v2.0.0-2) (2018-11-02)
### Bug Fixes
* add methods for legacy support ([4bdd034](https://github.com/nuxt/consola/commit/4bdd034))
* preserve additional new lines ([340a001](https://github.com/nuxt/consola/commit/340a001))
* update std-env to 2.1.0 ([2dc2a50](https://github.com/nuxt/consola/commit/2dc2a50))
### Features
* support badge with fancy ([38600fe](https://github.com/nuxt/consola/commit/38600fe))
<a name="2.0.0-1"></a>
# [2.0.0-1](https://github.com/nuxt/consola/compare/v2.0.0-0...v2.0.0-1) (2018-10-31)
<a name="2.0.0-0"></a>
# [2.0.0-0](https://github.com/nuxt/consola/compare/v1.4.4...v2.0.0-0) (2018-10-31)
### Bug Fixes
* add schmance.js ([2929648](https://github.com/nuxt/consola/commit/2929648))
* **docs:** update readme ([#22](https://github.com/nuxt/consola/issues/22)) ([e75f2a0](https://github.com/nuxt/consola/commit/e75f2a0))
* add default/undefined color for browser ([39584d2](https://github.com/nuxt/consola/commit/39584d2))
* add missing parseStack import ([da53dee](https://github.com/nuxt/consola/commit/da53dee))
* also copy symbols in assignGlobalReference ([b0eefb5](https://github.com/nuxt/consola/commit/b0eefb5))
* don't return this when calling log functions ([f07e056](https://github.com/nuxt/consola/commit/f07e056))
* fix badge display ([e036eed](https://github.com/nuxt/consola/commit/e036eed))
* fix main field ([4b56e48](https://github.com/nuxt/consola/commit/4b56e48))
* fix typos ([45e2f99](https://github.com/nuxt/consola/commit/45e2f99))
* handle null value of obj for assignToLogObj ([d2402af](https://github.com/nuxt/consola/commit/d2402af))
* improve browser packaging ([4d8c8d0](https://github.com/nuxt/consola/commit/4d8c8d0))
* lint ([f909761](https://github.com/nuxt/consola/commit/f909761))
* lint ([d976620](https://github.com/nuxt/consola/commit/d976620))
* only one color ending parameter is enough ([d213634](https://github.com/nuxt/consola/commit/d213634))
* readme: icon string length is digit ([31f1894](https://github.com/nuxt/consola/commit/31f1894))
* remove name assignment ([8d59075](https://github.com/nuxt/consola/commit/8d59075))
* remove pushes for better readability ([418d84a](https://github.com/nuxt/consola/commit/418d84a))
* rename private fields ([244fe5c](https://github.com/nuxt/consola/commit/244fe5c))
* rename require test file ([cfc8f9e](https://github.com/nuxt/consola/commit/cfc8f9e))
* return earlier on not displaying levels ([cfdcf04](https://github.com/nuxt/consola/commit/cfdcf04))
* support Error as logObject ([134ff54](https://github.com/nuxt/consola/commit/134ff54))
* text color comment ([9336fbc](https://github.com/nuxt/consola/commit/9336fbc))
* update demo ([3842e0e](https://github.com/nuxt/consola/commit/3842e0e))
* use symbols for private property access ([8e6343c](https://github.com/nuxt/consola/commit/8e6343c))
### Code Refactoring
* additionalStyle ~> additionalColor ([3f808e9](https://github.com/nuxt/consola/commit/3f808e9))
### Features
* add __VERSION__ to consola prototype ([982c8ca](https://github.com/nuxt/consola/commit/982c8ca))
* add assignGlobalConsola helper ([1af28f7](https://github.com/nuxt/consola/commit/1af28f7))
* add getter/setter for level ([7af5ed5](https://github.com/nuxt/consola/commit/7af5ed5))
* add global.consola ([4da784d](https://github.com/nuxt/consola/commit/4da784d))
* add shmancy reporter ([dc6121a](https://github.com/nuxt/consola/commit/dc6121a))
* add symbols to browser ([30cd4f0](https://github.com/nuxt/consola/commit/30cd4f0))
* add sync/async write ([8525525](https://github.com/nuxt/consola/commit/8525525))
* add typescript typings ([#24](https://github.com/nuxt/consola/issues/24)) ([0853a6f](https://github.com/nuxt/consola/commit/0853a6f))
* align basic and fancy reporter tags ([38a4729](https://github.com/nuxt/consola/commit/38a4729))
* better stack formater ([f5acb3c](https://github.com/nuxt/consola/commit/f5acb3c))
* detect version changes and throw a warning ([73bdd1a](https://github.com/nuxt/consola/commit/73bdd1a))
* improve packaging and exports ([90da862](https://github.com/nuxt/consola/commit/90da862))
* improve packaging for browser support ([47af1df](https://github.com/nuxt/consola/commit/47af1df))
* initial works for v2.0.0 ([455b6f9](https://github.com/nuxt/consola/commit/455b6f9))
* log formatting using printf ([2afb025](https://github.com/nuxt/consola/commit/2afb025))
* no more side effects ([c015c31](https://github.com/nuxt/consola/commit/c015c31))
* pause/resume ([f217cc1](https://github.com/nuxt/consola/commit/f217cc1))
* return new consola instance with consola.create ([4ae3614](https://github.com/nuxt/consola/commit/4ae3614))
* rework _createLogFn with better argument handling ([2d4af39](https://github.com/nuxt/consola/commit/2d4af39))
* scope inheritance support ([#23](https://github.com/nuxt/consola/issues/23)) ([0070c54](https://github.com/nuxt/consola/commit/0070c54))
* **fancy/basic:** support logObj.stack field ([aa2216f](https://github.com/nuxt/consola/commit/aa2216f))
* setReporters, withDefaults and withTag ([912446f](https://github.com/nuxt/consola/commit/912446f))
* showType option ([ed294e4](https://github.com/nuxt/consola/commit/ed294e4))
* style browser reporter ([d39684d](https://github.com/nuxt/consola/commit/d39684d))
* support all chalk colors ([2cec678](https://github.com/nuxt/consola/commit/2cec678)), closes [#20](https://github.com/nuxt/consola/issues/20)
* wrapConsole ([3962a1f](https://github.com/nuxt/consola/commit/3962a1f))
* wrapStd ([f8bfbeb](https://github.com/nuxt/consola/commit/f8bfbeb))
* write error and warns to process.stderr by default ([6565254](https://github.com/nuxt/consola/commit/6565254))
### Performance Improvements
* **basic:** refactor getWriteMethod ([c52db69](https://github.com/nuxt/consola/commit/c52db69))
* remove all DEPRECATED helpers for less bundle size ([fe39d37](https://github.com/nuxt/consola/commit/fe39d37))
### BREAKING CHANGES
* Use new additionalColor prop
* lot's of internals had been changed.
* Behavior may be changed in some conditions
<a name="1.4.4"></a>
## [1.4.4](https://github.com/nuxt/consola/compare/v1.4.3...v1.4.4) (2018-10-13)
### Bug Fixes
* add global.consola ([558cae5](https://github.com/nuxt/consola/commit/558cae5))
<a name="1.4.3"></a>
## [1.4.3](https://github.com/nuxt/consola/compare/v1.4.2...v1.4.3) (2018-08-18)
### Bug Fixes
* use more compatible string to clear the console ([82ce410](https://github.com/nuxt/consola/commit/82ce410))
<a name="1.4.2"></a>
## [1.4.2](https://github.com/nuxt/consola/compare/v1.4.1...v1.4.2) (2018-08-12)
### Bug Fixes
* cannot set level as 0 in options ([4c1ecce](https://github.com/nuxt/consola/commit/4c1ecce))
<a name="1.4.1"></a>
## [1.4.1](https://github.com/nuxt/consola/compare/v1.4.0...v1.4.1) (2018-05-27)
### Bug Fixes
* **fancy:** logObj.type ([418be28](https://github.com/nuxt/consola/commit/418be28))
<a name="1.4.0"></a>
# [1.4.0](https://github.com/nuxt/consola/compare/v1.3.0...v1.4.0) (2018-05-27)
### Features
* support custom additional style ([#18](https://github.com/nuxt/consola/issues/18)) ([7a750bf](https://github.com/nuxt/consola/commit/7a750bf))
* **fancy:** support icon field ([0123bed](https://github.com/nuxt/consola/commit/0123bed))
<a name="1.3.0"></a>
# [1.3.0](https://github.com/nuxt/consola/compare/v1.2.0...v1.3.0) (2018-04-15)
### Bug Fixes
* **reporters/fancy:** extra space for additional ([efeab44](https://github.com/nuxt/consola/commit/efeab44))
* prevent duplicate consola instances when different versions used by packages ([0bce262](https://github.com/nuxt/consola/commit/0bce262))
### Features
* support extra log arguments ([8b6d3d2](https://github.com/nuxt/consola/commit/8b6d3d2))
<a name="1.2.0"></a>
# [1.2.0](https://github.com/nuxt/consola/compare/v1.1.4...v1.2.0) (2018-04-02)
### Features
* **basic:** support additional field ([b50cad8](https://github.com/nuxt/consola/commit/b50cad8))
* improve packaging ([158e8ef](https://github.com/nuxt/consola/commit/158e8ef))
### Performance Improvements
* require needed lodash methods only ([91065e4](https://github.com/nuxt/consola/commit/91065e4))
<a name="1.1.4"></a>
## [1.1.4](https://github.com/nuxt/consola/compare/v1.1.3...v1.1.4) (2018-03-31)
### Bug Fixes
* **package:** add chalk to dependencies ([3f738e9](https://github.com/nuxt/consola/commit/3f738e9))
<a name="1.1.3"></a>
## [1.1.3](https://github.com/nuxt/consola/compare/v1.1.2...v1.1.3) (2018-03-31)
### Bug Fixes
* only include dist and src in package ([8b477ec](https://github.com/nuxt/consola/commit/8b477ec))
<a name="1.1.2"></a>
## [1.1.2](https://github.com/nuxt/consola/compare/v1.1.1...v1.1.2) (2018-03-31)
### Bug Fixes
* handle null and undefined calls ([1f98bb1](https://github.com/nuxt/consola/commit/1f98bb1))
<a name="1.1.1"></a>
## [1.1.1](https://github.com/nuxt/consola/compare/v1.1.0...v1.1.1) (2018-03-31)
### Bug Fixes
* add prepublish script ([8dd8700](https://github.com/nuxt/consola/commit/8dd8700))
<a name="1.1.0"></a>
# [1.1.0](https://github.com/nuxt/consola/compare/v1.0.0...v1.1.0) (2018-03-31)
### Features
* rewrite FancyReporter without ora ([73c1ddc](https://github.com/nuxt/consola/commit/73c1ddc))
<a name="1.0.0"></a>
# [1.0.0](https://github.com/nuxt/consola/compare/v0.1.0...v1.0.0) (2018-03-31)
<a name="0.1.0"></a>
# 0.1.0 (2018-03-31)
### Features
* add log type for console compability ([96a8162](https://github.com/nuxt/consola/commit/96a8162))
+283
View File
@@ -0,0 +1,283 @@
<p align="center">
<h1>🐨 Consola</h1>
<span>Elegant Console Logger for Node.js and Browser</span>
</p>
[![Standard JS][standard-js-src]][standard-js-href]
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![package phobia][package-phobia-src]][package-phobia-href]
[![bundle phobia][bundle-phobia-src]][bundle-phobia-href]
## Why Consola?
- Easy to use
- Fancy output with fallback for minimal environments
- Pluggable reporters
- Consistent command line interface (CLI) experience
- Tag support
- Redirect `console` and `stdout/stderr` to the consola and easily restore redirect.
- Browser support
- Pause/Resume support
- Mocking support
- Spam preventation by throttling logs
## Installation
Using yarn:
```bash
yarn add consola
```
Using npm:
```bash
npm i consola
```
## Getting Started
```js
const consola = require('consola')
// See types section for all available types
consola.success('Built!')
consola.info('Reporter: Some info')
consola.error(new Error('Foo'))
```
NOTE: Alternatively, you can import consola from source. But don't forget to whitelist it for transpilation:
```js
import consola from 'consola/src/node'
import consola from 'consola/src/browser'
```
## Methods
#### `<type>(logObject)` `<type>(args...)`
Log to all reporters.
Example: `consola.info('Message')`
A list of available types can be found [here](./src/types.js).
#### `addReporter(reporter)`
- Aliases: `add`
Register a custom reporter instance.
#### `removeReporter(reporter?)`
- Aliases: `remove`, `clear`
Remove a registered reporter.
If no arguments are passed all reporters will be removed.
#### `setReporters(reporter|reporter[])`
Replace all reporters.
#### `create(options)`
Create a new `Consola` instance and inherit all parent options for defaults.
#### `withDefaults(defaults)`
Create a new `Consola` instance with provided defaults
#### `withTag(tag)`
- Aliases: `withScope`
Create a new `Consola` instance with that tag.
#### `wrapConsole()` `restoreConsole()`
Globally redirect all `console.log`, etc calls to consola handlers.
#### `wrapStd()` `restoreStd()`
Globally redirect all stdout/stderr outputs to consola.
#### `wrapAll()` `restoreAll()`
Wrap both, std and console.
console uses std in the underlying so calling `wrapStd` redirects console too.
Benefit of this function is that things like `console.info` will be correctly redirected to the corresponding type.
#### `pauseLogs()` `resumeLogs()`
- Aliases: `pause`/`resume`
**Globally** pause and resume logs.
Consola will enqueue all logs when paused and then sends them to the reported when resumed.
#### `mockTypes`
- Aliases: `mock`
Mock all types. Useful for using with tests.
The first argument passed to `mockTypes` should be a callback function accepting `(typeName, type)` and returning the mocked value:
```js
consola.mockTypes((typeName, type) => jest.fn())
```
Please note that with the example above, everything is mocked independently for each type. If you need one mocked fn create it outside:
```js
const fn = jest.fn()
consola.mockTypes(() => fn)
```
If callback function returns a _falsy_ value, that type won't be mocked.
For example if you just need to mock `consola.fatal`:
```js
consola.mockTypes((typeName) => typeName === 'fatal' && jest.fn())
```
**NOTE:** Any instance of consola that inherits the mocked instance, will apply provided callback again.
This way, mocking works for `withTag` scoped loggers without need to extra efforts.
## Fields
#### `reporters`
An array of active reporters.
#### `level`
The level to display logs. Any logs at or above this level will be displayed.
List of available levels [here](./src/types.js).
You can set log level using `CONSOLA_LEVEL` environment variable.
## `logObject`
The `logObject` is a free-to-extend object which will be passed to reporters.
Standard fields:
- `message`
- `additional`
- `args`
- `date`
- `tag`
Extra fields:
- `badge`
## Reporters
Choose between one of the built-in reporters or bring in your own one.
By default `FancyReporter` is registered for modern terminals or `BasicReporter` will be used if running in limited environments such as CIs.
Available reporters:
- [BasicReporter](./src/reporters/basic.js)
- [FancyReporter](./src/reporters/fancy.js)
- [JSONReporter](./src/reporters/json.js)
- [WinstonReporter](./src/reporters/winston.js)
### Creating your own reporter
A reporter (class or object) exposes `log(logObj)` method.
To get more info about how to write your own reporter, take a look into the linked implementations above.
## Types
Types are used to actually log messages to the reporters.
Each type is attached to a _logging level_.
A list of all available default types is [here](./src/types.js).
## Creating a new instance
Consola has a global instance and is recommended to use everywhere.
In case more control is needed, create a new instance.
```js
import consola from 'consola'
const logger = consola.create({
// level: 4,
reporters: [
new consola.JSONReporter()
],
defaults: {
additionalColor: 'white'
}
})
```
## Integrations
### With jest
```js
describe('your-consola-mock-test', () => {
beforeAll(() => {
// Redirect std and console to consola too
// Calling this once is sufficient
consola.wrapAll()
})
beforeEach(() => {
// Re-mock consola before each test call to remove
// calls from before
consola.mockTypes(() => jest.fn())
})
test('your test', async () => {
// Some code here
// Let's retrieve all messages of `consola.log`
// Get the mock and map all calls to their first argument
const consolaMessages = consola.log.mock.calls.map(c => c[0])
expect(consolaMessages).toContain('your message')
})
})
```
### With jsdom
```js
{
virtualConsole: new jsdom.VirtualConsole().sendTo(consola)
}
```
## License
MIT - Made with 💖 By Nuxt.js team!
<!-- Refs -->
[standard-js-src]: https://flat.badgen.net/badge/code%20style/standard/green
[standard-js-href]: https://standardjs.com
[npm-version-src]: https://flat.badgen.net/npm/v/consola/latest
[npm-version-href]: https://npmjs.com/package/consola
[npm-downloads-src]: https://flat.badgen.net/npm/dt/consola
[npm-downloads-href]: https://npmjs.com/package/consola
[package-phobia-src]: https://flat.badgen.net/packagephobia/install/consola
[package-phobia-href]: https://packagephobia.now.sh/result?p=consola
[bundle-phobia-src]: https://flat.badgen.net/bundlephobia/minzip/consola
[bundle-phobia-href]: https://bundlephobia.com/result?p=consola
+1
View File
File diff suppressed because one or more lines are too long
+1
View File
File diff suppressed because one or more lines are too long
+104
View File
@@ -0,0 +1,104 @@
{
"_from": "consola@^2.10.1",
"_id": "consola@2.11.0",
"_inBundle": false,
"_integrity": "sha512-2bcAqHastlPSCvZ+ur8bgHInGAWvUnysWz3h3xRX+/XZoCY7avolJJnVXOPGoVoyCcg1b231XixonoArmgxaoA==",
"_location": "/consola",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "consola@^2.10.1",
"name": "consola",
"escapedName": "consola",
"rawSpec": "^2.10.1",
"saveSpec": null,
"fetchSpec": "^2.10.1"
},
"_requiredBy": [
"/@nuxt/opencollective"
],
"_resolved": "https://registry.npmjs.org/consola/-/consola-2.11.0.tgz",
"_shasum": "9bb35d850d8cecde894ce2eb4d792fa6b90d9013",
"_spec": "consola@^2.10.1",
"_where": "/home/george/citwa/red_de_investigacion_front/first/node_modules/@nuxt/opencollective",
"browser": "dist/consola.browser.js",
"bugs": {
"url": "https://github.com/nuxt/consola/issues"
},
"bundleDependencies": false,
"contributes": [
"Pooya Parsa <pooya@pi0.ir>",
"Clark Du (@clarkdo)",
"@pimlie <pimlie@hotmail.com>",
"Sébastien Chopin <seb@orion.sh>"
],
"deprecated": false,
"description": "Elegant Console Logger for Node.js and Browser",
"devDependencies": {
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"babel-eslint": "^10.0.3",
"babel-jest": "^24.9.0",
"benchmark": "^2.1.4",
"bili": "^4.8.1",
"chalk": "^3.0.0",
"dayjs": "^1.8.17",
"eslint": "^6.6.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^23.0.3",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"esm": "^3.2.25",
"figures": "^3.1.0",
"jest": "^24.9.0",
"lodash": "^4.17.15",
"sentencer": "^0.2.0",
"serve": "^11.2.0",
"standard-version": "^7.0.0",
"std-env": "^2.2.1",
"string-width": "^4.1.0",
"ts-node": "^8.4.1",
"typescript": "^3.7.2",
"winston": "^3.2.1"
},
"files": [
"src",
"dist",
"types"
],
"homepage": "https://github.com/nuxt/consola#readme",
"keywords": [
"console",
"logger",
"reporter",
"elegant",
"cli",
"universal",
"unified",
"winston"
],
"license": "MIT",
"main": "dist/consola.js",
"name": "consola",
"repository": {
"type": "git",
"url": "git+https://github.com/nuxt/consola.git"
},
"scripts": {
"browser": "serve",
"build": "rm -rf dist && yarn build:node && yarn build:browser",
"build:browser": "NODE_ENV=browser bili src/browser.js --file-name consola.browser.js --format umd --module-name consola --bundle-node-modules --minify --no-map",
"build:node": "NODE_ENV=node bili src/node.js --file-name consola.js --format cjs --bundle-node-modules --minify --no-map",
"demo": "node demo",
"lint": "eslint .",
"prepublish": "yarn build",
"release": "standard-version && yarn build && git push --follow-tags && npm publish",
"test": "yarn lint && yarn test:types && yarn build && jest test",
"test:types": "tsc -p test/types"
},
"typings": "types/consola.d.ts",
"version": "2.11.0"
}
+18
View File
@@ -0,0 +1,18 @@
import Consola from './consola.js'
import BrowserReporter from './reporters/browser.js'
function createConsola () {
const consola = new Consola({
reporters: [
new BrowserReporter()
]
})
// Expose constructors
consola.Consola = Consola
consola.BrowserReporter = BrowserReporter
return consola
}
export default (typeof window !== 'undefined' && window.consola) || createConsola()
+328
View File
@@ -0,0 +1,328 @@
import Types from './types.js'
import { isLogObj } from './utils/index.js'
let paused = false
const queue = []
class Consola {
constructor (options = {}) {
this._reporters = options.reporters || []
this._types = options.types || Types
this._level = options.level != null ? options.level : 3
this._defaults = options.defaults || {}
this._async = typeof options.async !== 'undefined' ? options.async : null
this._stdout = options.stdout
this._stderr = options.stdout
this._mockFn = options.mockFn
this._throttle = options.throttle || 2000
// Create logger functions for current instance
for (const type in this._types) {
this[type] = this._wrapLogFn(Object.assign(
{ type },
this._types[type],
this._defaults
))
}
// Use _mockFn if is set
if (this._mockFn) {
this.mockTypes()
}
// Keep serialized version of last log
this._lastLogSerialized = null
this._lastLog = null
this._lastLogTime = null
this._lastLogCount = 0
}
get level () {
return this._level
}
set level (newLevel) {
// Ensure that newLevel does not exceeds type level boundaries
let min = 0
let max = 0
for (const typeName in this._types) {
const type = this._types[typeName]
if (type.level > max) {
max = type.level
} else if (type.level < min) {
min = type.level
}
}
// Set level
this._level = Math.min(max, Math.max(min, newLevel))
}
get stdout () {
return this._stdout || console._stdout // eslint-disable-line no-console
}
get stderr () {
return this._stderr || console._stderr // eslint-disable-line no-console
}
create (options) {
return new Consola(Object.assign({
reporters: this._reporters,
level: this._level,
types: this._types,
defaults: this._defaults,
stdout: this._stdout,
stderr: this._stderr,
mockFn: this._mockFn
}, options))
}
withDefaults (defaults) {
return this.create({
defaults: Object.assign({}, this._defaults, defaults)
})
}
withTag (tag) {
return this.withDefaults({
tag: this._defaults.tag ? (this._defaults.tag + ':' + tag) : tag
})
}
addReporter (reporter) {
this._reporters.push(reporter)
return this
}
removeReporter (reporter) {
if (reporter) {
const i = this._reporters.indexOf(reporter)
if (i >= 0) {
return this._reporters.splice(i, 1)
}
} else {
this._reporters.splice(0)
}
return this
}
setReporters (reporters) {
this._reporters = Array.isArray(reporters)
? reporters
: [reporters]
return this
}
wrapAll () {
this.wrapConsole()
this.wrapStd()
}
restoreAll () {
this.restoreConsole()
this.restoreStd()
}
wrapConsole () {
for (const type in this._types) {
// Backup original value
if (!console['__' + type]) { // eslint-disable-line no-console
console['__' + type] = console[type] // eslint-disable-line no-console
}
// Override
console[type] = this[type] // eslint-disable-line no-console
}
}
restoreConsole () {
for (const type in this._types) {
// Restore if backup is available
if (console['__' + type]) { // eslint-disable-line no-console
console[type] = console['__' + type] // eslint-disable-line no-console
delete console['__' + type] // eslint-disable-line no-console
}
}
}
wrapStd () {
this._wrapStream(this.stdout, 'log')
this._wrapStream(this.stderr, 'log')
}
_wrapStream (stream, type) {
if (!stream) {
return
}
// Backup original value
if (!stream.__write) {
stream.__write = stream.write
}
// Override
stream.write = (data) => {
this[type](String(data).trim())
}
}
restoreStd () {
this._restoreStream(this.stdout)
this._restoreStream(this.stderr)
}
_restoreStream (stream) {
if (!stream) {
return
}
if (stream.__write) {
stream.write = stream.__write
delete stream.__write
}
}
pauseLogs () {
paused = true
}
resumeLogs () {
paused = false
// Process queue
const _queue = queue.splice(0)
for (const item of _queue) {
item[0]._logFn(item[1], item[2])
}
}
mockTypes (mockFn) {
this._mockFn = mockFn || this._mockFn
if (typeof this._mockFn !== 'function') {
return
}
for (const type in this._types) {
this[type] = this._mockFn(type, this._types[type]) || this[type]
}
}
_wrapLogFn (defaults) {
function logFn () {
if (paused) {
queue.push([this, defaults, arguments])
return
}
return this._logFn(defaults, arguments)
}
return logFn.bind(this)
}
_logFn (defaults, args) {
if (defaults.level > this._level) {
return this._async ? Promise.resolve(false) : false
}
// Construct a new log object
const logObj = Object.assign({
date: new Date(),
args: []
}, defaults)
// Consume arguments
if (args.length === 1 && isLogObj(args[0])) {
Object.assign(logObj, args[0])
} else {
logObj.args = Array.from(args)
}
// Aliases
if (logObj.message) {
logObj.args.unshift(logObj.message)
delete logObj.message
}
if (logObj.additional) {
if (!Array.isArray(logObj.additional)) {
logObj.additional = logObj.additional.split('\n')
}
logObj.args.push('\n' + logObj.additional.join('\n'))
delete logObj.additional
}
// Normalize type and tag to lowercase
logObj.type = typeof logObj.type === 'string' ? logObj.type.toLowerCase() : ''
logObj.tag = typeof logObj.tag === 'string' ? logObj.tag.toLowerCase() : ''
// Throttle
const diffTime = this._lastLogTime ? logObj.date - this._lastLogTime : 0
this._lastLogTime = logObj.date
if (diffTime < this._throttle) {
try {
const serializedLog = JSON.stringify([logObj.type, logObj.tag, logObj.args])
const isSameLog = this._lastLogSerialized === serializedLog
this._lastLogSerialized = serializedLog
if (isSameLog) {
this._lastLogCount++
return // SPAM!
}
} catch (_) {
// Circular References
}
}
if (this._lastLogCount) {
this._log({
...this._lastLog,
args: [
...this._lastLog.args,
`(repeated ${this._lastLogCount} times)`
]
})
this._lastLogCount = 0
}
this._lastLog = logObj
// Log
if (this._async) {
return this._logAsync(logObj)
} else {
this._log(logObj)
}
}
_log (logObj) {
for (const reporter of this._reporters) {
reporter.log(logObj, {
async: false,
stdout: this.stdout,
stderr: this.stderr
})
}
}
_logAsync (logObj) {
return Promise.all(
this._reporters.map(reporter => reporter.log(logObj, {
async: true,
stdout: this.stdout,
stderr: this.stderr
}))
)
}
}
// Legacy support
Consola.prototype.add = Consola.prototype.addReporter
Consola.prototype.remove = Consola.prototype.removeReporter
Consola.prototype.clear = Consola.prototype.removeReporter
Consola.prototype.withScope = Consola.prototype.withTag
Consola.prototype.mock = Consola.prototype.mockTypes
Consola.prototype.pause = Consola.prototype.pauseLogs
Consola.prototype.resume = Consola.prototype.resumeLogs
// Export class
export default Consola
+6
View File
@@ -0,0 +1,6 @@
export { default as Consola } from './consola'
export { default as Types } from './types'
export { isLogObj } from './utils'
export { assignGlobalConsola } from './utils/global'
export * from './reporters'
+35
View File
@@ -0,0 +1,35 @@
import env from 'std-env'
import { Consola, BasicReporter, FancyReporter, JSONReporter, WinstonReporter } from '.'
function createConsola () {
// Log level
let level = env.debug ? 4 : 3
if (process.env.CONSOLA_LEVEL) {
level = parseInt(process.env.CONSOLA_LEVEL) || level
}
// Create new consola instance
const consola = new Consola({
level,
reporters: [
(env.ci || env.test)
? new BasicReporter()
: new FancyReporter()
]
})
// Expose constructors
consola.Consola = Consola
consola.BasicReporter = BasicReporter
consola.FancyReporter = FancyReporter
consola.JSONReporter = JSONReporter
consola.WinstonReporter = WinstonReporter
return consola
}
if (!global.consola) {
global.consola = createConsola()
}
export default global.consola
+71
View File
@@ -0,0 +1,71 @@
import util from 'util'
import { parseStack } from '../utils/error'
import { writeStream } from '../utils/stream'
import { formatDate } from '../utils/date'
const DEFAULTS = {
dateFormat: 'HH:mm:ss',
formatOptions: {
colors: false,
compact: true
}
}
const bracket = x => x ? `[${x}]` : ''
export default class BasicReporter {
constructor (options) {
this.options = Object.assign({}, DEFAULTS, options)
}
formatStack (stack) {
return ' ' + parseStack(stack).join('\n ')
}
formatArgs (args) {
const _args = args.map(arg => {
if (arg && typeof arg.stack === 'string') {
return arg.message + '\n' + this.formatStack(arg.stack)
}
return arg
})
// Only supported with Node >= 10
// https://nodejs.org/api/util.html#util_util_inspect_object_options
if (typeof util.formatWithOptions === 'function') {
return util.formatWithOptions(this.options.formatOptions, ..._args)
} else {
return util.format(..._args)
}
}
formatDate (date) {
return formatDate(this.options.dateFormat, date)
}
filterAndJoin (arr) {
return arr.filter(x => x).join(' ')
}
formatLogObj (logObj) {
const message = this.formatArgs(logObj.args)
return this.filterAndJoin([
bracket(logObj.type),
bracket(logObj.tag),
message
])
}
log (logObj, { async, stdout, stderr } = {}) {
const line = this.formatLogObj(logObj, {
width: stdout.columns || 0
})
return writeStream(
line + '\n',
logObj.level < 2 ? stderr : stdout,
async ? 'async' : 'default'
)
}
}
+54
View File
@@ -0,0 +1,54 @@
export default class BrowserReporter {
constructor (options) {
this.options = Object.assign({}, options)
this.defaultColor = '#7f8c8d' // Gray
this.levelColorMap = {
0: '#c0392b', // Red
1: '#f39c12', // Yellow
3: '#00BCD4' // Cyan
}
this.typeColorMap = {
success: '#2ecc71' // Green
}
}
log (logObj) {
const consoleLogFn = logObj.level < 1
// eslint-disable-next-line no-console
? (console.__error || console.error)
// eslint-disable-next-line no-console
: logObj.level === 1 && console.warn ? (console.__warn || console.warn) : (console.__log || console.log)
// Type
const type = logObj.type !== 'log' ? logObj.type : ''
// Tag
const tag = logObj.tag ? logObj.tag : ''
// Styles
const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor
const style = `
background: ${color};
border-radius: 0.5em;
color: white;
font-weight: bold;
padding: 2px 0.5em;
`
const badge = `%c${[tag, type].filter(Boolean).join(':')}`
// Log to the console
if (typeof logObj.args[0] === 'string') {
consoleLogFn(
`${badge}%c ${logObj.args[0]}`,
style,
// Empty string as style resets to default console style
'',
...logObj.args.slice(1)
)
} else {
consoleLogFn(badge, style, ...logObj.args)
}
}
}
+85
View File
@@ -0,0 +1,85 @@
import stringWidth from 'string-width'
import figures from 'figures'
import chalk from 'chalk'
import BasicReporter from './basic'
import { parseStack } from '../utils/error'
import { chalkColor, chalkBgColor } from '../utils/chalk'
import { TYPE_COLOR_MAP, LEVEL_COLOR_MAP } from '../utils/fancy'
const DEFAULTS = {
secondaryColor: 'grey',
formatOptions: {
colors: true,
compact: false
}
}
const TYPE_ICONS = {
info: figures(''),
success: figures('✔'),
debug: figures(''),
trace: figures(''),
log: ''
}
export default class FancyReporter extends BasicReporter {
constructor (options) {
super(Object.assign({}, DEFAULTS, options))
}
formatStack (stack) {
const grey = chalkColor('grey')
const cyan = chalkColor('cyan')
return '\n' + parseStack(stack)
.map(line => ' ' + line
.replace(/^at +/, m => grey(m))
.replace(/\((.+)\)/, (_, m) => `(${cyan(m)})`)
)
.join('\n')
}
formatType (logObj, isBadge) {
const typeColor = TYPE_COLOR_MAP[logObj.type] ||
LEVEL_COLOR_MAP[logObj.level] ||
this.options.secondaryColor
if (isBadge) {
return chalkBgColor(typeColor).black(` ${logObj.type.toUpperCase()} `)
}
const _type = typeof TYPE_ICONS[logObj.type] === 'string' ? TYPE_ICONS[logObj.type] : (logObj.icon || logObj.type)
return _type ? chalkColor(typeColor)(_type) : ''
}
formatLogObj (logObj, { width }) {
const [message, ...additional] = this.formatArgs(logObj.args).split('\n')
const isBadge = typeof logObj.badge !== 'undefined' ? Boolean(logObj.badge) : logObj.level < 2
const secondaryColor = chalkColor(this.options.secondaryColor)
const date = secondaryColor(this.formatDate(logObj.date))
const type = this.formatType(logObj, isBadge)
const tag = logObj.tag ? secondaryColor(logObj.tag) : ''
const formattedMessage = message.replace(/`([^`]+)`/g, (_, m) => chalk.cyan(m))
let line
const left = this.filterAndJoin([type, formattedMessage])
const right = this.filterAndJoin([tag, date])
const space = width - stringWidth(left) - stringWidth(right) - 2
if (space > 0 && width >= 80) {
line = left + ' '.repeat(space) + right
} else {
line = left
}
line += additional.length ? '\n' + additional.join('\n') : ''
return isBadge ? '\n' + line + '\n' : line
}
}
+5
View File
@@ -0,0 +1,5 @@
export { default as BasicReporter } from './basic'
export { default as BrowserReporter } from './browser'
export { default as FancyReporter } from './fancy'
export { default as JSONReporter } from './json'
export { default as WinstonReporter } from './winston'
+9
View File
@@ -0,0 +1,9 @@
export default class JSONReporter {
constructor ({ stream } = {}) {
this.stream = stream || process.stdout
}
log (logObj) {
this.stream.write(JSON.stringify(logObj) + '\n')
}
}
+45
View File
@@ -0,0 +1,45 @@
// This reporter is compatible with Winston 3
// https://github.com/winstonjs/winston
// eslint-disable-next-line
const _require = typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : require // bypass webpack
export default class WinstonReporter {
constructor (logger) {
if (logger && logger.log) {
this.logger = logger
} else {
const winston = _require('winston')
this.logger = winston.createLogger(Object.assign({
level: 'info',
format: winston.format.simple(),
transports: [
new winston.transports.Console()
]
}, logger))
}
}
log (logObj) {
const args = [].concat(logObj.args)
const arg0 = args.shift()
this.logger.log({
level: levels[logObj.level] || 'info',
label: logObj.tag,
message: arg0,
args: args,
timestamp: logObj.date.getTime() / 1000
})
}
}
const levels = {
0: 'error',
1: 'warn',
2: 'info',
3: 'verbose',
4: 'debug',
5: 'silly'
}
+44
View File
@@ -0,0 +1,44 @@
export default {
// Level 0
fatal: {
level: 0
},
error: {
level: 0
},
// Level 1
warn: {
level: 1
},
// Level 2
log: {
level: 2
},
// Level 3
info: {
level: 3
},
success: {
level: 3
},
// Level 4
debug: {
level: 4
},
// Level 5
trace: {
level: 5
},
// Silent
silent: {
level: Infinity
},
// Legacy
ready: {
level: 3
},
start: {
level: 3
}
}
+37
View File
@@ -0,0 +1,37 @@
import chalk from 'chalk'
const _colorCache = {}
export function chalkColor (name) {
let color = _colorCache[name]
if (color) {
return color
}
if (name[0] === '#') {
color = chalk.hex(name)
} else {
color = chalk[name] || chalk.keyword(name)
}
_colorCache[name] = color
return color
}
const _bgColorCache = {}
export function chalkBgColor (name) {
let color = _bgColorCache[name]
if (color) {
return color
}
if (name[0] === '#') {
color = chalk.bgHex(name)
} else {
color = chalk['bg' + name[0].toUpperCase() + name.slice(1)] || chalk.bgKeyword(name)
}
_bgColorCache[name] = color
return color
}
+5
View File
@@ -0,0 +1,5 @@
import dayjs from 'dayjs'
export function formatDate (timeFormat, date) {
return dayjs(date).format(timeFormat)
}
+16
View File
@@ -0,0 +1,16 @@
import { sep } from 'path'
export function parseStack (stack) {
const cwd = process.cwd() + sep
const lines = stack
.split('\n')
.splice(1)
.map(l => l
.trim()
.replace('file://', '')
.replace(cwd, '')
)
return lines
}
+10
View File
@@ -0,0 +1,10 @@
export const TYPE_COLOR_MAP = {
info: 'cyan'
}
export const LEVEL_COLOR_MAP = {
0: 'red',
1: 'yellow',
2: 'white',
3: 'green'
}
+30
View File
@@ -0,0 +1,30 @@
import { vsprintf } from 'printj'
const FORMAT_ARGS = [
['additional', 5],
['message', 4],
['type', 2],
['date', 1],
['tag', 3]
] // .sort((a, b) => b[0].length - a[0].length)
const _compileCache = {}
// process.on('beforeExit', () => { console.log(_compileCache) })
export function compileFormat (format) {
if (_compileCache[format]) {
return _compileCache[format]
}
let _format = format
for (const arg of FORMAT_ARGS) {
_format = _format.replace(new RegExp('([%-])' + arg[0], 'g'), '$1' + arg[1])
}
_compileCache[format] = _format
return _format
}
export function formatString (format, argv) {
return vsprintf(compileFormat(format), argv)
}
+38
View File
@@ -0,0 +1,38 @@
export function assignGlobalReference (newInstance, referenceKey) {
if (!newInstance.constructor || (global[referenceKey] && !global[referenceKey].constructor)) {
throw new Error('Assigning to global reference is only supported for class instances')
} else if (newInstance.constructor && !global[referenceKey]) {
global[referenceKey] = newInstance
} else if (!(
newInstance instanceof global[referenceKey].constructor ||
global[referenceKey] instanceof newInstance.constructor
)) {
throw new Error(`Not a ${global[referenceKey].constructor.name} instance`)
}
const oldInstance = Object.create(global[referenceKey])
for (const prop in global[referenceKey]) {
oldInstance[prop] = global[referenceKey][prop]
delete global[referenceKey][prop]
}
for (const prop of Object.getOwnPropertySymbols(global[referenceKey])) {
oldInstance[prop] = global[referenceKey][prop]
delete global[referenceKey][prop]
}
for (const prop in newInstance) {
global[referenceKey][prop] = newInstance[prop]
}
for (const prop of Object.getOwnPropertySymbols(newInstance)) {
global[referenceKey][prop] = newInstance[prop]
}
return oldInstance
}
export function assignGlobalConsola (newConsola) {
return assignGlobalReference(newConsola, 'consola')
}
+23
View File
@@ -0,0 +1,23 @@
export function isPlainObject (obj) {
return Object.prototype.toString.call(obj) === '[object Object]'
}
// TODO: remove for consola@3
export function isLogObj (arg) {
// Should be plain object
if (!isPlainObject(arg)) {
return false
}
// Should contains either 'message' or 'args' field
if (!arg.message && !arg.args) {
return false
}
// Handle non-standard error objects
if (arg.stack) {
return false
}
return true
}
+20
View File
@@ -0,0 +1,20 @@
import { writeSync } from 'fs'
export function writeStream (data, stream, mode = 'default') {
const write = stream.__write || stream.write
switch (mode) {
case 'async':
return new Promise((resolve) => {
if (write.call(stream, data) === true) {
resolve()
} else {
stream.once('drain', () => { resolve() })
}
})
case 'sync':
return writeSync(stream.fd, data)
default:
return write.call(stream, data)
}
}
+41
View File
@@ -0,0 +1,41 @@
export function centerAlign (str, len, space = ' ') {
const free = len - str.length
if (free <= 0) {
return str
}
const freeLeft = Math.floor(free / 2)
let _str = ''
for (let i = 0; i < len; i++) {
_str += (i < freeLeft || i >= freeLeft + str.length) ? space : str[i - freeLeft]
}
return _str
}
export function rightAlign (str, len, space = ' ') {
const free = len - str.length
if (free <= 0) {
return str
}
let _str = ''
for (let i = 0; i < len; i++) {
_str += i < free ? space : str[i - free]
}
return _str
}
export function leftAlign (str, len, space = ' ') {
let _str = ''
for (let i = 0; i < len; i++) {
_str += i < str.length ? str[i] : space
}
return _str
}
export function align (alignment, str, len, space = ' ') {
switch (alignment) {
case 'left': return leftAlign(str, len, space)
case 'right': return rightAlign(str, len, space)
case 'center': return centerAlign(str, len, space)
default: return str
}
}
+96
View File
@@ -0,0 +1,96 @@
export interface ConsolaLogObject {
level?: number,
tag?: string,
type?: string,
message?: string,
additional?: string | string[],
args?: any[],
}
type ConsolaMock = (...args: any) => void
type ConsolaMockFn = (type: string, defaults: ConsolaLogObject) => ConsolaMock
export interface ConsolaReporterArgs {
async: boolean,
stdout: any,
stderr: any,
}
export interface ConsolaReporter {
log: (logObj: ConsolaLogObject, args: ConsolaReporterArgs) => void
}
export interface ConsolaOptions {
reporters?: ConsolaReporter[],
types?: { [type: string]: ConsolaLogObject },
level?: number,
defaults?: ConsolaLogObject,
async?: boolean,
stdout?: any,
stderr?: any,
mockFn?: ConsolaMockFn,
throttle?: number,
}
export declare class Consola {
constructor(options: ConsolaOptions)
level: number
readonly stdout: any
readonly stderr: any
// Built-in log levels
fatal(message: ConsolaLogObject | any, ...args: any[]): void
error(message: ConsolaLogObject | any, ...args: any[]): void
warn(message: ConsolaLogObject | any, ...args: any[]): void
log(message: ConsolaLogObject | any, ...args: any[]): void
info(message: ConsolaLogObject | any, ...args: any[]): void
start(message: ConsolaLogObject | any, ...args: any[]): void
success(message: ConsolaLogObject | any, ...args: any[]): void
ready(message: ConsolaLogObject | any, ...args: any[]): void
debug(message: ConsolaLogObject | any, ...args: any[]): void
trace(message: ConsolaLogObject | any, ...args: any[]): void
// Create
create(options: ConsolaOptions): Consola
withDefaults(defaults: ConsolaLogObject): Consola
withTag(tag: string): Consola
withScope(tag: string): Consola
// Reporter
addReporter(reporter: ConsolaReporter): Consola
setReporters(reporters: Array<ConsolaReporter>): Consola
removeReporter(reporter?: ConsolaReporter): Consola
remove(reporter?: ConsolaReporter): Consola
clear(reporter?: ConsolaReporter): Consola
// Wrappers
wrapAll(): void
restoreAll(): void
wrapConsole(): void
restoreConsole(): void
wrapStd(): void
restoreStd(): void
// Pause/Resume
pauseLogs(): void
pause(): void
resumeLogs(): void
resume(): void
// Mock
mockTypes(mockFn: ConsolaMockFn): any
mock(mockFn: ConsolaMockFn): any
}
export declare class BrowserReporter implements ConsolaReporter {
log: (logObj: ConsolaLogObject, args: ConsolaReporterArgs) => void
}
declare const consolaGlobalInstance: Consola;
export default consolaGlobalInstance