dom96 / jester

A sinatra-like web framework for Nim.
MIT License
1.56k stars 120 forks source link

Logging helpers #101

Open FedericoCeratto opened 7 years ago

FedericoCeratto commented 7 years ago

Jester could have some hooks to call logging functions:

dom96 commented 6 years ago

I suppose I should implement some logging formatting settings.

iffy commented 1 year ago

I would love for each request to have an ID, or at least for there to be an ID available that is scoped to a single request. This would let me attach arbitrary data to a request (such as logging information, or some app-specific data).

iffy commented 1 year ago

Would something like this be welcome? Maybe behind -d:useRequestIDs (or not)?

diff --git a/jester/request.nim b/jester/request.nim
index 9a1a83c..5746758 100644
--- a/jester/request.nim
+++ b/jester/request.nim
@@ -20,6 +20,9 @@ type
     patternParams: Option[Table[string, string]]
     reMatches: array[MaxSubpatterns, string]
     settings*: Settings
+    countId: uint64
+
+var nextRequestId: uint64 = 0

 proc body*(req: Request): string =
   ## Body of the request, only for POST.
@@ -121,6 +124,11 @@ proc formData*(req: Request): MultiData =
   if contentType.startsWith("multipart/form-data"):
     result = parseMPFD(contentType, req.body)

+proc id*(req: Request): uint64 =
+  ## Relatively unique id of this request.
+  ## It wraps around after going through full range of uint64
+  req.countId
+
 proc matches*(req: Request): array[MaxSubpatterns, string] =
   req.reMatches

@@ -182,9 +190,12 @@ proc cookies*(req: Request): Table[string, string] =
 #[ Protected procs ]#

 proc initRequest*(req: NativeRequest, settings: Settings): Request {.inline.} =
+  let id = nextRequestId
+  nextRequestId.inc()
   Request(
     req: req,
-    settings: settings
+    settings: settings,
+    countId: id,
   )
dom96 commented 1 year ago

HttpBeast already tracks request IDs, maybe you can simply expose them in Jester? https://github.com/dom96/httpbeast/blob/master/src/httpbeast.nim#L50

iffy commented 1 year ago

@dom96 Oh, that's great! So maybe just do this for useStdLib?

dom96 commented 1 year ago

sure