Skip to content

Commit c1e5d08

Browse files
Merge pull request #19 from cybercoder-naj/feat/on-request
Feat: Optional OnRequest logging
2 parents a92c7a7 + 5009e80 commit c1e5d08

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# CHANGELOG
2+
## [1.1.0] - 31-03-2024
3+
### Added
4+
- Optional `onRequest` logging formatter for hooking onto onRequest. ([#13](https://github.com/cybercoder-naj/logestic/issues/13))
5+
26
## [1.0.1] - 30-03-2024
37
### Added
48
- Type-safety to onSuccess method, based on attributes passed on `use`. ([#6](https://github.com/cybercoder-naj/logestic/issues/6))

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "logestic",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"author": "Nishant Aanjaney Jalan <cybercoder.nishant@gmail.com>",
55
"description": "An advanced and customisable logging library for ElysiaJS",
66
"keywords": [

src/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,16 @@ export class Logestic<K extends keyof Attribute = keyof Attribute> {
129129
format(this: Logestic, formatAttr: Callback<K>) {
130130
return this.build()
131131
.state('logestic_timeStart', 0n)
132-
.onRequest(({ store }) => {
132+
.onRequest(({ store, request }) => {
133133
store.logestic_timeStart = process.hrtime.bigint();
134+
135+
if (formatAttr.onRequest) {
136+
let msg = formatAttr.onRequest(request);
137+
if (this.showLevel) {
138+
msg = `${colourLogType('http', this.logLevelColour)} ${msg}`;
139+
}
140+
this.log(msg);
141+
}
134142
})
135143
.onResponse({ as: 'global' }, ctx => {
136144
if (!this.httpLogging) {

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type Preset = 'common' | 'fancy';
3636
* `Callback` is an object that contains functions to format successful and failed logs.
3737
*/
3838
export type Callback<K extends keyof Attribute> = {
39+
onRequest?: (attr: Request) => string;
3940
onSuccess: (attr: Pick<Attribute, K>) => string;
4041
onFailure: (attr: ErrorAttribute) => string;
4142
};

0 commit comments

Comments
 (0)