Skip to content

Commit 0e17b89

Browse files
committed
add enableFileAndLine
1 parent b9513d5 commit 0e17b89

File tree

9 files changed

+108
-18
lines changed

9 files changed

+108
-18
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ fileStream.close()
9999

100100
You can use `less -R test.log` to see the result.
101101

102+
### `enableFileAndLine()`
103+
104+
Print the filename and line information in the logs. Note that this may increase a little performance overhead.
105+
106+
```js
107+
logger.enableFileAndLine(true /* enable */);
108+
109+
logger.debug('This log should include file and line information.');
110+
// 2025-07-23T18:43:27.199Z[/Users/tigercosmos/node-color-log/index.js:157] [DEBUG] This log should include file and line information.
111+
112+
logger.enableFileAndLine(true /* enable */, true /* short file name */);
113+
logger.debug('This log should include file and line information.');
114+
// 2025-07-23T18:43:27.200Z[index.js:157:33:157] [DEBUG] This log should include file and line information.
115+
```
116+
102117
### `fontColorLog()`, `bgColorLog()`, `colorLog()`
103118

104119
- `message` here must be a string.

change.log.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
13.0.0
2+
- add `enableFileAndLine`
3+
14
12.0.1
25
- added tests.
36

index.d.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ declare class Logger {
3232

3333
setLevelColor(): void;
3434

35+
enableFileAndLine(enable: boolean, isShortFile?: boolean): void;
36+
3537
isLevelValid(level: types.LEVEL): boolean;
3638

3739
isAllowedLevel(level: types.LEVEL): boolean;
@@ -47,27 +49,27 @@ declare class Logger {
4749
setDate(callback: Function): void;
4850

4951
getDate(): string;
50-
52+
5153
color(ticket: types.COLOR): Logger;
52-
54+
5355
bgColor(ticket: types.COLOR): Logger;
54-
56+
5557
bold(): Logger;
56-
58+
5759
dim(): Logger;
58-
60+
5961
underscore(): Logger;
60-
62+
6163
strikethrough(): Logger;
62-
64+
6365
reverse(): Logger;
64-
66+
6567
italic(): Logger;
66-
68+
6769
fontColorLog(ticket: types.COLOR, text: string, setting?: settingObject): void;
68-
70+
6971
bgColorLog(ticket: types.COLOR, text: string, setting?: settingObject): void;
70-
72+
7173
colorLog(ticketObj: ticketObject, text: string, setting?: settingObject): void;
7274

7375
log(...args: any[]): Logger;

index.js

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class Logger {
5555
this._getDate = () => (new Date()).toISOString();
5656

5757
this._customizedConsole = console;
58+
59+
this._enableFileAndLine = {
60+
enable: false,
61+
isShortFile: false,
62+
};
5863
}
5964

6065
createNamedLogger(name) {
@@ -94,6 +99,15 @@ class Logger {
9499
return this.level ? LEVELS.indexOf(this.level) <= LEVELS.indexOf(level) : true
95100
}
96101

102+
enableFileAndLine(enable, isShortFile = false) {
103+
if (typeof enable === 'boolean') {
104+
this._enableFileAndLine.enable = enable;
105+
this._enableFileAndLine.isShortFile = isShortFile;
106+
} else {
107+
console.error("node-color-log warning: enableFileAndLine should be a boolean value.");
108+
}
109+
}
110+
97111
log(...args) {
98112
this.append(...args);
99113
if (!this.noColor) {
@@ -134,11 +148,19 @@ class Logger {
134148
}
135149

136150
getPrefix() {
151+
let prefix = `${this._getDate()}`;
152+
137153
if (this.name) {
138-
return `${this._getDate()} [${this.name}]`;
139-
} else {
140-
return this._getDate();
154+
prefix += ` [${this.name}]`;
155+
}
156+
if (this._enableFileAndLine.enable) {
157+
const fileAndLine = getFileAndLine(this._enableFileAndLine.isShortFile);
158+
if (fileAndLine) {
159+
prefix += `[${fileAndLine}]`;
160+
}
141161
}
162+
163+
return prefix;
142164
}
143165

144166
color(ticket) {
@@ -376,5 +398,32 @@ class Logger {
376398
}
377399
}
378400

401+
function getFileAndLine(isShortFile = false) {
402+
const e = new Error();
403+
const line = e.stack.split('\n')[2]; // 3rd line: caller
404+
405+
// find ( and ) in the line from the end
406+
let start = line.lastIndexOf('(');
407+
let end = line.lastIndexOf(')');
408+
if (start === -1 || end === -1 || start >= end) {
409+
return '';
410+
}
411+
// Extract the file and line number
412+
const fileAndLine = line.substring(start + 1, end);
413+
414+
if (isShortFile) {
415+
const fileName = fileAndLine.split('/').pop(); // Get the last part of the path
416+
return `${fileName}:${fileAndLine.split(':')[1]}`; // Return file name and line number
417+
}
418+
419+
// Split by : to get the file path and line number
420+
const parts = fileAndLine.split(':');
421+
if (parts.length < 2) {
422+
return '';
423+
}
424+
// Return the file path and line number
425+
return `${parts[0]}:${parts[1]}`;
426+
}
427+
379428
const logger = new Logger();
380-
module.exports = logger;
429+
module.exports = logger;

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-color-log",
3-
"version": "12.0.1",
3+
"version": "13.0.0",
44
"description": "The more powerful JavaScript logger for NodeJS and browsers.",
55
"main": "index.js",
66
"scripts": {

test/test5.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const logger = require('../index');
2+
3+
logger.log('****************');
4+
logger.log('*** Test File and Line ***');
5+
logger.log('****************');
6+
7+
logger.enableFileAndLine(true);
8+
9+
logger.debug('This log should include file and line information.');
10+
logger.debug('Another log with file and line information.');
11+
12+
13+
logger.enableFileAndLine(true, true);
14+
15+
logger.debug('This log should include file and line information.');
16+
logger.debug('Another log with file and line information.');

test/test_main.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ node test\test3.js
88
$env:LOGGER = "info"
99
node test\test4.js
1010

11+
$env:LOGGER = "debug"
12+
node test\test5.js
13+
1114
node test\test_stream.js

test/test_main.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ node test/test3.js
88

99
LOGGER=info node test/test4.js
1010

11+
node test/test5.js
12+
1113
node test/test_stream.js

0 commit comments

Comments
 (0)