generated from tinyhttp/repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from aarontravass/aaron/logger
feat: added log file streaming
- Loading branch information
Showing
9 changed files
with
239 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules | ||
dist | ||
coverage | ||
coverage | ||
log | ||
*.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { accessSync, writeFileSync, createWriteStream, WriteStream, mkdirSync } from 'fs' | ||
import { dirname as directoryname } from 'path' | ||
|
||
export class FileLogger { | ||
readonly #filename: string | ||
readonly #dirname: string | ||
private writableStream: WriteStream | ||
constructor(filename: string) { | ||
this.#dirname = directoryname(filename) | ||
this.#filename = filename | ||
this.#_stat() | ||
this.#_createWritableStream() | ||
this.#_endStream() | ||
} | ||
|
||
#fsAccess(filename: string, mode?: number) { | ||
try { | ||
accessSync(filename, mode) | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
|
||
#_stat() { | ||
//check if file exists | ||
if (!this.#fsAccess(this.#filename)) { | ||
// check if directory exists | ||
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 | ||
} | ||
} | ||
|
||
#_createWritableStream() { | ||
this.writableStream = createWriteStream(this.#filename, { flags: 'a' }) | ||
} | ||
|
||
toFile(stringToLog: string) { | ||
this.writableStream.write(stringToLog + '\n') | ||
} | ||
|
||
#_endStream() { | ||
process.on('exit', () => { | ||
this.writableStream.close() | ||
}) | ||
|
||
process.on('SIGTERM', () => { | ||
this.writableStream.close() | ||
process.exit(0) | ||
}) | ||
|
||
process.on('SIGINT', () => { | ||
this.writableStream.close() | ||
process.exit(0) | ||
}) | ||
|
||
process.on('uncaughtException', () => { | ||
this.writableStream.close() | ||
process.exit(1) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.