Skip to content

Commit a0f7a56

Browse files
Merge pull request #15 from cybercoder-naj/fix/file-logging
Fix file logging
2 parents aafa101 + 358e26e commit a0f7a56

File tree

8 files changed

+32
-20
lines changed

8 files changed

+32
-20
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# CHANGELOG
2+
## [1.0.0-alpha.6] - 26-03-2024
3+
### Fixed
4+
- Fix issues related to file logging. ([#14](https://github.com/cybercoder-naj/logestic/issues/14))
5+
26
## [1.0.0-alpha.5] - 26-03-2024
37
### Changed
48
- Request duration returns time in microseconds.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "logestic",
3-
"version": "1.0.0-alpha.5",
3+
"version": "1.0.0-alpha.6",
44
"author": "Nishant Aanjaney Jalan <cybercoder.nishant@gmail.com>",
55
"description": "An advanced and customisable logging library for ElysiaJS",
66
"keywords": [

preview/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.log

preview/bun.lockb

346 Bytes
Binary file not shown.

preview/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
"homepage": "https://github.com/cybercoder-naj/logestic#readme",
1919
"dependencies": {
2020
"elysia": "^1.0.0",
21-
"logestic": ".."
21+
"logestic": "file:.."
2222
}
2323
}

preview/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Elysia } from 'elysia';
22
import { Logestic } from 'logestic';
33

44
const app = new Elysia()
5-
.use(Logestic.preset('common', { showType: true }))
5+
.use(Logestic.preset('fancy'))
66
.get('/', () => 'Hello, world!')
77
.get('/hello/:name', ({ params: { name } }) => `Hello, ${name}!`)
88
.get('/returnBad', ({ set }) => {

src/index.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import {
1313
} from './types';
1414
import { BunFile } from 'bun';
1515
import c from 'chalk';
16-
import { buildAttrs, colourLogType } from './utils';
16+
import { buildAttrs, colourLogType, removeAnsi } from './utils';
1717
import { getPreset } from './presets';
18+
import fs from 'node:fs';
1819

1920
export type { Attribute, LogesticOptions };
2021
export const chalk = c; // Re-export chalk for custom formatting
@@ -62,11 +63,15 @@ export class Logestic {
6263
}
6364

6465
// Custom file destination
65-
this.dest = this.createFileIfNotExists(dest);
66+
this.createFileIfNotExists(dest)
67+
.then(file => (this.dest = file))
68+
.catch(err => {
69+
throw err;
70+
});
6671
}
6772

68-
private createFileIfNotExists(dest: BunFile): BunFile {
69-
if (!dest.exists()) {
73+
private async createFileIfNotExists(dest: BunFile): Promise<BunFile> {
74+
if (!(await dest.exists())) {
7075
Bun.write(dest, '');
7176
}
7277
return dest;
@@ -156,21 +161,19 @@ export class Logestic {
156161
}
157162

158163
private async log(msg: string): Promise<void> {
159-
let content: string | undefined = undefined;
160-
// Get the content of the file if it is not stdout
161-
// Possible race condition in the file for multiple requests
162-
if (this.dest !== Bun.stdout) {
163-
content = await this.dest.text();
164+
const msgNewLine = `${msg}\n`;
165+
if (!this.dest.name || !this.dest.name.length) {
166+
// This is either stdout or stderr
167+
Bun.write(this.dest, msgNewLine);
168+
return;
164169
}
165170

166-
// Append the log message
167-
const writer = this.dest.writer();
168-
if (content) {
169-
writer.write(content);
170-
}
171-
writer.write(msg);
172-
writer.write('\n');
173-
writer.flush();
171+
const sanitised = removeAnsi(msgNewLine);
172+
fs.appendFile(this.dest.name, sanitised, err => {
173+
if (err) {
174+
throw err;
175+
}
176+
});
174177
}
175178

176179
/**

src/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,7 @@ export const colourLogType = (
116116
const withSpaces = ` ${type.toUpperCase()} `;
117117
return bgColour?.(withSpaces) ?? withSpaces;
118118
};
119+
120+
export function removeAnsi(text: string): string {
121+
return text.replace(/\u001b\[\d*m/g, '').trimStart();
122+
}

0 commit comments

Comments
 (0)