▼ |  | Unstable: May Change Without Warning

1 | Guidelines for Developing unstable Libraries
2 | Automata: Compiling State Machines
3 | Bytes
4 | Contracts
5 | Contracts for Macro Subexpressions
6 | Debugging
7 | Definitions
8 | Errors
9 | Filesystem
10 | Find
11 | Flonums
12 | Futures
13 | Functions
14 | Hash Tables
15 | Interface-Oriented Programming for Classes
16 | Lazy Require
17 | Lists
18 | Logging
19 | Macro Testing
20 | Mark Parameters
21 | Match
22 | Open place expressions
23 | Options
24 | Parameter Groups
25 | Pretty-Printing
26 | Re-Contracting Identifiers
27 | Sandbox
28 | Sequences
29 | Strings
30 | Structs
31 | Struct Printing
32 | Syntax
33 | Temporal Contracts: Explicit Contract Monitors
34 | Unix Domain Sockets
35 | 2D Syntax
36 | GUI Libraries

On this page:

with-logging-to-port
with-intercepted-logging
start-recording
stop-recording

5.3.6
top ← prev up next →

### 18`` Logging

Vincent St-Amour <stamourv@racket-lang.org>

This library is unstable; compatibility will not be maintained. See Unstable: May Change Without Warning for more information.

(require unstable/logging)

This module provides tools for logging.

> > procedure

> > (with-logging-to-port port proc log-spec ...) → any

> port : output-port?
> proc : (-> any)
> log-spec : (or/c 'fatal 'error 'warning 'info 'debug symbol? #f)

Runs proc, outputting any logging that would be received by (make-log-receiver (current-logger) log-spec ...) to port. Returns whatever proc returns.

Example:

> > (let ([my-log (open-output-string)])
> (with-logging-to-port my-log
> (lambda ()
> (log-warning "Warning World!")
> (+ 2 2))
> 'warning)
> (get-output-string my-log))

> "Warning World!\n"

> > procedure

> > (with-intercepted-logging |  | interceptor |  |  |  |
> > |  | proc |  |  |  |
> > |  | log-spec ...) |  | → |  | any

> interceptor

> :

> (-> (vector/c
> (or/c 'fatal 'error 'warning 'info 'debug)
> string?
> any/c
> (or/c symbol? #f))
> any)

> proc : (-> any)
> log-spec : (or/c 'fatal 'error 'warning 'info 'debug symbol? #f)

Runs proc, calling interceptor on any log message that would be received by (make-log-receiver (current-logger) log-spec ...). interceptor receives the entire log vectors (see Receiving Logged Events) as arguments. Returns whatever proc returns.

Example:

> > (let ([warning-counter 0])
> (with-intercepted-logging
> (lambda (l)
> (when (eq? (vector-ref l 0)
> 'warning)
> (set! warning-counter (add1 warning-counter))))
> (lambda ()
> (log-warning "Warning!")
> (log-warning "Warning again!")
> (+ 2 2))
> 'warning)
> warning-counter)

> 2

A lower-level interface to logging is also available.

> > procedure

> > (start-recording log-spec ...) → listener?

> log-spec : (or/c 'fatal 'error 'warning 'info 'debug symbol? #f)

> > procedure

> > (stop-recording listener)

> →

> (listof (vector/c (or/c 'fatal 'error 'warning 'info 'debug)
> string?
> any/c
> (or/c symbol? #f)))

> listener : listener?

start-recording starts recording log messages matching the given log-spec (see make-log-receiver for how log-spec is interpreted). Messages will be recorded until stopped by passing the returned listener object to stop-recording. stop-recording will then return a list of the log messages that have been reported.

Examples:

> (define l (start-recording 'warning))

> > (log-warning "1")

> > (log-warning "2")

> > (stop-recording l)

> '(#(warning "1" #<continuation-mark-set> #f)
> #(warning "2" #<continuation-mark-set> #f))

top ← prev up next →