Skip to content

Commit 3dd3475

Browse files
committed
chore(@stenodb/logger): add tests
1 parent 4b218ac commit 3dd3475

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

packages/logger/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
],
2323
"scripts": {
2424
"dev": "pnpm build --watch src",
25-
"build": "tsup src/index.ts src/rotation.ts --format esm,cjs --dts --clean"
25+
"build": "tsup src/index.ts src/rotation.ts --format esm,cjs --dts --clean",
26+
"test": "tsx src/index.test.ts",
27+
"test:watch": "tsx watch src/index.test.ts"
2628
},
2729
"dependencies": {
2830
"rotating-file-stream": "3.0.4",

packages/logger/src/index.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { existsSync } from 'node:fs'
2+
import { readFile, rm } from 'node:fs/promises'
3+
import { join, resolve } from 'node:path'
4+
import { Logger } from 'tslog'
5+
import { test } from 'uvu'
6+
import * as assert from 'uvu/assert'
7+
import { createLogger } from './logger.js'
8+
import { createRotation } from './rotation.js'
9+
10+
const logsPath = resolve('logs')
11+
12+
test.before(async () => {
13+
if (existsSync(logsPath)) {
14+
await rm(logsPath, { recursive: true })
15+
}
16+
})
17+
18+
test.after(async () => {
19+
const logs = await readFile(join(logsPath, 'logs.log'), { encoding: 'utf-8' })
20+
assert.equal(logs, '')
21+
})
22+
23+
const loggerRotation = createRotation({
24+
path: 'logs',
25+
size: '10M',
26+
interval: '1d',
27+
compress: 'gzip'
28+
})
29+
30+
test('createLogger', () => {
31+
const logger = createLogger({ type: 'pretty' })('test')
32+
assert.instance(logger, Logger)
33+
assert.is(logger.settings.type, 'pretty')
34+
})
35+
36+
test('createRotation', async () => {
37+
const logger = createLogger({
38+
type: 'pretty',
39+
rotation: loggerRotation
40+
})('test-rotation')
41+
42+
logger.info('foo info')
43+
logger.warn('foo warn')
44+
logger.error('foo error')
45+
logger.debug('foo debug')
46+
47+
// TODO: read logs afterEach
48+
})
49+
50+
test.run()

packages/logger/src/rotation.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import { BaseLogger, RotationOptions } from './types.js'
33

44
export function createRotation(options: RotationOptions) {
55
return (logger: BaseLogger) => {
6-
const stream = createStream(options.path, options)
6+
const stream = createStream(options.path + '.log', options)
77

88
logger.attachTransport((log) => {
9-
stream.write(JSON.stringify(log) + '\n')
9+
const meta = log['_meta']
10+
const message = `${meta?.date.toISOString()} ${meta?.logLevelName} [${
11+
meta?.name
12+
}] ${log[0]}`
13+
stream.write(message + '\n')
1014
})
1115
}
1216
}

0 commit comments

Comments
 (0)