jester icon indicating copy to clipboard operation
jester copied to clipboard

Logging helpers

Open FedericoCeratto opened this issue 8 years ago • 6 comments

Jester could have some hooks to call logging functions:

  • many web frameworks log the client IP address for security (and sometimes legal) reasons. A callback on incoming requests perhaps?
  • to easily correlate log messages related to the same request a short unique ID could be attached to requests. Unfortunately the logging module does not support adding extra variables.

FedericoCeratto avatar Feb 07 '17 21:02 FedericoCeratto

I suppose I should implement some logging formatting settings.

dom96 avatar May 26 '18 13:05 dom96

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 avatar Nov 01 '22 17:11 iffy

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,
   )
 

iffy avatar Nov 01 '22 18:11 iffy

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

dom96 avatar Nov 01 '22 18:11 dom96

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

iffy avatar Nov 01 '22 18:11 iffy

sure

dom96 avatar Nov 01 '22 20:11 dom96