Add logger for consistent info output (#14)
* Add logger as a util * Add logger * Update flow checks
This commit is contained in:
@@ -8,5 +8,6 @@ tmp/
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
npm-debug.log*
|
||||
package-lock.json
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
+20
-1
@@ -7,4 +7,23 @@ Utility methods for this package are split into groups
|
||||
- [hex](hex.md) Internal utilities to create and test for hex values
|
||||
- [is](is.md) Type checking utilities
|
||||
- [jsonrpc](jsonrpc.md) Convenience functions for values from @polkadot/jsonrpc
|
||||
- [keccak](keccak.md) Create Keccak256 values as hex, string & buffer output
|
||||
- [keccak](keccak.md) Create Keccak256 values as hex, string & buffer output
|
||||
|
||||
# Available methods
|
||||
|
||||
## Logger
|
||||
|
||||
Creates a consistent log interface for messages
|
||||
|
||||
```js
|
||||
logger (type: string): Logger
|
||||
```
|
||||
|
||||
|
||||
Returns a `Logger` that has `.log`, `.error` and `.warn` methods. Loggins is done with a consistent prefix (type of logger, date) followed by the actual message uning the underlying console.
|
||||
|
||||
```js
|
||||
const l = require('@polkadot/util/logger')('test');
|
||||
|
||||
l.log('blah'); // <date> TEST: blah
|
||||
```
|
||||
+2
-2
@@ -2,8 +2,8 @@
|
||||
|
||||
Utility methods to convert to and from `BN` objects
|
||||
|
||||
- [bnFromHex](#bnFromHex) Creates a BN.js bignumber object from a hex string.
|
||||
- [bnToHex](#bnToHex) Creates a hex value from a BN.js bignumber object.
|
||||
- [bnFromHex](#bnfromhex) Creates a BN.js bignumber object from a hex string.
|
||||
- [bnToHex](#bntohex) Creates a hex value from a BN.js bignumber object.
|
||||
|
||||
## bnFromHex
|
||||
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@
|
||||
|
||||
Utility methods to convert to and from `Buffer` objects
|
||||
|
||||
- [bufferFromHex](#bufferFromHex) Creates a Buffer object from a hex string.
|
||||
- [bufferToHex](#bufferToHex) Creates a hex value from a Buffer object.
|
||||
- [bufferFromHex](#bufferfromhex) Creates a Buffer object from a hex string.
|
||||
- [bufferToHex](#buffertohex) Creates a hex value from a Buffer object.
|
||||
|
||||
## bufferFromHex
|
||||
|
||||
|
||||
+3
-3
@@ -2,9 +2,9 @@
|
||||
|
||||
Internal utilities to create and test for hex values
|
||||
|
||||
- [hexAddPrefix](#hexAddPrefix) Adds the `0x` prefix to string values.
|
||||
- [hexHasPrefix](#hexHasPrefix) Tests for the existence of a `0x` prefix.
|
||||
- [hexStripPrefix](#hexStripPrefix) Strips any leading `0x` prefix.
|
||||
- [hexAddPrefix](#hexaddprefix) Adds the `0x` prefix to string values.
|
||||
- [hexHasPrefix](#hexhasprefix) Tests for the existence of a `0x` prefix.
|
||||
- [hexStripPrefix](#hexstripprefix) Strips any leading `0x` prefix.
|
||||
|
||||
## hexAddPrefix
|
||||
|
||||
|
||||
+8
-8
@@ -2,14 +2,14 @@
|
||||
|
||||
Type checking utilities
|
||||
|
||||
- [isBN](#isBN) Tests for a `BN` object instance.
|
||||
- [isBuffer](#isBuffer) Tests for a `Buffer` object instance.
|
||||
- [isFunction](#isFunction) Tests for a `function`.
|
||||
- [isHex](#isHex) Tests for a hex string.
|
||||
- [isInstanceOf](#isInstanceOf) Tests for a instance of a class.
|
||||
- [isNumber](#isNumber) Tests for a JavaScript number.
|
||||
- [isString](#isString) Tests for a string.
|
||||
- [isUndefined](#isUndefined) Tests for a `undefined` values.
|
||||
- [isBN](#isbn) Tests for a `BN` object instance.
|
||||
- [isBuffer](#isbuffer) Tests for a `Buffer` object instance.
|
||||
- [isFunction](#isfunction) Tests for a `function`.
|
||||
- [isHex](#ishex) Tests for a hex string.
|
||||
- [isInstanceOf](#isinstanceof) Tests for a instance of a class.
|
||||
- [isNumber](#isnumber) Tests for a JavaScript number.
|
||||
- [isString](#isstring) Tests for a string.
|
||||
- [isUndefined](#isundefined) Tests for a `undefined` values.
|
||||
|
||||
## isBN
|
||||
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
Convenience functions for values from @polkadot/jsonrpc
|
||||
|
||||
- [jsonrpcSignature](#jsonrpcSignature) Returns a string representation of the method with inputs and outputs.
|
||||
- [jsonrpcSignature](#jsonrpcsignature) Returns a string representation of the method with inputs and outputs.
|
||||
|
||||
## jsonrpcSignature
|
||||
|
||||
|
||||
+3
-3
@@ -2,9 +2,9 @@
|
||||
|
||||
Create Keccak256 values as hex, string & buffer output
|
||||
|
||||
- [keccakAsBuffer](#keccakAsBuffer) Creates a keccak Buffer from the input.
|
||||
- [keccakAsHex](#keccakAsHex) Creates a keccak hex string from the input.
|
||||
- [keccakAsString](#keccakAsString) Creates a keccak string from the input.
|
||||
- [keccakAsBuffer](#keccakasbuffer) Creates a keccak Buffer from the input.
|
||||
- [keccakAsHex](#keccakashex) Creates a keccak hex string from the input.
|
||||
- [keccakAsString](#keccakasstring) Creates a keccak string from the input.
|
||||
|
||||
## keccakAsBuffer
|
||||
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@
|
||||
"test": "jest --coverage src"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/dev": "^0.3.1",
|
||||
"@polkadot/dev": "^0.3.2",
|
||||
"@polkadot/jsonrpc": "^0.3.9"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
+2
-1
@@ -7,10 +7,11 @@ const hex = require('./hex');
|
||||
const is = require('./is');
|
||||
const jsonrpc = require('./jsonrpc');
|
||||
const keccak = require('./keccak');
|
||||
const logger = require('./logger');
|
||||
|
||||
/**
|
||||
@summary Utility methods for this package are split into groups
|
||||
*/
|
||||
module.exports = Object.assign(
|
||||
{}, bn, buffer, hex, is, jsonrpc, keccak
|
||||
{}, bn, buffer, hex, is, jsonrpc, keccak, logger
|
||||
);
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
type Logger = {
|
||||
error: (params: any) => void,
|
||||
log: (params: any) => void,
|
||||
warn: (params: any) => void
|
||||
};
|
||||
|
||||
function format (type: string, values: Array<any>): Array<any> {
|
||||
return [new Date().toString(), type].concat(values);
|
||||
}
|
||||
|
||||
function error (type: string, values: Array<any>): void {
|
||||
console.error.apply(console, format(type, values));
|
||||
}
|
||||
|
||||
function log (type: string, values: Array<any>): void {
|
||||
console.log.apply(console, format(type, values));
|
||||
}
|
||||
|
||||
function warn (type: string, values: Array<any>): void {
|
||||
console.warn.apply(console, format(type, values));
|
||||
}
|
||||
|
||||
/**
|
||||
@name Logger
|
||||
@signature logger (type: string): Logger
|
||||
@summary Creates a consistent log interface for messages
|
||||
@description
|
||||
Returns a `Logger` that has `.log`, `.error` and `.warn` methods. Loggins is done with a consistent prefix (type of logger, date) followed by the actual message uning the underlying console.
|
||||
@example
|
||||
const l = require('@polkadot/util/logger')('test');
|
||||
|
||||
l.log('blah'); // <date> TEST: blah
|
||||
*/
|
||||
module.exports = function logger (_type: string): Logger {
|
||||
const type = ` ${_type.toUpperCase()}:`.slice(-11);
|
||||
|
||||
return {
|
||||
format: (...values: any): Array<any> => format(type, values),
|
||||
error: (...values: any): void => error(type, values),
|
||||
log: (...values: any): void => log(type, values),
|
||||
warn: (...values: any): void => warn(type, values)
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* global jest */
|
||||
|
||||
const logger = require('./logger');
|
||||
|
||||
describe('logger', () => {
|
||||
let l;
|
||||
let spy;
|
||||
|
||||
beforeEach(() => {
|
||||
l = logger('test');
|
||||
|
||||
spy = {
|
||||
error: jest.fn(),
|
||||
log: jest.fn(),
|
||||
warn: jest.fn()
|
||||
};
|
||||
global.console = spy;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('format', () => {
|
||||
let formatted;
|
||||
|
||||
beforeEach(() => {
|
||||
formatted = l.format('paramA', 'paramB');
|
||||
});
|
||||
|
||||
it('adds the date, type & params', () => {
|
||||
expect(formatted.length).toBe(4);
|
||||
});
|
||||
});
|
||||
|
||||
it('logs to console.error with .error', () => {
|
||||
l.error('console.error test');
|
||||
|
||||
expect(spy.error).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('logs to console.log with .log', () => {
|
||||
l.log('console.log test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('logs to console.warn with .warn', () => {
|
||||
l.warn('console.warn test');
|
||||
|
||||
expect(spy.warn).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -54,9 +54,9 @@
|
||||
lodash "^4.2.0"
|
||||
to-fast-properties "^2.0.0"
|
||||
|
||||
"@polkadot/dev@^0.3.1":
|
||||
version "0.3.1"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.3.1.tgz#9cb3bd91df1355ab0dba0a277d2f853c0959a592"
|
||||
"@polkadot/dev@^0.3.2":
|
||||
version "0.3.2"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.3.2.tgz#54b2b70a412f33b2dee39f8bb68121f9e85aa970"
|
||||
dependencies:
|
||||
babel-cli "^6.26.0"
|
||||
babel-core "^6.26.0"
|
||||
|
||||
Reference in New Issue
Block a user