Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

chore: updates docs and example to use transports #49

Merged
merged 31 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d30934c
Move PINO logging out of the main execution thread
aaestrada Dec 16, 2021
add0fa3
Run profile test passing the pino instance at hapi-pino in the regist…
aaestrada Dec 16, 2021
3576cd1
Register hapi-pino logs from the manifest.json file
aaestrada Jan 3, 2022
ef9c49c
Remove unused variables
aaestrada Jan 4, 2022
844017e
Set up legacy transport using pino-http-print
aaestrada Jan 11, 2022
df89f2a
Set Up legacy transport with pino-pretty library
aaestrada Jan 12, 2022
ad789e6
clean package json file
aaestrada Jan 12, 2022
fd64100
del duplicate command
aaestrada Jan 12, 2022
54b5de0
explain pino logs config
aaestrada Jan 12, 2022
012c3ad
Update readme.md
aaestrada Jan 12, 2022
d991278
update docs
aaestrada Jan 12, 2022
8039f77
chore: updates docs and example to use transports for pino-pretty
aaestrada Jan 14, 2022
124ff83
Update docs with better redaction
aaestrada Jan 17, 2022
403c795
logs to the transport over custom pipeline
aaestrada Jan 19, 2022
492d9a1
Merge branch 'master' into CAT-1135
aaestrada Jan 19, 2022
5681f6d
Add support to transport logs with custom cli options
aaestrada Jan 20, 2022
f544e00
Merge remote-tracking branch 'origin/CAT-1135' into CAT-1135
aaestrada Jan 20, 2022
e2c2e6b
update code example
aaestrada Jan 20, 2022
eeca56c
update readme file
aaestrada Jan 21, 2022
a038b61
Update examples/custom-pipeline/my-transport-process.js
aaestrada Jan 25, 2022
d3bc615
Update examples/custom-pipeline/my-transport-process.js
aaestrada Jan 25, 2022
dfad3f5
upgrade hapi-pino to read ECONRESET error
aaestrada Jan 26, 2022
ca0037d
upgrade hapi-pino to fix read ECONRESET error
aaestrada Jan 26, 2022
4e099b1
Update examples/simple/readme.md
aaestrada Feb 2, 2022
bde025f
update docs and default config for logs
aaestrada Feb 2, 2022
9574050
update docs and default config for logs
aaestrada Feb 2, 2022
21dcf2f
updte docs
aaestrada Feb 2, 2022
c327c20
update path
aaestrada Feb 2, 2022
28da9c8
clean package json file
aaestrada Feb 2, 2022
7e17e62
bring back pino-pretty for dev
aaestrada Feb 2, 2022
78c4636
update config file
aaestrada Feb 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/custom-pipeline/index.js
@@ -0,0 +1,43 @@
/**
* After running this example code with node,
* you can see the results in browser at:
* http://localhost:8080/items
*/

const Catalyst = require('../..');
const Path = require('path');
const getResponse = [
{string: 'string1', number: 1, boolean: true},
{string: 'string2', number: 2, boolean: false},
];

const hapiPino = require('hapi-pino');
const pino = require('pino');

async function start (options = {}) {
const server = await Catalyst.init({
...options,
userConfigPath: Path.resolve(__dirname, 'manifest.json'),
});

server.route({
path: '/items',
method: 'GET',
options: {
log: { collect: true },
cache: { expiresIn: 5000 },
},
handler: async function (req, h) {
// you can also use a pino instance, which will be faster
req.logger.info('GET_items', getResponse);
return await h.response(getResponse);
},
});

await server.start()
server.log(['info'], `items endpoint : ${server.info.uri}/items`)
return server
}

start()

15 changes: 15 additions & 0 deletions examples/custom-pipeline/manifest.json
@@ -0,0 +1,15 @@
{
"server": {
"debug": false // disable Hapi debug console logging
},
"register": {
"hapi-pino": {
"plugin": "require:hapi-pino",
"options":{
"logRequestComplete": false,
"ignoredEventTags": { "log": ["client"], "request": "*" },
"logPayload": false
}
}
}
}
37 changes: 37 additions & 0 deletions examples/custom-pipeline/my-transport-process.js
@@ -0,0 +1,37 @@
'use strict';
aaestrada marked this conversation as resolved.
Show resolved Hide resolved
const split = require('split2')
const pump = require('pump')
const through = require('through2')
const args = require('args')
const sonic = require('sonic-boom')

/**
* Set up more options passing at the node cli
* node . | node my-transport-process.js --d="logs/log.log"
*/
args
.option(['d','destination'], 'The port on which the app will be running')

const flagOptions = args.parse(process.argv);

let destination = flagOptions.destination;

if (!destination) throw new Error('destination is required');

const myTransport = through.obj(async function (chunk, enc, cb) {
if(destination === 1 ){
aaestrada marked this conversation as resolved.
Show resolved Hide resolved
console.log(chunk)
} else{
aaestrada marked this conversation as resolved.
Show resolved Hide resolved
let WriteTodestination = sonic({
dest: destination || 1,
append: true,
mkdir: true,
sync: true // by default sonic will be async
})

WriteTodestination.write(JSON.stringify(chunk) + '\n')
}
cb()
})

