@defra/bng-utils-lib 0.273.10 → 0.273.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.jest/test.env.js CHANGED
@@ -8,3 +8,4 @@ process.env.POSTGRES_PASSWORD='postgres'
8
8
  process.env.POSTGRES_DATABASE='bng'
9
9
  process.env.POSTGRES_PORT='5432'
10
10
  process.env.POSTGRES_SSL_MODE=''
11
+ process.env.NODE_ENV='test'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/bng-utils-lib",
3
- "version": "0.273.10",
3
+ "version": "0.273.13",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "description": "A library of utilities for the Biodiversity Net Gain Service",
@@ -14,9 +14,10 @@
14
14
  "license": "OGL-UK-3.0",
15
15
  "dependencies": {
16
16
  "@azure/identity": "^4.0.0",
17
- "@defra/bng-connectors-lib": "^0.273.10",
17
+ "@defra/bng-connectors-lib": "^0.273.13",
18
18
  "axios": "^1.6.2",
19
+ "pino": "^8.17.2",
19
20
  "pino-pretty": "10.3.1"
20
21
  },
21
- "gitHead": "492925c3ce0091426d955d3fb64901cb6364fb66"
22
+ "gitHead": "cc76fbc0722f4e33164d1a2306c246fe3b0d64cf"
22
23
  }
@@ -1,13 +1,55 @@
1
- import logger from '../logger.js'
2
-
3
- // Adapted from https://stackoverflow.com/questions/44890836/how-to-test-console-log-output-using-jest-or-other-javascript-testing-framework
4
1
  describe('The logger', () => {
5
- it('should log output', async () => {
6
- const mockLogText = 'mock log text'
7
- const loggedData = []
8
- const storeLogOutput = input => loggedData.push(input)
9
- logger.info = jest.fn(storeLogOutput)
10
- logger.info(mockLogText)
11
- expect(loggedData).toContain(mockLogText)
2
+ const timeout = 1000
3
+ it('should be configured to log at warning level and above by default', done => {
4
+ jest.isolateModules(() => {
5
+ try {
6
+ const logger = require('../logger.js')
7
+ // Finishing the test in setImmediate is not long enough to conclude async operations
8
+ setTimeout(() => {
9
+ expect(logger.default.level).toEqual('warn')
10
+ done()
11
+ }, timeout)
12
+ } catch (err) {
13
+ setTimeout(() => {
14
+ done(err)
15
+ }, timeout)
16
+ }
17
+ })
18
+ })
19
+ it('should log at a configured level and above', done => {
20
+ jest.isolateModules(() => {
21
+ try {
22
+ process.env.LOG_LEVEL = 'info'
23
+ const logger = require('../logger.js')
24
+ // Finishing the test in setImmediate is not long enough to conclude async operations
25
+ setTimeout(async () => {
26
+ expect(logger.default.level).toEqual('info')
27
+ done()
28
+ }, timeout)
29
+ } catch (err) {
30
+ setTimeout(() => {
31
+ done(err)
32
+ }, timeout)
33
+ }
34
+ })
35
+ })
36
+ it('should log at warning level when an invalid log level is configured', done => {
37
+ jest.isolateModules(() => {
38
+ try {
39
+ process.env.LOG_LEVEL = 'invalid'
40
+ // Change the NODE_ENV to confiure use of pino-pretty. This increases test coverage.
41
+ process.env.NODE_ENV = 'development'
42
+ const logger = require('../logger.js')
43
+ // Finishing the test in setImmediate is not long enough to conclude async operations
44
+ setTimeout(async () => {
45
+ expect(logger.default.level).toEqual('warn')
46
+ done()
47
+ }, timeout)
48
+ } catch (err) {
49
+ setTimeout(() => {
50
+ done(err)
51
+ }, timeout)
52
+ }
53
+ })
12
54
  })
13
55
  })
@@ -1,6 +1,40 @@
1
1
  import pino from 'pino'
2
2
 
3
- import loggerOptions from './logger-options.js'
3
+ const debug = 'debug'
4
+ const error = 'error'
5
+ const fatal = 'fatal'
6
+ const info = 'info'
7
+ const silent = 'silent'
8
+ const trace = 'trace'
9
+ const warn = 'warn'
10
+
11
+ const logLevels = {
12
+ trace,
13
+ debug,
14
+ info,
15
+ warn,
16
+ error,
17
+ fatal,
18
+ silent
19
+ }
20
+
21
+ const logLevel = (process.env.LOG_LEVEL || logLevels.warn).toLowerCase()
22
+ const loggerOptions = {
23
+ level: logLevels[logLevel] || logLevels.warn,
24
+ redact: {
25
+ paths: ['req.headers.authorization', 'req.headers.cookie', 'res.headers'],
26
+ remove: true
27
+ }
28
+ }
29
+
30
+ if (process.env.NODE_ENV === 'development') {
31
+ loggerOptions.transport = {
32
+ target: 'pino-pretty',
33
+ options: {
34
+ colorize: true
35
+ }
36
+ }
37
+ }
4
38
 
5
39
  const logger = pino(loggerOptions)
6
40
 
package/src/utils.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import getBearerToken from './helpers/get-bearer-token.js'
2
2
  import getDBConnection from './helpers/get-db-connection.js'
3
3
  import logger from './helpers/logger.js'
4
- import loggerOptions from './helpers/logger-options.js'
5
4
 
6
- export { getBearerToken, getDBConnection, logger, loggerOptions }
5
+ export { getBearerToken, getDBConnection, logger }
@@ -1,21 +0,0 @@
1
- const loggerOptions = {
2
- options: {
3
- logPayload: true,
4
- level: 'warn',
5
- redact: {
6
- paths: ['req.headers.authorization', 'req.headers.cookie', 'res.headers'],
7
- remove: true
8
- }
9
- }
10
- }
11
-
12
- if (process.env.NODE_ENV === 'development') {
13
- loggerOptions.options.transport = {
14
- target: 'pino-pretty',
15
- options: {
16
- colorize: true
17
- }
18
- }
19
- }
20
-
21
- export default loggerOptions