Skip to content

Commit

Permalink
feat: added date time to error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
cybercoder-naj committed Mar 21, 2024
1 parent 590e391 commit 3233de1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export class Logestic {
this.log(msg);
})
.onError({ as: 'global' }, ({ request, error, code }) => {
const msg = formatAttr.onFailure({ request, error, code });
let datetime = new Date();
const msg = formatAttr.onFailure({ request, error, code, datetime });
this.log(msg);
});
}
Expand Down
12 changes: 9 additions & 3 deletions src/presets/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import chalk from 'chalk';
const preset: PresetValue = {
uses: ['time', 'method', 'path', 'status'],
formatAttr: {
onSuccess: ({ time, method, path, status }) => {
const grayTime = chalk.gray(`[${time!!.toISOString()}]`);
onSuccess({ time, method, path, status }) {
const grayTime = chalk.gray(`${time!!.toISOString()}`);
const methodPath = chalk.cyan(`${method} ${path}`);
let statusColor = chalk.white;

Expand All @@ -15,7 +15,13 @@ const preset: PresetValue = {

return `[${grayTime}] ${methodPath} ${statusColor(status)}`;
},
onFailure: () => ''
onFailure({ request, error, code, datetime }) {
const grayTime = chalk.gray(`${datetime.toISOString()}`);
const msg = chalk.red(
`Error: ${request.method} ${request.url} ${error.message} ${code}`
);
return `[${grayTime}] ${msg}`;
}
}
};

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type ErrorAttribute = {
request: Request;
error: Error;
code: any; // either string description or number
datetime: Date;
};

/**
Expand Down

0 comments on commit 3233de1

Please sign in to comment.