Skip to content

Commit 675225b

Browse files
committed
feat: support ecmascript syntax
1 parent 5b43ec8 commit 675225b

File tree

10 files changed

+199
-160
lines changed

10 files changed

+199
-160
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ web_modules/
4848

4949
# TypeScript cache
5050
*.tsbuildinfo
51+
.tsimp/
5152

5253
# Optional npm cache directory
5354
.npm

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ It will pretty-print logs when run locally, but it will write logs in JSON when
99
## Install
1010

1111
```
12-
npm i @byu-oit/logger
12+
npm i @byu-oit/logger
1313
```
1414

1515
## Usage

package.json

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "@byu-oit/logger",
33
"version": "0.4.2",
44
"description": "Default configuration for pino logger",
5+
"engines": {
6+
"node": ">=18"
7+
},
58
"contributors": [
69
{
710
"name": "Scott Hutching",
@@ -12,56 +15,79 @@
1215
"email": "[email protected]"
1316
}
1417
],
15-
"main": "dist/logger.js",
16-
"types": "dist/logger.d.ts",
18+
"type": "module",
19+
"main": "./dist/cjs/index.js",
20+
"types": "./dist/cjs/index.d.ts",
21+
"exports": {
22+
"./package.json": "./package.json",
23+
".": {
24+
"import": {
25+
"types": "./dist/esm/index.d.ts",
26+
"default": "./dist/esm/index.js"
27+
},
28+
"require": {
29+
"types": "./dist/cjs/index.d.ts",
30+
"default": "./dist/cjs/index.js"
31+
}
32+
}
33+
},
1734
"files": [
1835
"dist"
1936
],
2037
"scripts": {
21-
"build": "npx rimraf dist && tsc",
22-
"coverage": "npm run test -- --coverage || exit 0",
38+
"build": "rimraf dist && concurrently \"npm:build:*\" -c cyanBright",
39+
"build:cjs": "tsc -p tsconfig.cjs.json && echo-cli \"{\\\"type\\\": \\\"commonjs\\\"}\" > dist/cjs/package.json",
40+
"build:esm": "tsc -p tsconfig.json && echo-cli \"{\\\"type\\\": \\\"module\\\"}\" > dist/esm/package.json",
41+
"clean": "rimraf dist",
42+
"coverage": "c8 ava",
2343
"lint": "npx ts-standard | snazzy",
2444
"lint:fix": "npx ts-standard --fix | snazzy",
25-
"test": "jest",
45+
"test": "ava",
2646
"prepublishOnly": "npm run build"
2747
},
2848
"author": "Brigham Young University - Office of Information Technology",
2949
"license": "Apache-2.0",
50+
"repository": {
51+
"type": "git",
52+
"url": "git+https://github.com/byu-oit/logger.git"
53+
},
54+
"bugs": {
55+
"url": "https://github.com/byu-oit/logger/issues"
56+
},
3057
"dependencies": {
3158
"deepmerge": "^4.2.2",
3259
"pino": "^8.11.0",
3360
"pino-http": "^6.6.0"
3461
},
3562
"devDependencies": {
36-
"@tsconfig/node12": "^1.0.11",
63+
"@tsconfig/node18": "^18.2.4",
3764
"@types/jest": "^29.5.0",
38-
"@types/node": "^16.9.1",
39-
"jest": "^29.5.0",
65+
"@types/node": "^18.19.42",
66+
"@types/sinon": "^17.0.3",
67+
"ava": "^6.1.3",
68+
"c8": "^10.1.2",
69+
"concurrently": "^8.2.2",
70+
"echo-cli": "^2.0.0",
4071
"lint-staged": "^12.0.2",
4172
"pino-pretty": "^10.0.0",
73+
"rimraf": "^6.0.1",
74+
"sinon": "^18.0.0",
4275
"snazzy": "^9.0.0",
43-
"ts-jest": "^29.1.0",
76+
"tsimp": "^2.0.11",
4477
"typescript": "^5.0.3"
4578
},
4679
"optionalDependencies": {
4780
"pino-pretty": ">=7"
4881
},
49-
"engines": {
50-
"node": ">=12"
51-
},
52-
"jest": {
53-
"transform": {
54-
"^.+\\.(ts|tsx)$": "ts-jest"
55-
}
56-
},
5782
"lint-staged": {
5883
"*.ts": "npm run lint:fix"
5984
},
60-
"repository": {
61-
"type": "git",
62-
"url": "git+https://github.com/byu-oit/logger.git"
63-
},
64-
"bugs": {
65-
"url": "https://github.com/byu-oit/logger/issues"
85+
"ava": {
86+
"extensions": {
87+
"ts": "module"
88+
},
89+
"nodeArguments": [
90+
"--import=tsimp"
91+
]
6692
}
6793
}

