Back
with-context (clj)
(source)macro
(with-context context & body)
Executes body so that given arbitrary data will be passed (as `:context`)
to appenders for any enclosed logging calls.
(with-context
{:user-name "Stu"} ; Will be incl. in data dispatched to appenders
(info "User request"))
See also `with-context+`.
Examples
yapsterapp/a-frame
(ns a-frame.log
"overrides of the timbre logging macros
which set a context from an a-frame
interceptor-context key, or app-ctx key"
#?(:cljs (:require-macros [a-frame.log]))
(:require
[taoensso.timbre :as timbre]
[a-frame.schema :as af.schema]))
(timbre/with-context (get ~context-src af.schema/a-frame-log-ctx)
~@body)))
zalando-incubator/chisel
(ns chisel.logging-test
(:require [clojure.test :refer :all]
[taoensso.timbre :as timbre]
[chisel.logging :as log]
[chisel.correlation-ctx :as ctx]
[io.pedestal.log :as pedestal-log]))
(testing "When captured, request context is propagated"
(let [log-args (atom nil)
log-level (atom nil)
context {:key "value"}]
(with-redefs [pedestal-log/log (fn [keyvals level]
(reset! log-level level)
(reset! log-args keyvals))]
(ctx/with-context {::ctx/ctx context}
(log/debug :msg "test-message")
(is (= :debug @log-level))
(is (= context (:context @log-args)))))))
(testing "When specified, filtering is applied to the context"
(let [log-args (atom nil)
log-level (atom nil)]
(with-redefs [pedestal-log/log (fn [keyvals level]
(reset! log-level level)
(reset! log-args keyvals))]
(log/set-context-filter! #(dissoc % :private))
(ctx/with-context {::ctx/ctx {:public "public-value"
:private "private-value"}}
(prn :ctx ctx/*ctx*)
(log/debug :msg "test-message")
(is (= :debug @log-level))
palletops/log-config
(ns com.palletops.log-config.slf4j-test
(:require
[clojure.test :refer :all]
[taoensso.timbre :refer [example-config info log]]
[com.palletops.log-config.tools-logging :refer :all]
[com.palletops.log-config.timbre
:refer [add-var format-with-context]]
[com.palletops.log-config.timbre.tools-logging
:refer [make-tools-logging-appender with-context]]
[com.palletops.log-config.tools-logging
:refer [logging-to-string]]))
(deftest ^:slf4j with-context-test
(testing "with context aware formatter"
(let [config (merge example-config
{:fmt-output-fn format-with-context
:appenders {:slf4j (make-tools-logging-appender {})}})]
(testing "without context setting middleware"
(is (not
(re-find
#":m 1\n$"
(logging-to-string
(with-context {:m 1}
(log config :info "something")))))
"log message doesn't contain context"))
(testing "with context setting middleware"
(is (re-find
#":m 1"
(logging-to-string
(with-context {:m 1}
(log (assoc config :middleware [(add-var :context #'v)])
:info "something"))))
"log message contains context")))))
kronkltd/jiksnu
(ns jiksnu.modules.web.routes.home-routes
(:require [cemerick.friend :as friend]
[ciste.core :refer [with-context]]
[ciste.sections.default :refer [show-section]]
[jiksnu.modules.core.actions.site-actions :as site]
[jiksnu.modules.core.model.user :as model.user]
[jiksnu.modules.web.core :refer [jiksnu]]
[jiksnu.modules.http.resources :refer [add-group! defresource defgroup resources]]
[jiksnu.modules.web.helpers :refer [angular-resource as-collection-resource]]
[octohipster.mixins :as mixin]
[taoensso.timbre :as timbre]))
(defresource api :whoami
:name "Whoami"
:url "/whoami"
:mixins [as-collection-resource]
:exists? (fn [ctx]
(let [user (model.user/get-user (:current (friend/identity (:request ctx))))]
{:data (with-context [:http :as] (show-section user))})))