Skip to content

Commit f49a87b

Browse files
committed
feat: improve logging with pino
1 parent 8b90c98 commit f49a87b

17 files changed

+594
-55
lines changed

package-lock.json

Lines changed: 534 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"dev": "NODE_ENV=development npm-run-all build server",
1818
"dev:watch": "nodemon",
1919
"pretty": "prettier --write \"{src,test}/**/*.ts\"",
20-
"server": "node ./dist/start.js",
20+
"server": "node ./dist/start.js | pino-pretty --colorize --levelFirst",
2121
"test": "node -r esm ./node_modules/.bin/mocha 'test/**/*.js' --recursive "
2222
},
2323
"dependencies": {
@@ -26,6 +26,7 @@
2626
"express": "^4.17.1",
2727
"pg": "^7.0.0",
2828
"pg-format": "^1.0.4",
29+
"pino": "^6.8.0",
2930
"sql-template-strings": "^2.2.2"
3031
},
3132
"devDependencies": {
@@ -40,6 +41,7 @@
4041
"mocha": "^7.1.2",
4142
"nodemon": "^1.19.4",
4243
"npm-run-all": "^4.1.5",
44+
"pino-pretty": "^4.3.0",
4345
"pkg": "^4.4.8",
4446
"prettier": "^2.0.5",
4547
"rimraf": "^3.0.2",

src/api/columns.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RunQuery } from '../lib/connectionPool'
55
import sql = require('../lib/sql')
66
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
77
import { Tables } from '../lib/interfaces'
8+
import { logger } from '../lib/logger'
89

