Documented and fixed cljs support

parent cbaec916
......@@ -30,26 +30,56 @@ your consumers worry about formatting and appenders and all that jazz.
If your project is an application, add `Envelope` to your dependency graph:
```
[no.nsd.envelope "0.1.6"]
```clojure
[no.nsd/envelope "0.1.7"]
```
And use it like this:
```clojure
(ns no.nsd.your-app
(:require [no.nsd.envelope :as log))
(:require [no.nsd.envelope :as log])
(defn -main
[& args]
(log/init! {:service-name "your-app" :service-version "1.0.0"}
(log/debug "Hello NSD ELK format")))
[& args]
(log/init! {:service-name "your-app" :service-version "1.0.0"}
(log/debug "Hello NSD ELK format")))
```
`Timbre`'s macros `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `report`,
`spy`, and `with-context` are exposed as top-level macros in `Envelope`.
### ClojureScript
Usage from ClojureScript is a bit more involved. Instead of wrapping Tibre, in
ClojureScript, Envelope is just a support library for configuring Timbre to log
according to the NSD ELK schema. To use it, you will need to require both
Envolope and Timbre:
```clojure
[no.nsd/envelope "0.1.7"]
[com.taoensso/timbre "4.10.0"]
```
and then use it like this:
```clojure
(ns no.nsd.your-app
(:require [no.nsd.envelope :as envelope]
[taoensso.timbre :as log))
(defn -main
[& args]
(envelope/init! {:service-name "your-app" :service-version "1.0.0"}
(log/debug "Hello NSD ELK format")))
```
The `init!` and `merge-config!` functions will work like they do from Clojure.
Any othe functions/macros should be called directly from Timbre.
### Setup
While Envelope will work without any configuration, you will probably want to
......
(defproject no.nsd/envelope "0.1.6"
(defproject no.nsd/envelope "0.1.7"
:description "A thin wrapper around taoensso.timbre with built in settings for logging to NSD's ELK stack"
:url "https://gitlab.nsd.no/clojure/envelope"
:license {:name "Eclipse Public License"
......
(ns no.nsd.envelope
(:require [clojure.string :as str]
[clojure.data.json :as json]
[camel-snake-kebab.core :refer :all]
[taoensso.timbre :as timbre]
[camel-snake-kebab.core :refer [->camelCase]]
#?(:clj [clojure.data.json :as json])
#?(:clj [taoensso.timbre.tools.logging :as cljlog])))
(defonce default-config {:log-to-elk true
......@@ -13,6 +13,12 @@
(defonce envelope-config (atom default-config))
(defn write-json
[data]
#?(:clj (json/write-str data)
:cljs (.stringify js/JSON (clj->js data))))
(defn nsd-logstash-format-fn [config data]
"NSD format, compliant with https://gitlab.nsd.no/nsd-commons/log-schema"
(let [{:keys [level ?err msg_ ?ns-str ?file hostname_
......@@ -30,34 +36,34 @@
?err
(assoc :error {:stack (timbre/stacktrace ?err {:stacktrace-fonts {}})}))]
(json/write-str (select-keys (reduce #(assoc %1 (->camelCase %2) (get context %2))
output-data
(keys context))
["@timestamp"
:serviceName
:serviceVersion
:schemaVersion
:levelName
:message
:serviceStack
:xRequestId
:thread
:process
:file
:line
:userId
:loggerName
:tags
:host
:port
:data
:url
:responseTime
:statusCode
:method
:error
:request
:response]))))
(write-json (select-keys (reduce #(assoc %1 (->camelCase %2) (get context %2))
output-data
(keys context))
["@timestamp"
:serviceName
:serviceVersion
:schemaVersion
:levelName
:message
:serviceStack
:xRequestId
:thread
:process
:file
:line
:userId
:loggerName
:tags
:host
:port
:data
:url
:responseTime
:statusCode
:method
:error
:request
:response]))))
......@@ -112,9 +118,9 @@
(def ^:private timbre-symbols
['trace 'debug 'info 'warn 'error 'fatal 'report 'spy 'with-context])
(defmacro timbre-alias ^:private [sym]
(defmacro timbre-intern [sym]
`(let [timbre-sym# (resolve (symbol (str "timbre/" ~sym)))]
(intern *ns* (with-meta ~sym (meta timbre-sym#)) (deref timbre-sym#))))
(doseq [sym timbre-symbols]
(timbre-alias sym))
#?(:clj (doseq [sym timbre-symbols]
(timbre-intern sym)))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment