Skip to content

Commit 2a5af1b

Browse files
committed
Add fileNameWithLine, Fix #182
1 parent 7c8be79 commit 2a5af1b

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,14 @@ Following settings are available for styling:
426426
- `{{nameWithDelimiterSuffix}}`: optional name of the current logger (s. above) with a delimiter at the end
427427
- `{{fullFilePath}}`: a full path starting from `/` root
428428
- `{{filePathWithLine}}`: a full path below the project path with line number
429+
- `{{fileNameWithLine}}`: file name with line number
429430
- `prettyErrorTemplate`: template string for error message. Possible placeholders:
430431
- `{{errorName}}`: name of the error
431432
- `{{errorMessage}}`: error message
432433
- `{{errorStack}}`: Placeholder for all stack lines defined by `prettyErrorStackTemplate`
433434
- `prettyErrorStackTemplate`: template string for error stack trace lines. Possible placeholders:
434435
- `{{fileName}}`: name of the file
436+
- `{{fileNameWithLine}}`: file name with line number
435437
- `{{filePathWithLine}}`: a full path below the project path with a line number
436438
- `{{method}}`: _optional_ name of the invoking method
437439
- `prettyErrorParentNamesSeparator`: separator to be used when joining names ot the parent logger, and the current one (default: `:`)
@@ -483,6 +485,7 @@ const logger = new Logger({
483485
nameWithDelimiterSuffix: ["white", "bold"],
484486
errorName: ["bold", "bgRedBright", "whiteBright"],
485487
fileName: ["yellow"],
488+
fileNameWithLine: "white",
486489
},
487490
});
488491

src/BaseLogger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class BaseLogger<LogObj> {
5151
nameWithDelimiterSuffix: ["white", "bold"],
5252
errorName: ["bold", "bgRedBright", "whiteBright"],
5353
fileName: ["yellow"],
54+
fileNameWithLine: "white",
5455
},
5556
prettyInspectOptions: settings?.prettyInspectOptions ?? {
5657
colors: true,
@@ -315,6 +316,7 @@ export class BaseLogger<LogObj> {
315316
placeholderValues["rawIsoStr"] = dateInSettingsTimeZone?.toISOString();
316317
placeholderValues["dateIsoStr"] = dateInSettingsTimeZone?.toISOString().replace("T", " ").replace("Z", "");
317318
placeholderValues["logLevelName"] = logObjMeta?.logLevelName;
319+
placeholderValues["fileNameWithLine"] = logObjMeta?.path?.fileNameWithLine;
318320
placeholderValues["filePathWithLine"] = logObjMeta?.path?.filePathWithLine;
319321
placeholderValues["fullFilePath"] = logObjMeta?.path?.fullFilePath;
320322
// name

src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export interface ISettings<LogObj> extends ISettingsParam<LogObj> {
8787
dateIsoStr?: TStyle;
8888
logLevelName?: TStyle;
8989
fileName?: TStyle;
90+
fileNameWithLine?: TStyle;
9091
filePath?: TStyle;
9192
fileLine?: TStyle;
9293
filePathWithLine?: TStyle;
@@ -116,6 +117,7 @@ export interface ILogObjMeta {
116117
export interface IStackFrame {
117118
fullFilePath?: string;
118119
fileName?: string;
120+
fileNameWithLine?: string;
119121
filePath?: string;
120122
fileLine?: string;
121123
fileColumn?: string;

src/runtime/browser/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ export function getCallerStackFrame(stackDepthLevel: number): IStackFrame {
6363
const fileLine = pathArray?.pop();
6464
const filePath = pathArray?.pop()?.split("?")?.[0];
6565
const fileName = filePath?.split("/").pop();
66-
const filePathWithLine = `${href}${filePath}:${fileLine}`;
66+
const fileNameWithLine = `${fileName}:${fileLine}`;
67+
const filePathWithLine = `${filePath}:${fileLine}`;
6768
const errorStackLine = fullFilePath?.split(" (");
6869
return {
6970
fullFilePath,
7071
fileName,
72+
fileNameWithLine,
7173
fileColumn,
7274
fileLine,
7375
filePath,
@@ -79,6 +81,7 @@ export function getCallerStackFrame(stackDepthLevel: number): IStackFrame {
7981
return {
8082
fullFilePath: undefined,
8183
fileName: undefined,
84+
fileNameWithLine: undefined,
8285
fileColumn: undefined,
8386
fileLine: undefined,
8487
filePath: undefined,
@@ -114,12 +117,14 @@ export function getErrorTrace(error: Error): IStackFrame[] {
114117
const fileLine = pathArray?.pop();
115118
const filePath = pathArray?.pop()?.split("?")[0];
116119
const fileName = filePath?.split("/")?.pop()?.split("?")[0];
120+
const fileNameWithLine = `${fileName}:${fileLine}`;
117121
const filePathWithLine = `${filePath}:${fileLine}`;
118122

119123
if (filePath != null && filePath.length > 0) {
120124
result.push({
121125
fullFilePath,
122126
fileName,
127+
fileNameWithLine,
123128
fileColumn,
124129
fileLine,
125130
filePath,

src/runtime/nodejs/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ export function getCallerStackFrame(stackDepthLevel: number, error?: Error): ISt
5555
const fileColumn = pathArray?.pop();
5656
const fileLine = pathArray?.pop();
5757
const filePath = pathArray?.pop();
58-
const fileName = filePath?.split("/").pop();
5958
const filePathWithLine = fileNormalize(`${filePath}:${fileLine}`);
59+
const fileName = filePath?.split("/").pop();
60+
const fileNameWithLine = `${fileName}:${fileLine}`;
61+
6062
return {
6163
fullFilePath,
6264
fileName,
65+
fileNameWithLine,
6366
fileColumn,
6467
fileLine,
6568
filePath,
@@ -80,13 +83,15 @@ export function getErrorTrace(error: Error): IStackFrame[] {
8083
const fileColumn = pathArray?.pop();
8184
const fileLine = pathArray?.pop();
8285
const filePath = pathArray?.pop();
83-
const fileName = filePath?.split("/")?.pop();
8486
const filePathWithLine = fileNormalize(`${filePath}:${fileLine}`);
87+
const fileName = filePath?.split("/")?.pop();
88+
const fileNameWithLine = `${fileName}:${fileLine}`;
8589

8690
if (filePath != null && filePath.length > 0) {
8791
result.push({
8892
fullFilePath,
8993
fileName,
94+
fileNameWithLine,
9095
fileColumn,
9196
fileLine,
9297
filePath,

0 commit comments

Comments
 (0)