src/logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Logger, LoggerOptions, pino } from 'pino'
2-
import { getLevel, isInstalled, isProduction } from './util'
31
import deepmerge from 'deepmerge'
2+
import { Logger, LoggerOptions, pino } from 'pino'
3+
import { getLevel, isInstalled, isProduction } from './util.js'
44

55
export function ByuLogger (options?: LoggerOptions): Logger {
66
const defaultOptions: LoggerOptions = {

test/logger.spec.ts

Lines changed: 0 additions & 130 deletions
This file was deleted.

test/production.spec.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import ava, { TestFn } from 'ava'
2+
import sinon, { SinonFakeTimers } from 'sinon'
3+
import { Logger } from 'pino'
4+
import { ByuLogger } from '../src/logger.js'
5+
6+
type Context = {
7+
logged: string
8+
logger: Logger
9+
clock: SinonFakeTimers
10+
now: Date
11+
}
12+
13+
const test = ava as TestFn<Context>
14+
15+
test.beforeEach((t) => {
16+
/* Capture the stdout pipe */
17+
process.stdout.write = (buffer: string) => {
18+
t.context.logged += buffer
19+
return true
20+
}
21+
22+
/* Stub the date time */
23+
const jan1st = new Date(2021, 0, 1)
24+
t.context.now = jan1st
25+
t.context.clock = sinon.useFakeTimers(jan1st.getTime())
26+
27+
process.env.NODE_ENV = 'production'
28+
t.context.logged = ''
29+
t.context.logger = ByuLogger()
30+
})
31+
32+
test.afterEach((t) => {
33+
t.context.clock.restore()
34+
})
35+
36+
test.serial('default logger should default to info level', (t) => {
37+
t.context.logger.debug('debug does not work')
38+
39+
t.is(t.context.logger.level, 'info')
40+
t.is(t.context.logged, '') // no logs should have happened
41+
})
42+
43+
test.serial('default logger displays logs in JSON format', (t) => {
44+
t.context.logger.info('json works')
45+
46+
try {
47+
const parsedLog = JSON.parse(t.context.logged)
48+
t.truthy(parsedLog.message)
49+
t.truthy(parsedLog.level)
50+
t.truthy(parsedLog.time)
51+
} catch (e) {
52+
t.log(e)
53+
t.fail(`The log format should be stringified JSON but parsing failed. See the logged error for details.`)
54+
}
55+
})
56+
57+
test.serial('default logger should display info logs', (t) => {
58+
t.context.logger.info('info works')
59+
60+
try {
61+
const parsedLog = JSON.parse(t.context.logged)
62+
t.is(parsedLog.message, 'info works')
63+
t.is(parsedLog.level, 'info')
64+
} catch (e) {
65+
t.log(e)
66+
t.fail(`The log format should be stringified JSON but parsing failed. See the logged error for details.`)
67+
}
68+
})
69+
70+
test.serial('default logger displays logs with epoch datetime format', (t) => {
71+
t.context.logger.info('iso date works')
72+
73+
try {
74+
const parsedLog = JSON.parse(t.context.logged)
75+
t.is(parsedLog.time, t.context.now.getTime())
76+
} catch (e) {
77+
t.log(e)
78+
t.fail(`The log format should be stringified JSON but parsing failed. See the logged error for details.`)
79+
}
80+
})

test/test.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import ava, { TestFn } from 'ava'
2+
import sinon, { SinonFakeTimers } from 'sinon'
3+
import { Logger } from 'pino'
4+
import { ByuLogger } from '../src/logger.js'
5+
6+
type Context = {
7+
logged: string
8+
logger: Logger
9+
clock: SinonFakeTimers
10+
now: Date
11+
}
12+
13+
const test = ava as TestFn<Context>
14+
15+
test.beforeEach((t) => {
16+
/* Capture the stdout pipe */
17+
process.stdout.write = (buffer: string) => {
18+
t.context.logged += buffer
19+
return true
20+
}
21+
22+
/* Stub the date time */
23+
const jan1st = new Date(2021, 0, 1)
24+
t.context.now = jan1st
25+
t.context.clock = sinon.useFakeTimers(jan1st.getTime())
26+
27+
process.env.NODE_ENV = 'test'
28+
t.context.logged = ''
29+
t.context.logger = ByuLogger()
30+
})
31+
32+
test.afterEach((t) => {
33+
t.context.clock.restore()
34+
})
35+
36+
test.serial('default logger should default to silent level', (t) => {
37+
t.context.logger.debug('debug does not work')
38+
39+
t.is(t.context.logger.level, 'silent')
40+
t.is(t.context.logged, '') // no logs should have happened
41+
})
42+
43+
test.serial('default logger should not display logs', (t) => {
44+
t.context.logger.info('info works')
45+
46+
t.is(t.context.logged, '')
47+
})

0 commit comments

Comments
 (0)