return pump(process.stdin, split(JSON.parse), myTransport)
57 changes: 57 additions & 0 deletions examples/custom-pipeline/readme.md
@@ -0,0 +1,57 @@
# Catalyst server log configuration

## Pipe logs to the transport over custom pipeline input for production

Example below will take logs from the `process.stdin` and pipe to either `stdout`

my-transport-process.js
```
'use strict';
const split = require('split2')
const pump = require('pump')
const through = require('through2')
const args = require('args')
const sonic = require('sonic-boom')

/**
* Set up more options passing at the node cli
* node . | node my-transport-process.js --d="logs/log.log"
*/
args
.option(['d','destination'], 'The final destination for the logs')

const flagOptions = args.parse(process.argv);

let destination = flagOptions.destination;

if (!destination) throw new Error('destination is required');

const myTransport = through.obj(async function (chunk, enc, cb) {
if(destination === 1 ){
console.log(chunk)
} else{
let WriteTodestination = sonic({
dest: destination || 1,
append: true,
mkdir: true,
sync: true // by default sonic will be async
})

WriteTodestination.write(JSON.stringify(chunk) + '\n')
}
cb()
})

return pump(process.stdin, split(JSON.parse), myTransport)
```

Logs can now be consumed using shell piping:

```
node --max-http-header-size=32768 ./examples/custom-pipeline/index.js | node ./examples/custom-pipeline/my-transport-process.js -d=1
```
Or logs can be transport to a file:

```
node --max-http-header-size=32768 ./examples/custom-pipeline/index.js | node ./examples/custom-pipeline/my-transport-process.js -d="logs/log.log"
```
5 changes: 5 additions & 0 deletions examples/simple/.pino-prettyrc
@@ -0,0 +1,5 @@
{
"colorize": true,
"singleLine": true
// ... pass pino-pretty options here
}
27 changes: 6 additions & 21 deletions examples/simple/manifest.json
@@ -1,30 +1,15 @@
{
"server": {
"debug": false // disable Hapi debug console logging
},
"register": {
"hapi-pino": {
"plugin": "require:hapi-pino",
"options": {
"mergeHapiLogData": false,
// "level": "debug",
"ignorePaths": [
"/health",
"/alive.txt",
"/private"
],
"tags": { "GET_items": "info", "GET_error": "error" },
// transport option is not recomended for prod env
"transport": {
"target": "pino-pretty",
"options": {
"singleLine": true,
"colorize": true,
"mkdir": true,
"append": false,
"destination": "logs/output.log"
}
}
}
"options":{
"logRequestComplete": false,
"ignoredEventTags": { "log": ["client"], "request": "*" },
"logPayload": false
}
aaestrada marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
65 changes: 65 additions & 0 deletions examples/simple/readme.md
@@ -0,0 +1,65 @@
# Catalyst server log configuration

The follow diagram explains how our catalyst server manages logs

![threads-diagram drawio](https://user-images.githubusercontent.com/88118994/149195822-de5d33ad-f29f-48ff-840b-ce2fb41eb08a.png)

## [Pino-pretty](https://github.com/pinojs/pino-pretty) logs for development mode:

install pino-pretty as a development dependency.

` npm install --save-dev pino-pretty `

manifest.json
```
"hapi-pino": {
"plugin": "require:hapi-pino",
"options": {
"$filter": "env.NODE_ENV",
"$default": {},
"development": {
"transport": {
"target": "pino-pretty",
"options": {
"colorize": true,
"translateTime": true
}
}
}
}
}
```

## [Pino-pretty](https://github.com/pinojs/pino/blob/master/docs/pretty.md) logs for production mode(legacy transport):

Install Pino-pretty

`npm install pino-pretty`

It is recommended to use pino-pretty with pino by piping output to the CLI tool:

```
node --max-http-header-size=32768 ./examples/simple/index.js | ./node_modules/.bin/pino-pretty --config=./examples/simple/.pino-prettyrc
```

.pino-prettyrc
```
{
"colorize": true,
"singleLine": true
// ... pass pino-pretty options here
}
```

.pino-pretty pass the [CLI Arguments](https://github.com/pinojs/pino-pretty#cli-arguments)



_node js profile review_
aaestrada marked this conversation as resolved.
Show resolved Hide resolved

```
clinic doctor --autocannon [ /items ] -- node --max-http-header-size=32768 ./examples/simple/index.js | ./node_modules/.bin/pino-pretty --config=./examples/simple/.pino-prettyrc

```


Binary file added examples/simple/threads-diagram.drawio.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -65,10 +65,11 @@
"@hapi/crumb": "^8.0.0",
"@hapi/hoek": "^9.0.4",
"@vrbo/steerage": "^12.1.3",
"hapi-pino": "^9.0.0",
"hapi-pino": "^9.1.0",
"joi": "^17.2.0",
"pino-pretty": "^7.2.0",
"shortstop-handlers": "^1.0.1"
"shortstop-handlers": "^1.0.1",
"through2": "^4.0.2"
},
"devDependencies": {
"@hapi/hapi": "^20.2.1",
Expand Down