我正在编写一个需要记录信息的Racket程序,但我想将日志存储在文件中。我的第一个尝试是使用“ with-logging-to-port”并使用“ open-output-file”创建输出端口。
#lang racket
(require racket/logging)
(define (identity/log x)
(log-info "returning ~a" x) x)
(with-logging-to-port (open-output-file "testing.txt")
(? () (identity/log 4)) 'info)
Run Code Online (Sandbox Code Playgroud)
但是,当我之后打开文件时,它是空白的!另外,我不能多次运行它,因为“ open-output-file”给我一个错误,指出该文件已存在。
小智 1
使用标志“append”打开文件。例如:
(open-output-file "testing.txt" #:exists 'append )
Run Code Online (Sandbox Code Playgroud)