Last active
August 27, 2022 15:05
-
-
Save mikeananev/6424de1679ab7ef66acd079e2e51dce1 to your computer and use it in GitHub Desktop.
Clojure mulog example - logs and traces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[com.brunobonacci.mulog :as u]) | |
(require '[com.brunobonacci.mulog.flakes]) | |
;; | |
;; Start publisher | |
;; | |
(def publisher (u/start-publisher! {:type :console :pretty? true})) | |
(u/set-global-context! {:app-name "my-app" | |
:jvm-version (System/getProperty "java.version") | |
:jvm-name (System/getProperty "java.vm.name") | |
:os-name (System/getProperty "os.name")}) | |
(u/log ::example1 :qwe 123) | |
;; {:mulog/event-name :org.rssys.xchart/example1, | |
;; :mulog/timestamp 1661612008748, | |
;; :mulog/trace-id #mulog/flake "4kwvJN1tYwQeBG_RygsNztxQ7pQXTncq", | |
;; :mulog/namespace "org.rssys.xchart", | |
;; :app-name "my-app", | |
;; :jvm-name "OpenJDK 64-Bit Server VM", | |
;; :jvm-version "19-ea", | |
;; :os-name "Mac OS X", | |
;; :qwe 123} | |
;; | |
(u/trace ::example2 [:qwe 123] (/ 1 0)) | |
;; | |
;;{:mulog/event-name :org.rssys.xchart/example2, | |
;; :mulog/timestamp 1661612016766, | |
;; :mulog/trace-id #mulog/flake "4kwvJpuhGKWSU3_5SU9DemM8lfWP35lT", | |
;; :mulog/root-trace #mulog/flake "4kwvJpuhGKWSU3_5SU9DemM8lfWP35lT", | |
;; :mulog/duration 78487, | |
;; :mulog/namespace "org.rssys.xchart", | |
;; :mulog/outcome :error, | |
;; :app-name "my-app", | |
;; :exception #error { | |
;; :cause "Divide by zero" | |
;; :via | |
;; [{:type java.lang.ArithmeticException | |
;; :message "Divide by zero" | |
;; :at [clojure.lang.Numbers divide "Numbers.java" 190]}] | |
;; :trace | |
;; [[clojure.lang.Numbers divide "Numbers.java" 190] | |
;; [clojure.lang.Numbers divide "Numbers.java" 3911] | |
;; ..., | |
;; :jvm-name "OpenJDK 64-Bit Server VM", | |
;; :jvm-version "19-ea", | |
;; :os-name "Mac OS X", | |
;; :qwe 123} | |
;; | |
(defn read-file [filename] | |
(try | |
(+ 1 2) | |
(slurp filename) | |
(catch Exception e | |
(throw (ex-info "Can't open file" {:filename filename} e))))) | |
(defn load-user-data | |
[user] | |
(try | |
(read-file (str user ".txt")) | |
(catch Exception e | |
(throw (ex-info "Can't load user data" {:user user} e))))) | |
(defn read-transaction | |
[tx] | |
(try | |
(load-user-data (:user tx)) | |
(catch Exception e | |
(throw (ex-info "Can't read transaction" {:tx tx} e))))) | |
(def user "Vasya") | |
(u/with-context {:app/user user :mulog/root-trace #mulog/flake "4kwtHU4X108wYXVH-6t5Qd8L3-Jb1777"} | |
(try | |
(u/trace ::read-transaction-trace [] (read-transaction {:tx 1 :user user})) | |
(catch Exception e | |
#_(u/log :read-transaction-error :exception (->> e Throwable->map :via))))) | |
;; | |
;; Stop publisher | |
;; | |
(publisher) | |
;; | |
;;{:mulog/event-name :org.rssys.xchart/read-transaction-trace, | |
;; :mulog/timestamp 1661612069047, | |
;; :mulog/trace-id #mulog/flake "4kwvMsfys1aJLHyJvPyQdm-RqqRMFrbd", | |
;; :mulog/root-trace #mulog/flake "4kwtHU4X108wYXVH-6t5Qd8L3-Jb1777", | |
;; :mulog/duration 1278051, | |
;; :mulog/namespace "org.rssys.xchart", | |
;; :mulog/outcome :error, | |
;; :app-name "my-app", | |
;; :app/user "Vasya", | |
;; :exception #error { | |
;; :cause "Vasya.txt (No such file or directory)" | |
;; :via | |
;; [{:type clojure.lang.ExceptionInfo | |
;; :message "Can't read transaction" | |
;; :data {:tx {:tx 1, :user "Vasya"}} | |
;; :at [org.rssys.xchart$read_transaction invokeStatic "xchart.clj" 6]} | |
;; {:type clojure.lang.ExceptionInfo | |
;; :message "Can't load user data" | |
;; :data {:user "Vasya"} | |
;; :at [org.rssys.xchart$load_user_data invokeStatic "xchart.clj" 6]} | |
;; {:type clojure.lang.ExceptionInfo | |
;; :message "Can't open file" | |
;; :data {:filename "Vasya.txt"} | |
;; :at [org.rssys.xchart$read_file invokeStatic "xchart.clj" 6]} | |
;; {:type java.io.FileNotFoundException | |
;; :message "Vasya.txt (No such file or directory)" | |
;; :at [java.io.FileInputStream open0 "FileInputStream.java" -2]}] | |
;; :trace | |
;; [[java.io.FileInputStream open0 "FileInputStream.java" -2] | |
;; [java.io.FileInputStream open "FileInputStream.java" 219] | |
;; [java.io.FileInputStream <init> "FileInputStream.java" 158] | |
;; [clojure.java.io$fn__11617 invokeStatic "io.clj" 229] | |
;; [clojure.java.io$fn__11617 invoke "io.clj" 229] | |
;; [clojure.java.io$fn__11569$G__11523__11576 invoke "io.clj" 69] | |
;; [clojure.java.io$fn__11629 invokeStatic "io.clj" 258] | |
;; [clojure.java.io$fn__11629 invoke "io.clj" 254] | |
;; [clojure.java.io$fn__11569$G__11523__11576 invoke "io.clj" 69] | |
;; [clojure.java.io$fn__11591 invokeStatic "io.clj" 165] | |
;; [clojure.java.io$fn__11591 invoke "io.clj" 165] | |
;; [clojure.java.io$fn__11530$G__11519__11537 invoke "io.clj" 69] | |
;; [clojure.java.io$reader invokeStatic "io.clj" 102] | |
;; [clojure.java.io$reader doInvoke "io.clj" 86] | |
;; [clojure.lang.RestFn invoke "RestFn.java" 410] | |
;; [clojure.lang.AFn applyToHelper "AFn.java" 154] | |
;; [clojure.lang.RestFn applyTo "RestFn.java" 132] | |
;; [clojure.core$apply invokeStatic "core.clj" 669] | |
;; [clojure.core$slurp invokeStatic "core.clj" 7009] | |
;; [clojure.core$slurp doInvoke "core.clj" 7009] | |
;; [clojure.lang.RestFn invoke "RestFn.java" 410] | |
;; ...}, | |
;; :jvm-name "OpenJDK 64-Bit Server VM", | |
;; :jvm-version "19-ea", | |
;; :os-name "Mac OS X"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment