diff --git a/src/filelogger.ts b/src/filelogger.ts index 5e9dca9..ceed4ad 100644 --- a/src/filelogger.ts +++ b/src/filelogger.ts @@ -1,4 +1,4 @@ -import { accessSync, constants as fsConstants, writeFileSync, createWriteStream, WriteStream, mkdirSync } from 'fs' +import { accessSync, writeFileSync, createWriteStream, WriteStream, mkdirSync } from 'fs' import { dirname as directoryname } from 'path' export class FileLogger { @@ -10,9 +10,10 @@ export class FileLogger { this.#filename = filename this.#_stat() this.#_createWritableStream() + this.#_endStream() } - #fsAccess(filename: string, mode: number) { + #fsAccess(filename: string, mode?: number) { try { accessSync(filename, mode) return true @@ -23,11 +24,13 @@ export class FileLogger { #_stat() { //check if file exists - if (!this.#fsAccess(this.#filename, fsConstants.W_OK)) { + if (!this.#fsAccess(this.#filename)) { // check if directory exists - if (!this.#fsAccess(this.#dirname, fsConstants.W_OK)) { - mkdirSync(this.#dirname, { recursive: true, mode: fsConstants.W_OK }) + if (!this.#fsAccess(this.#dirname)) { + // create the directory + mkdirSync(this.#dirname, { recursive: true }) } + // create the file and write an empty string to it writeFileSync(this.#filename, '') return } @@ -42,21 +45,21 @@ export class FileLogger { } #_endStream() { - process.on('exit', (code) => { + process.on('exit', () => { this.writableStream.close() }) - process.on('SIGTERM', (signal) => { + process.on('SIGTERM', () => { this.writableStream.close() process.exit(0) }) - process.on('SIGINT', (signal) => { + process.on('SIGINT', () => { this.writableStream.close() process.exit(0) }) - process.on('uncaughtException', (err) => { + process.on('uncaughtException', () => { this.writableStream.close() process.exit(1) }) diff --git a/tests/index.test.ts b/tests/index.test.ts index a2d3434..65cb33e 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -6,6 +6,7 @@ import { App } from '@tinyhttp/app' import expect from 'expect' import * as assert from 'uvu/assert' import { promises, constants as fsConstants, readFileSync, unlinkSync, existsSync } from 'fs' +import { resolve } from 'path' function describe(name: string, fn: (it: uvu.Test) => void) { const s = suite(name) @@ -15,7 +16,7 @@ function describe(name: string, fn: (it: uvu.Test) => void) { function checkFileExists(file) { return promises - .access(file, fsConstants.F_OK) + .access(file) .then(() => true) .catch(() => false) } @@ -98,7 +99,7 @@ describe('Logger tests', (it) => { }) describe('Log file tests', (it) => { it('should check if log file and directory is created', async (test) => { - const filename = './log/tiny.log' + const filename = './tests/tiny.log' const app = new App() app.use( @@ -120,7 +121,7 @@ describe('Logger tests', (it) => { .finally(() => server.close()) }) it('should read log file and check if logs are written', async (test) => { - const filename = './log/tiny.log' + const filename = './logs/test1/tiny.log' const level = LogLevel.warn const app = new App() app.use(