910
/**
1011
* @param {boolean} [include_system_schemas=false] - Return system schemas as well as user schemas
@@ -25,7 +26,7 @@ router.get('/', async (req, res) => {
2526
if (!include_system_schemas) payload = removeSystemSchemas(data)
2627
return res.status(200).json(payload)
2728
} catch (error) {
28-
console.log('throwing error', error)
29+
logger.error({ error, req: req.body })
2930
res.status(500).json({ error: error.message })
3031
}
3132
})
@@ -49,7 +50,7 @@ router.post('/', async (req, res) => {
4950

5051
return res.status(200).json(column)
5152
} catch (error) {
52-
console.log('throwing error', error)
53+
logger.error({ error, req: req.body })
5354
res.status(400).json({ error: error.message })
5455
}
5556
})
@@ -67,7 +68,7 @@ router.patch('/:id', async (req, res) => {
6768
const updated = (await RunQuery(req.headers.pg, getColumnQuery)).data[0]
6869
return res.status(200).json(updated)
6970
} catch (error) {
70-
console.log('throwing error', error)
71+
logger.error({ error, req: req.body })
7172
if (error instanceof TypeError) {
7273
res.status(404).json({ error: 'Cannot find a column with that id' })
7374
} else {
@@ -88,7 +89,7 @@ router.delete('/:id', async (req, res) => {
8889

8990
return res.status(200).json(column)
9091
} catch (error) {
91-
console.log('throwing error', error)
92+
logger.error({ error, req: req.body })
9293
if (error instanceof TypeError) {
9394
res.status(404).json({ error: 'Cannot find a column with that id' })
9495
} else {

src/api/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Router } from 'express'
22

33
import { RunQuery } from '../lib/connectionPool'
44
import sql = require('../lib/sql')
5+
import { logger } from '../lib/logger'
56
const { config, version } = sql
67

78
const router = Router()
@@ -10,7 +11,7 @@ router.get('/', async (req, res) => {
1011
const { data } = await RunQuery(req.headers.pg, config)
1112
return res.status(200).json(data)
1213
} catch (error) {
13-
console.log('throwing error', error)
14+
logger.error({ error, req: req.body })
1415
res.status(500).json({ error: error.message })
1516
}
1617
})
@@ -19,7 +20,7 @@ router.get('/version', async (req, res) => {
1920
const { data } = await RunQuery(req.headers.pg, version)
2021
return res.status(200).json(data[0]) // only one row
2122
} catch (error) {
22-
console.log('throwing error', error)
23+
logger.error({ error, req: req.body })
2324
res.status(500).json({ error: error.message })
2425
}
2526
})

src/api/extensions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import SQL from 'sql-template-strings'
44
import sqlTemplates = require('../lib/sql')
55
const { extensions } = sqlTemplates
66
import { RunQuery } from '../lib/connectionPool'
7+
import { logger } from '../lib/logger'
78

89
const router = Router()
910

@@ -13,7 +14,7 @@ router.get('/', async (req, res) => {
1314
const { data } = await RunQuery(req.headers.pg, getExtensionsQuery)
1415
return res.status(200).json(data)
1516
} catch (error) {
16-
console.log('throwing error', error)
17+
logger.error({ error, req: req.body })
1718
res.status(500).json({ error: error.message })
1819
}
1920
})
@@ -28,7 +29,7 @@ router.post('/', async (req, res) => {
2829

2930
return res.status(200).json(extension)
3031
} catch (error) {
31-
console.log('throwing error', error)
32+
logger.error({ error, req: req.body })
3233
res.status(400).json({ error: error.message })
3334
}
3435
})
@@ -46,7 +47,7 @@ router.patch('/:name', async (req, res) => {
4647

4748
return res.status(200).json(updated)
4849
} catch (error) {
49-
console.log('throwing error', error)
50+
logger.error({ error, req: req.body })
5051
res.status(400).json({ error: error.message })
5152
}
5253
})
@@ -64,7 +65,7 @@ router.delete('/:name', async (req, res) => {
6465

6566
return res.status(200).json(deleted)
6667
} catch (error) {
67-
console.log('throwing error', error)
68+
logger.error({ error, req: req.body })
6869
res.status(400).json({ error: error.message })
6970
}
7071
})

src/api/functions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { functions } = sql
55
import { RunQuery } from '../lib/connectionPool'
66
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
77
import { Functions } from '../lib/interfaces'
8+
import { logger } from '../lib/logger'
89

910
/**
1011
* @param {boolean} [include_system_schemas=false] - Return system schemas as well as user schemas
@@ -23,7 +24,7 @@ router.get('/', async (req, res) => {
2324
if (!include_system_schemas) payload = removeSystemSchemas(data)
2425
return res.status(200).json(payload)
2526
} catch (error) {
26-
console.log('throwing error', error)
27+
logger.error({ error, req: req.body })
2728
res.status(500).json({ error: error.message })
2829
}
2930
})

src/api/policies.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RunQuery } from '../lib/connectionPool'
55
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
66
import { Tables } from '../lib/interfaces'
77
import sqlTemplates = require('../lib/sql')
8+
import { logger } from '../lib/logger'
89

910
/**
1011
* @param {string} [include_system_schemas=false] - Return system schemas as well as user schemas
@@ -25,7 +26,7 @@ router.get('/', async (req, res) => {
2526
if (!include_system_schemas) payload = removeSystemSchemas(data)
2627
return res.status(200).json(payload)
2728
} catch (error) {
28-
console.log('throwing error', error)
29+
logger.error({ error, req: req.body })
2930
res.status(500).json({ error: error.message })
3031
}
3132
})
@@ -46,8 +47,7 @@ router.post('/', async (req, res) => {
4647
const newPolicy = await getPolicyByName(pcConnection, name, schema, table)
4748
return res.status(200).json(newPolicy)
4849
} catch (error) {
49-
// For this one, we always want to give back the error to the customer
50-
console.log('POST error!', error)
50+
logger.error({ error, req: req.body })
5151
res.status(400).json({ error: error.message })
5252
}
5353
})
@@ -83,8 +83,7 @@ router.patch('/:id', async (req, res) => {
8383
const updated = await getPolicyById(pcConnection, id)
8484
return res.status(200).json(updated)
8585
} catch (error) {
86-
// For this one, we always want to give back the error to the customer
87-
console.log('Soft error!', error)
86+
logger.error({ error, req: req.body })
8887
res.status(400).json({ error: error.message })
8988
}
9089
})
@@ -105,7 +104,7 @@ router.delete('/:id', async (req, res) => {
105104

106105
return res.status(200).json(policy)
107106
} catch (error) {
108-
console.log('throwing error', error)
107+
logger.error({ error, req: req.body })
109108
res.status(400).json({ error: error.message })
110109
}
111110
})

src/api/publications.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Router } from 'express'
22
import { ident, literal } from 'pg-format'
33
import sql = require('../lib/sql')
44
import { RunQuery } from '../lib/connectionPool'
5+
import { logger } from '../lib/logger'
56

67
const { publications } = sql
78
const router = Router()
@@ -11,6 +12,7 @@ router.get('/', async (req, res) => {
1112
const { data } = await RunQuery(req.headers.pg, publications)
1213
return res.status(200).json(data)
1314
} catch (error) {
15+
logger.error({ error, req: req.body })
1416
res.status(500).json({ error: error.message })
1517
}
1618
})
@@ -24,6 +26,7 @@ router.post('/', async (req, res) => {
2426
const newPublication = (await RunQuery(req.headers.pg, getPublicationSql)).data[0]
2527
return res.status(200).json(newPublication)
2628
} catch (error) {
29+
logger.error({ error, req: req.body })
2730
res.status(400).json({ error: error.message })
2831
}
2932
})
@@ -41,7 +44,7 @@ router.patch('/:id', async (req, res) => {
4144
const updatedPublication = (await RunQuery(req.headers.pg, getPublicationSql)).data[0]
4245
return res.status(200).json(updatedPublication)
4346
} catch (error) {
44-
console.log(error.message)
47+
logger.error({ error, req: req.body })
4548
if (error instanceof TypeError) {
4649
res.status(404).json({ error: 'Cannot find a publication with that id' })
4750
} else {
@@ -62,6 +65,7 @@ router.delete('/:id', async (req, res) => {
6265

6366
return res.status(200).json(publication)
6467
} catch (error) {
68+
logger.error({ error, req: req.body })
6569
if (error instanceof TypeError) {
6670
res.status(404).json({ error: 'Cannot find a publication with that id' })
6771
} else {

src/api/query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Router } from 'express'
22

33
import { RunQuery } from '../lib/connectionPool'
4+
import { logger } from '../lib/logger'
45

56
const router = Router()
67
router.post('/', async (req, res) => {
@@ -9,8 +10,7 @@ router.post('/', async (req, res) => {
910
const { data } = await RunQuery(req.headers.pg, query)
1011
return res.status(200).json(data || [])
1112
} catch (error) {
12-
// For this one, we always want to give back the error to the customer
13-
console.log('Soft error!', error)
13+
logger.error({ error, req: req.body })
1414
res.status(400).json({ error: error.message })
1515
}
1616
})

src/api/roles.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { coalesceRowsToArray } from '../lib/helpers'
88
import { RunQuery } from '../lib/connectionPool'
99
import { DEFAULT_ROLES, DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
1010
import { Roles } from '../lib/interfaces'
11+
import { logger } from '../lib/logger'
1112

1213
/**
1314
* @param {boolean} [include_system_schemas=false] - Return system schemas as well as user schemas
@@ -32,7 +33,7 @@ router.get('/', async (req, res) => {
3233

3334
return res.status(200).json(payload)
3435
} catch (error) {
35-
console.log('throwing error', error)
36+
logger.error({ error, req: req.body })
3637
res.status(500).json({ error: error.message })
3738
}
3839
})
@@ -47,7 +48,7 @@ router.post('/', async (req, res) => {
4748

4849
return res.status(200).json(role)
4950
} catch (error) {
50-
console.log('throwing error', error)
51+
logger.error({ error, req: req.body })
5152
res.status(400).json({ error: error.message })
5253
}
5354
})
@@ -67,7 +68,7 @@ router.patch('/:id', async (req, res) => {
6768
const updated = (await RunQuery(req.headers.pg, getRoleQuery)).data[0]
6869
return res.status(200).json(updated)
6970
} catch (error) {
70-
console.log('throwing error', error)
71+
logger.error({ error, req: req.body })
7172
res.status(400).json({ error: error.message })
7273
}
7374
})
@@ -84,7 +85,7 @@ router.delete('/:id', async (req, res) => {
8485

8586
return res.status(200).json(role)
8687
} catch (error) {
87-
console.log('throwing error', error)
88+
logger.error({ error, req: req.body })
8889
res.status(400).json({ error: error.message })
8990
}
9091
})

src/api/schemas.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import sqlTemplates = require('../lib/sql')
55
import { RunQuery } from '../lib/connectionPool'
66
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
77
import { Schemas } from '../lib/interfaces'
8+
import { logger } from '../lib/logger'
89

910
const { schemas } = sqlTemplates
1011

@@ -27,7 +28,7 @@ router.get('/', async (req, res) => {
2728

2829
return res.status(200).json(payload)
2930
} catch (error) {
30-
console.log('throwing error', error)
31+
logger.error({ error, req: req.body })
3132
res.status(500).json({ error: error.message })
3233
}
3334
})
@@ -47,7 +48,7 @@ router.post('/', async (req, res) => {
4748
let schema: Schemas.Schema = data[0]
4849
return res.status(200).json(schema)
4950
} catch (error) {
50-
console.log('throwing error', error)
51+
logger.error({ error, req: req.body })
5152
res.status(400).json({ error: error.message })
5253
}
5354
})
@@ -79,7 +80,7 @@ router.patch('/:id', async (req, res) => {
7980
let updated: Schemas.Schema = updatedResults[0]
8081
return res.status(200).json(updated)
8182
} catch (error) {
82-
console.log('throwing error', error)
83+
logger.error({ error, req: req.body })
8384
res.status(400).json({ error: error.message })
8485
}
8586
})
@@ -96,7 +97,7 @@ router.delete('/:id', async (req, res) => {
9697

9798
return res.status(200).json(schema)
9899
} catch (error) {
99-
console.log('throwing error', error)
100+
logger.error({ error, req: req.body })
100101
res.status(400).json({ error: error.message })
101102
}
102103
})

0 commit comments

Comments
 (0)