<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
As part of my testing suite, I also needed a logger, since each client runs in its own thread. In a Java application, I might use something like log4j, but I didn't find anything like that in the documentation, so I put together this quick and dirty solution:<div><br></div><div><div>;;Trivial logger - replace with something more sophisticated.</div><div>(define (make-trivial-logger fname)</div><div> (lambda (lvl msg)</div><div> ;be sure not to overwrite the log file</div><div> (call-with-output-file fname #:exists 'append</div><div> (lambda (out)</div><div> (begin</div><div> (cond</div><div> ((eq? lvl 'info) (fprintf out "INFO "))</div><div> ((eq? lvl 'warn) (fprintf out "WARN "))</div><div> ((eq? lvl 'error) (fprintf out "ERROR "))</div><div> (else (fprintf out "UNKNOWN ")))</div><div> (fprintf out "~a" msg)</div><div> ;include time</div><div> (fprintf out " ~a~n" (date->string (seconds->date (current-seconds)) #t)))))))</div><div><br></div><div>It's not pretty because it opens the log file on each invocation. An alternative I considered is starting a logger thread and then using thread mailboxes to deliver messages to it, but then it's not clear to me how to go about closing the file and shutting down the logger thread when it's no longer needed.</div><div><br></div><div>But does a tool like this already exist? It seems like a common enough task.</div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div><br class="Apple-interchange-newline"><br class="khtml-block-placeholder"></div><div>"Interaction is the mind-body problem</div><div>of computing." --Philip L. Wadler</div><div><br></div><div><div><a href="http://www.gwoodhouse.com">http://www.gwoodhouse.com</a></div><div><a href="http://GregWoodhouse.ImageKind.com">http://GregWoodhouse.ImageKind.com</a></div><div><br></div></div><div><br class="khtml-block-placeholder"></div><div><br></div></span></span></div></body></html>