so colours
much lightweight
such better
wow
~ Eek 2016
npm i --save hapi-console
const Hapi = require('hapi');
const server = new Hapi.Server({ debug: false });
const hapiConsole = require('hapi-console');
server.register({
register: hapiConsole,
options: {
// ignore /ignore/me route
ignore: ['/ignore/me'],
// show custom data about the request
custom: {
// shows custom data about auth.credentials.uid as 'uid: <>'
'auth.credentials.uid': true,
// shows a JSON.stringify version of query
'query': true,
// executes the function and uses the output where x is request.headers
'headers': (x) => JSON.stringify(x).replace(/"/g, '')
},
// if true it uses the full path of the key to display the data
// if false it uses just the last property key to display data
customFullLengthKey: false
}
}, () => {
if (err) {
console.error(err);
}
server.route({
path: '/',
method: 'get',
handler: (req, res) => {
// will show an error
req.log('error', 'err');
// will also show an error with stack
req.log('error', new Error());
// will log on request
req.log('log', 'log 1');
// will log on server
server.log('log', 'this is a server log'); // we
// will show an unexpected error
setTimeout(() => {throw new Error('up')}, 100);
}
});
server.start();
});
Note: userFilter
has been deprecated in favour of custom
since 0.6.0
Will not write to console output from the route's context but will log errors
Will not write to console output any syscall errors
Will filter what data from request.auth.credentials
is shown in the output
Example:
SERVER test/2 STARTED
ID: Stefan:26747:itfodt15
PORT: 8081
HOST: Stefan
PROTOCOL: http
URI: http://Stefan:8081
Explanation:
SERVER {connection assigned labels split by /} STARTED
ID: {connection id}
PORT: {port}
HOST: {host}
PROTOCOL: {protocol}
URI: {full uri}
server.log(['log', 'test'], 'this is a server log');
Example:
1474629596922:Stefan:26747:itfodt0w | [log/test] this is a server log
Explanation:
{ timestamp }:{host}:{pid}:{ts base64} | [{tags}] {message}
req.log(['log', 'test'], 'request log');
Example:
1474629596915:Stefan:26747:itfodt0w:10000:8080 [test|1] 127.0.0.1 [{uid: 12345}] [log/test] log 1
Explanation:
{ timestamp }:{host}:{pid}:{ts base64}:{counter}:{port} [{connection labels}] {ip} [{user data}] [{tags}] {message}
req.log('error', new Error('¯\\_(ツ)_/¯')
Example:
1474629596915:Stefan:26747:itfodt0w:10000:8080 [test|1] 127.0.0.1 [null] [ERROR] Error ¯\_(ツ)_/¯ \n {stack}
Explanation:
{ timestamp }:{host}:{pid}:{ts base64}:{counter}:{port} [{connection labels}] {ip} [{user data}] [ERROR] {error stack trace}
res({success: true}}
Example:
1474629596915:Stefan:26747:itfodt0w:10000:8080 [test|1] 127.0.0.1 [{uid: 1234}] 200 GET:/ 0.74[0.02~0+0.28~0.42]
Explanation:
{ timestamp }:{host}:{pid}:{ts base64}:{counter}:{port} [{connection labels}] {ip} [{user data}] {status code} {method}:{path} {total time ms}[{traffic in time + hapi head}~{auth time ms}+{handler time ms}~{traffic out time + hapi tail}]
Info: if the times don't add up that's because there's also time spent inside the `HAPI` server code
throw 'up'
Example:
1474629596915:Stefan:26747:itfodt0w:10000:8080 [test|1] 127.0.0.1 [null] [ERROR] Error: Uncaught error: up
Explanation:
{ timestamp }:{host}:{pid}:{ts base64}:{counter}:{port} [{connection labels}] {ip} [{user data}] [ERROR] {error stack trace}
Colors will enable automatically
Use --no-color
or --color=false
Use --color
, --color=true
or --color=always
Please see license.md