-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.clj
33 lines (27 loc) · 888 Bytes
/
file.clj
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
(ns dialog.output.file
"Log appender which writes events to a file.
**NOTE:** this output is NOT safe for use when multiple processes are writing
to the same file!"
(:require
[clojure.java.io :as io])
(:import
java.io.BufferedWriter))
(defn writer
"Construct a file event writer function.
Output options should include:
- `:path`
String path to the log file to write to."
[output]
(when-not (string? (:path output))
(throw (IllegalArgumentException.
(str "File output writer requires a string :path, got: "
(pr-str (:path output))))))
(let [path (:path output)
lock (Object.)]
(io/make-parents path)
(fn write-event
[_event message]
(locking lock
(with-open [^BufferedWriter out (io/writer path :append true)]
(.write out (str message))
(.newLine out))))))