Skip to content

Commit

Permalink
feat: support ecmascript syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
stuft2 committed Jul 26, 2024
1 parent 5b43ec8 commit 675225b
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 160 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ web_modules/

# TypeScript cache
*.tsbuildinfo
.tsimp/

# Optional npm cache directory
.npm
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ It will pretty-print logs when run locally, but it will write logs in JSON when
## Install

```
npm i @byu-oit/logger
npm i @byu-oit/logger
```

## Usage
Expand Down
72 changes: 49 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "@byu-oit/logger",
"version": "0.4.2",
"description": "Default configuration for pino logger",
"engines": {
"node": ">=18"
},
"contributors": [
{
"name": "Scott Hutching",
Expand All @@ -12,56 +15,79 @@
"email": "[email protected]"
}
],
"main": "dist/logger.js",
"types": "dist/logger.d.ts",
"type": "module",
"main": "./dist/cjs/index.js",
"types": "./dist/cjs/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
}
},
"files": [
"dist"
],
"scripts": {
"build": "npx rimraf dist && tsc",
"coverage": "npm run test -- --coverage || exit 0",
"build": "rimraf dist && concurrently \"npm:build:*\" -c cyanBright",
"build:cjs": "tsc -p tsconfig.cjs.json && echo-cli \"{\\\"type\\\": \\\"commonjs\\\"}\" > dist/cjs/package.json",
"build:esm": "tsc -p tsconfig.json && echo-cli \"{\\\"type\\\": \\\"module\\\"}\" > dist/esm/package.json",
"clean": "rimraf dist",
"coverage": "c8 ava",
"lint": "npx ts-standard | snazzy",
"lint:fix": "npx ts-standard --fix | snazzy",
"test": "jest",
"test": "ava",
"prepublishOnly": "npm run build"
},
"author": "Brigham Young University - Office of Information Technology",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/byu-oit/logger.git"
},
"bugs": {
"url": "https://github.com/byu-oit/logger/issues"
},
"dependencies": {
"deepmerge": "^4.2.2",
"pino": "^8.11.0",
"pino-http": "^6.6.0"
},
"devDependencies": {
"@tsconfig/node12": "^1.0.11",
"@tsconfig/node18": "^18.2.4",
"@types/jest": "^29.5.0",
"@types/node": "^16.9.1",
"jest": "^29.5.0",
"@types/node": "^18.19.42",
"@types/sinon": "^17.0.3",
"ava": "^6.1.3",
"c8": "^10.1.2",
"concurrently": "^8.2.2",
"echo-cli": "^2.0.0",
"lint-staged": "^12.0.2",
"pino-pretty": "^10.0.0",
"rimraf": "^6.0.1",
"sinon": "^18.0.0",
"snazzy": "^9.0.0",
"ts-jest": "^29.1.0",
"tsimp": "^2.0.11",
"typescript": "^5.0.3"
},
"optionalDependencies": {
"pino-pretty": ">=7"
},
"engines": {
"node": ">=12"
},
"jest": {
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
}
},
"lint-staged": {
"*.ts": "npm run lint:fix"
},
"repository": {
"type": "git",
"url": "git+https://github.com/byu-oit/logger.git"
},
"bugs": {
"url": "https://github.com/byu-oit/logger/issues"
"ava": {
"extensions": {
"ts": "module"
},
"nodeArguments": [
"--import=tsimp"
]
}
}
4 changes: 2 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Logger, LoggerOptions, pino } from 'pino'
import { getLevel, isInstalled, isProduction } from './util'
import deepmerge from 'deepmerge'
import { Logger, LoggerOptions, pino } from 'pino'
import { getLevel, isInstalled, isProduction } from './util.js'

export function ByuLogger (options?: LoggerOptions): Logger {
const defaultOptions: LoggerOptions = {
Expand Down
130 changes: 0 additions & 130 deletions test/logger.spec.ts

This file was deleted.

80 changes: 80 additions & 0 deletions test/production.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import ava, { TestFn } from 'ava'
import sinon, { SinonFakeTimers } from 'sinon'
import { Logger } from 'pino'
import { ByuLogger } from '../src/logger.js'

type Context = {
logged: string
logger: Logger
clock: SinonFakeTimers
now: Date
}

const test = ava as TestFn<Context>

test.beforeEach((t) => {
/* Capture the stdout pipe */
process.stdout.write = (buffer: string) => {
t.context.logged += buffer
return true
}

/* Stub the date time */
const jan1st = new Date(2021, 0, 1)
t.context.now = jan1st
t.context.clock = sinon.useFakeTimers(jan1st.getTime())

process.env.NODE_ENV = 'production'
t.context.logged = ''
t.context.logger = ByuLogger()
})

test.afterEach((t) => {
t.context.clock.restore()
})

test.serial('default logger should default to info level', (t) => {
t.context.logger.debug('debug does not work')

t.is(t.context.logger.level, 'info')
t.is(t.context.logged, '') // no logs should have happened
})

test.serial('default logger displays logs in JSON format', (t) => {
t.context.logger.info('json works')

try {
const parsedLog = JSON.parse(t.context.logged)
t.truthy(parsedLog.message)
t.truthy(parsedLog.level)
t.truthy(parsedLog.time)
} catch (e) {
t.log(e)
t.fail(`The log format should be stringified JSON but parsing failed. See the logged error for details.`)
}
})

test.serial('default logger should display info logs', (t) => {
t.context.logger.info('info works')

try {
const parsedLog = JSON.parse(t.context.logged)
t.is(parsedLog.message, 'info works')
t.is(parsedLog.level, 'info')
} catch (e) {
t.log(e)
t.fail(`The log format should be stringified JSON but parsing failed. See the logged error for details.`)
}
})

test.serial('default logger displays logs with epoch datetime format', (t) => {
t.context.logger.info('iso date works')

try {
const parsedLog = JSON.parse(t.context.logged)
t.is(parsedLog.time, t.context.now.getTime())
} catch (e) {
t.log(e)
t.fail(`The log format should be stringified JSON but parsing failed. See the logged error for details.`)
}
})
47 changes: 47 additions & 0 deletions test/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import ava, { TestFn } from 'ava'
import sinon, { SinonFakeTimers } from 'sinon'
import { Logger } from 'pino'
import { ByuLogger } from '../src/logger.js'

type Context = {
logged: string
logger: Logger
clock: SinonFakeTimers
now: Date
}

const test = ava as TestFn<Context>

test.beforeEach((t) => {
/* Capture the stdout pipe */
process.stdout.write = (buffer: string) => {
t.context.logged += buffer
return true
}

/* Stub the date time */
const jan1st = new Date(2021, 0, 1)
t.context.now = jan1st
t.context.clock = sinon.useFakeTimers(jan1st.getTime())

process.env.NODE_ENV = 'test'
t.context.logged = ''
t.context.logger = ByuLogger()
})

test.afterEach((t) => {
t.context.clock.restore()
})

test.serial('default logger should default to silent level', (t) => {
t.context.logger.debug('debug does not work')

t.is(t.context.logger.level, 'silent')
t.is(t.context.logged, '') // no logs should have happened
})

test.serial('default logger should not display logs', (t) => {
t.context.logger.info('info works')

t.is(t.context.logged, '')
})
Loading

0 comments on commit 675225b

Please sign in to comment.