Skip to content

Commit

Permalink
Add fileNameWithLine, Fix #182
Browse files Browse the repository at this point in the history
  • Loading branch information
terehov committed Dec 21, 2022
1 parent 7c8be79 commit 2a5af1b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: `:`)
Expand Down Expand Up @@ -483,6 +485,7 @@ const logger = new Logger({
nameWithDelimiterSuffix: ["white", "bold"],
errorName: ["bold", "bgRedBright", "whiteBright"],
fileName: ["yellow"],
fileNameWithLine: "white",
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/BaseLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class BaseLogger<LogObj> {
nameWithDelimiterSuffix: ["white", "bold"],
errorName: ["bold", "bgRedBright", "whiteBright"],
fileName: ["yellow"],
fileNameWithLine: "white",
},
prettyInspectOptions: settings?.prettyInspectOptions ?? {
colors: true,
Expand Down Expand Up @@ -315,6 +316,7 @@ export class BaseLogger<LogObj> {
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
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface ISettings<LogObj> extends ISettingsParam<LogObj> {
dateIsoStr?: TStyle;
logLevelName?: TStyle;
fileName?: TStyle;
fileNameWithLine?: TStyle;
filePath?: TStyle;
fileLine?: TStyle;
filePathWithLine?: TStyle;
Expand Down Expand Up @@ -116,6 +117,7 @@ export interface ILogObjMeta {
export interface IStackFrame {
fullFilePath?: string;
fileName?: string;
fileNameWithLine?: string;
filePath?: string;
fileLine?: string;
fileColumn?: string;
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -79,6 +81,7 @@ export function getCallerStackFrame(stackDepthLevel: number): IStackFrame {
return {
fullFilePath: undefined,
fileName: undefined,
fileNameWithLine: undefined,
fileColumn: undefined,
fileLine: undefined,
filePath: undefined,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/nodejs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 2a5af1b

Please sign in to comment.