Skip to content

Commit 08f65db

Browse files
Merge pull request #12 from cybercoder-naj/feat-10/request-timing
Feat-10/request-timing
2 parents 0064c55 + d62ff7b commit 08f65db

File tree

7 files changed

+39
-10
lines changed

7 files changed

+39
-10
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
# CHANGELOG
2+
## [1.0.0-alpha.5] - 26-03-2024
3+
### Changed
4+
- Request duration returns time in microseconds.
5+
- `fancy` preset includes the duration on successful requests.
6+
7+
<!-- ### Fixed
8+
- Incorrect status code when returning with `error` function ([#11](https://github.com/cybercoder-naj/logestic/issues/11)) -->
9+
10+
## [1.0.0-alpha.4] - 26-03-2024
11+
### Added
12+
- Request duration as a logging feature ([#10](https://github.com/cybercoder-naj/logestic/issues/10))
13+
214
## [1.0.0-alpha.3] - 25-03-2024
315
### Changed
416
- README file and updated the Wiki page.

bun.lockb

0 Bytes
Binary file not shown.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "logestic",
3-
"version": "1.0.0-alpha.3",
3+
"version": "1.0.0-alpha.5",
44
"author": "Nishant Aanjaney Jalan <cybercoder.nishant@gmail.com>",
55
"description": "An advanced and customisable logging library for ElysiaJS",
66
"keywords": [
@@ -30,12 +30,12 @@
3030
},
3131
"peerDependencies": {
3232
"typescript": "^5.0.0",
33-
"elysia": "^1.0.0"
33+
"elysia": "^1.0.9"
3434
},
3535
"devDependencies": {
36-
"@elysiajs/eden": "1.0.0",
36+
"@elysiajs/eden": "1.0.7",
3737
"bun-types": "latest",
38-
"elysia": "^1.0.7",
38+
"elysia": "^1.0.9",
3939
"rimraf": "^5.0.5",
4040
"typescript": "^5.4.3"
4141
},

src/index.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,20 @@ export class Logestic {
125125
*/
126126
format(this: Logestic, formatAttr: FormatObj) {
127127
return this.build()
128-
.onAfterHandle({ as: 'global' }, ctx => {
128+
.state('logestic_timeStart', 0n)
129+
.onRequest(({ store }) => {
130+
store.logestic_timeStart = process.hrtime.bigint();
131+
})
132+
.onResponse({ as: 'global' }, ctx => {
129133
if (!this.httpLogging) {
130134
return;
131135
}
132136

133137
// get attributes, format and log
134-
let attrs = buildAttrs(ctx, this.requestedAttrs);
138+
const {
139+
store: { logestic_timeStart }
140+
} = ctx;
141+
let attrs = buildAttrs(ctx, this.requestedAttrs, logestic_timeStart);
135142
let msg = formatAttr.onSuccess(attrs);
136143
if (this.showLevel) {
137144
msg = `${colourLogType('http', this.logLevelColour)} ${msg}`;

src/presets/fancy.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ export default (options: LogesticOptions) =>
2121
...defaultOptions,
2222
...options
2323
})
24-
.use(['time', 'method', 'path'])
24+
.use(['time', 'method', 'path', 'duration'])
2525
.format({
26-
onSuccess({ time, method, path }) {
26+
onSuccess({ time, method, path, duration }) {
2727
const dateTime = chalk.gray(getDateTimeString(time!!));
2828
const methodPath = chalk.cyan(`${method} ${path}`);
2929

30-
return `${dateTime} ${methodPath}`;
30+
return `${dateTime} ${methodPath} ${duration}μs`;
3131
},
3232
onFailure({ request, datetime }) {
3333
const dateTime = getDateTimeString(datetime!!);

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type Attribute = {
1818
status?: any;
1919
referer?: string;
2020
userAgent?: string;
21+
duration?: bigint;
2122
};
2223

2324
export type ErrorAttribute = {

src/utils.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import chalk, { ChalkInstance } from 'chalk';
1414
* @param reqAttrs A map of attributes to be built
1515
* @returns The built attributes
1616
*/
17-
export const buildAttrs = (ctx: Context, reqAttrs: AttributeMap): Attribute => {
17+
export const buildAttrs = (
18+
ctx: Context,
19+
reqAttrs: AttributeMap,
20+
timeStart: bigint
21+
): Attribute => {
1822
const { request, path, body, query, set } = ctx;
1923

2024
let attrs: Attribute = {};
@@ -60,6 +64,11 @@ export const buildAttrs = (ctx: Context, reqAttrs: AttributeMap): Attribute => {
6064
case 'userAgent':
6165
attrs.userAgent = request.headers.get('user-agent') || '<user-agent?>';
6266
break;
67+
68+
case 'duration':
69+
const now = process.hrtime.bigint();
70+
attrs.duration = (now - timeStart) / 1000n;
71+
break;
6372
}
6473
}
6574

0 commit comments

Comments
 (0)