Skip to content

Commit c9e6213

Browse files
committed
fixed .log formatter
1 parent 436822d commit c9e6213

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Per abilitare la scrittura su file nella directory `logs/`:
8282

8383
```env
8484
LOG_STORE=true
85+
LOG_DIR=test_log
8586
```
8687

8788
> Se `LOG_STORE=true`, ogni log sarà anche salvato in file `logs/YYYY-MM-DD.log`.

package-lock.json

Lines changed: 40 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@
5151
"@types/node": "^24.2.0",
5252
"eslint": "^8.56.0",
5353
"ts-node": "^10.9.1",
54-
"typescript": "^5.9.2",
55-
"tsx": "^4.8.0"
54+
"tsx": "^4.8.0",
55+
"typescript": "^5.9.2"
5656
},
5757
"dependencies": {
5858
"commander": "^14.0.0",
5959
"console-log-colors": "^0.5.0",
60-
"dotenv": "^17.2.1"
60+
"dotenv": "^17.2.1",
61+
"strip-ansi": "^7.1.0"
6162
}
62-
}
63+
}

src/consoleHelpers.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// consoleHelpers.js
22
import fs from 'fs';
33
import path from 'path';
4+
import stripAnsi from 'strip-ansi';
5+
46
import { color, log, red, green, cyan, gray, cyanBright, whiteBright, white, whiteBG, bgWhite, bgYellowBright, bgCyanBright, blueBright, yellow, magenta } from 'console-log-colors';
57
const ENABLE_LOG_STORE = process.env.LOG_STORE === 'true';
68

79
/* ─────────────── CONFIGURAZIONE BASE ──────────────── */
8-
const LOG_DIR = path.resolve('logs'); // cartella log
10+
const LOG_DIR = path.resolve(process.env.LOG_DIR || 'logs'); // cartella log
11+
912
const DATE_FMT = new Intl.DateTimeFormat('it-IT', {
1013
year: 'numeric',
1114
month: '2-digit',
@@ -92,6 +95,7 @@ const getLogDateString = (): string => {
9295
const [day, month, year] = DATE_FMT.format(new Date()).split('/');
9396
return `${year}-${month}-${day}`;
9497
};
98+
9599
/**
96100
* Scrive il log su file se abilitato da env.
97101
*
@@ -106,10 +110,11 @@ const writeLogToFile = (level: LogLevel, context: string, subMex: string, format
106110

107111
const timestamp = getLogTimestamp();
108112
const logLine = `[${timestamp}] [${context}] ${level} ${subMex}:\n${typeof formattedMessage === 'string' ? formattedMessage : JSON.stringify(formattedMessage, null, 2)}\n${errorStack}\n`;
113+
const cleanLog = stripAnsi(logLine); // 🔥 pulizia ansi solo qui
109114

110115
const logFileName = path.join(LOG_DIR, `${getLogDateString()}.log`);
111116
try {
112-
fs.appendFileSync(logFileName, logLine);
117+
fs.appendFileSync(logFileName, cleanLog + '\n');
113118
} catch (err: unknown) {
114119
const e = toLoggable(err);
115120
console.warn(`[consoleHelpers] ❌ Errore scrittura log file: ${e instanceof Error ? e.message : String(e)}`);

0 commit comments

Comments
 (0)