forked from webyrd/mediKanren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging2.rkt
54 lines (47 loc) · 1.54 KB
/
logging2.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#lang racket
(provide
lognew-info lognew-error requestid)
(require
racket/date
json
)
(define requestid (make-parameter -1))
(date-display-format 'iso-8601)
(define (lognew-message level msg)
(define t (current-seconds))
(define st-t (date->string (seconds->date t #f) #t))
(define jsexpr
(if (hash? msg)
(hash-set
(hash-set
(hash-set msg
'level (symbol->string level))
'requestid (requestid))
't st-t)
(hasheq
'msg msg
't st-t
'requestid (requestid)
'level (symbol->string level))))
(with-handlers ([exn:fail?
(λ (e)
#|
Racket json is very strict about what it accepts as a jsexpr. For example, this succeeds:
(jsexpr->string (hasheq 'foo "bar"))
but these both consider their input to be ill-formed and throw:
(jsexpr->string (hasheq "foo" "bar"))
(jsexpr->string (hasheq 'foo '#"bar"))
|#
(printf
"Caught exception in lognew-message when converting/printing jsexpr->string.\nlevel:\n~s\nmsg:\n~s\njsexpr:\n~s\nexception:\n~s\n"
level
msg
jsexpr
e))])
(displayln
(jsexpr->string jsexpr)))
(flush-output (current-output-port)))
(define (lognew-info msg)
(lognew-message 'info msg))
(define (lognew-error msg)
(lognew-message 'error msg))