diff --git a/README.md b/README.md index 8f83cd9..9710e76 100644 --- a/README.md +++ b/README.md @@ -426,12 +426,14 @@ Following settings are available for styling: - `{{nameWithDelimiterSuffix}}`: optional name of the current logger (s. above) with a delimiter at the end - `{{fullFilePath}}`: a full path starting from `/` root - `{{filePathWithLine}}`: a full path below the project path with line number + - `{{fileNameWithLine}}`: file name with line number - `prettyErrorTemplate`: template string for error message. Possible placeholders: - `{{errorName}}`: name of the error - `{{errorMessage}}`: error message - `{{errorStack}}`: Placeholder for all stack lines defined by `prettyErrorStackTemplate` - `prettyErrorStackTemplate`: template string for error stack trace lines. Possible placeholders: - `{{fileName}}`: name of the file + - `{{fileNameWithLine}}`: file name with line number - `{{filePathWithLine}}`: a full path below the project path with a line number - `{{method}}`: _optional_ name of the invoking method - `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({ nameWithDelimiterSuffix: ["white", "bold"], errorName: ["bold", "bgRedBright", "whiteBright"], fileName: ["yellow"], + fileNameWithLine: "white", }, }); diff --git a/src/BaseLogger.ts b/src/BaseLogger.ts index 69a3e7d..5403887 100644 --- a/src/BaseLogger.ts +++ b/src/BaseLogger.ts @@ -51,6 +51,7 @@ export class BaseLogger { nameWithDelimiterSuffix: ["white", "bold"], errorName: ["bold", "bgRedBright", "whiteBright"], fileName: ["yellow"], + fileNameWithLine: "white", }, prettyInspectOptions: settings?.prettyInspectOptions ?? { colors: true, @@ -315,6 +316,7 @@ export class BaseLogger { placeholderValues["rawIsoStr"] = dateInSettingsTimeZone?.toISOString(); placeholderValues["dateIsoStr"] = dateInSettingsTimeZone?.toISOString().replace("T", " ").replace("Z", ""); placeholderValues["logLevelName"] = logObjMeta?.logLevelName; + placeholderValues["fileNameWithLine"] = logObjMeta?.path?.fileNameWithLine; placeholderValues["filePathWithLine"] = logObjMeta?.path?.filePathWithLine; placeholderValues["fullFilePath"] = logObjMeta?.path?.fullFilePath; // name diff --git a/src/interfaces.ts b/src/interfaces.ts index 49c39e4..c10c72d 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -87,6 +87,7 @@ export interface ISettings extends ISettingsParam { dateIsoStr?: TStyle; logLevelName?: TStyle; fileName?: TStyle; + fileNameWithLine?: TStyle; filePath?: TStyle; fileLine?: TStyle; filePathWithLine?: TStyle; @@ -116,6 +117,7 @@ export interface ILogObjMeta { export interface IStackFrame { fullFilePath?: string; fileName?: string; + fileNameWithLine?: string; filePath?: string; fileLine?: string; fileColumn?: string; diff --git a/src/runtime/browser/index.ts b/src/runtime/browser/index.ts index e5fdcb3..4b94253 100644 --- a/src/runtime/browser/index.ts +++ b/src/runtime/browser/index.ts @@ -63,11 +63,13 @@ export function getCallerStackFrame(stackDepthLevel: number): IStackFrame { const fileLine = pathArray?.pop(); const filePath = pathArray?.pop()?.split("?")?.[0]; const fileName = filePath?.split("/").pop(); - const filePathWithLine = `${href}${filePath}:${fileLine}`; + const fileNameWithLine = `${fileName}:${fileLine}`; + const filePathWithLine = `${filePath}:${fileLine}`; const errorStackLine = fullFilePath?.split(" ("); return { fullFilePath, fileName, + fileNameWithLine, fileColumn, fileLine, filePath, @@ -79,6 +81,7 @@ export function getCallerStackFrame(stackDepthLevel: number): IStackFrame { return { fullFilePath: undefined, fileName: undefined, + fileNameWithLine: undefined, fileColumn: undefined, fileLine: undefined, filePath: undefined, @@ -114,12 +117,14 @@ export function getErrorTrace(error: Error): IStackFrame[] { const fileLine = pathArray?.pop(); const filePath = pathArray?.pop()?.split("?")[0]; const fileName = filePath?.split("/")?.pop()?.split("?")[0]; + const fileNameWithLine = `${fileName}:${fileLine}`; const filePathWithLine = `${filePath}:${fileLine}`; if (filePath != null && filePath.length > 0) { result.push({ fullFilePath, fileName, + fileNameWithLine, fileColumn, fileLine, filePath, diff --git a/src/runtime/nodejs/index.ts b/src/runtime/nodejs/index.ts index ead6441..d8ae6e3 100644 --- a/src/runtime/nodejs/index.ts +++ b/src/runtime/nodejs/index.ts @@ -55,11 +55,14 @@ export function getCallerStackFrame(stackDepthLevel: number, error?: Error): ISt const fileColumn = pathArray?.pop(); const fileLine = pathArray?.pop(); const filePath = pathArray?.pop(); - const fileName = filePath?.split("/").pop(); const filePathWithLine = fileNormalize(`${filePath}:${fileLine}`); + const fileName = filePath?.split("/").pop(); + const fileNameWithLine = `${fileName}:${fileLine}`; + return { fullFilePath, fileName, + fileNameWithLine, fileColumn, fileLine, filePath, @@ -80,13 +83,15 @@ export function getErrorTrace(error: Error): IStackFrame[] { const fileColumn = pathArray?.pop(); const fileLine = pathArray?.pop(); const filePath = pathArray?.pop(); - const fileName = filePath?.split("/")?.pop(); const filePathWithLine = fileNormalize(`${filePath}:${fileLine}`); + const fileName = filePath?.split("/")?.pop(); + const fileNameWithLine = `${fileName}:${fileLine}`; if (filePath != null && filePath.length > 0) { result.push({ fullFilePath, fileName, + fileNameWithLine, fileColumn, fileLine, filePath,