How to publish logs to the file in a json format ? #86
Replies: 2 comments 1 reply
-
Hi, Currently we only have a Console JSON publisher using the https://cljdoc.org/d/com.brunobonacci/mulog/0.8.2/doc/publishers/advanced-console-publisher The solution you proposed doesn't work because your transformation function returns a string (JSON formatted) while the publisher expect a map with the event. However, you can write your custom publisher. It is very easy, the process is explained in details here: https://cljdoc.org/d/com.brunobonacci/mulog/0.8.2/doc/howtos/how-to-write-custom-publishers Here is an example how a JSON file publishers would look like
(ns your.namespace
(:require [com.brunobonacci.mulog :as u]
[com.brunobonacci.mulog.publisher :as p]
[com.brunobonacci.mulog.buffer :as rb]
[com.brunobonacci.mulog.common.json :as json]
[clojure.java.io :as io]))
(deftype JsonFilePublisher [config ^java.io.Writer filewriter buffer transform]
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_]
buffer)
(publish-delay [_]
200)
(publish [_ buffer]
;; items are pairs [offset <item>]
(doseq [item (transform (map second (rb/items buffer)))]
(.write filewriter ^String (str (json/to-json item) \newline)))
(.flush filewriter)
(rb/clear buffer))
java.io.Closeable
(close [_]
(.flush filewriter)
(.close filewriter)))
(defn json-file-publisher
[{:keys [filename transform] :as config}]
{:pre [filename]}
(let [filename (io/file filename)]
;; make parent dirs
(when-let [path (.getParentFile filename)]
(.mkdirs path))
(JsonFilePublisher.
config
(io/writer filename :append true)
(rb/agent-buffer 10000)
(or transform identity)))) Then to use it you would do as follow ;; create and start your publisher
(def my-publisher
(u/start-publisher!
{:type :inline
:publisher (json-file-publisher
{:filename "/tmp/mulog.json"})}))
(u/log ::test :foo "bar") Sorry if the above solution contain some errors I wrote it on an iPad while on holiday, and I don't have any way to test it before sending it to you. I hope it helps, if you have more difficulties, I'll be happy to help with a fully working example when next week when I'm back home. Regards |
Beta Was this translation helpful? Give feedback.
-
Thanks for the prompt response @BrunoBonacci . Shall try this and let you know incase of any errors or blockers. 🙏 |
Beta Was this translation helpful? Give feedback.
-
I am new to clojure and have been trying to add logs to a file in json format, following is what I have tried
Whenever we use identity it logs the hash map correctly in the log file, but when I try transforming the hash map to a json, it adds a lot of characters(attached the log file). This is most likely my lack of understanding, on how transforms works, so any help would be appreciated 🙏
mulog_events_new_f1.log
Beta Was this translation helpful? Give feedback.
All reactions