Back

logf (clj)

(source)

macro

(logf level & args)

Examples

timbre
(ns taoensso.timbre.appenders.community.rotor-test
  (:require
    [clojure.test :refer :all]
    [clojure.java.io :as io]
    [clojure.set :as set]
    [taoensso.timbre :as timbre]
    [taoensso.timbre.appenders.community.rotor :as rotor]))

(def logfile "rotor-test.log")

(defn logname
  [i]
  (format "%s.%03d" logfile i))

(defn setup
  [n-logs]
  (timbre/merge-config!
   {:appenders {:rotor (rotor/rotor-appender
                        {:path logfile
                         :max-size 200
                         :backlog n-logs})}}))

(defn check-logs-present
  [n-logs]
  (let [f (io/file logfile)]
    (is (.exists f)))

(defn delete
  [logfile]
  (let [f (io/file logfile)]
    (try
      (.delete f)
      (catch java.io.FileNotFoundException e nil))))

(defn teardown
  [n-logs]
  (delete logfile)

(defn check-complete
  [n-logs n-lines]
  (is (= (set (range n-lines))
         (apply set/union
                (for [n (cons logfile (map logname (range 1 (inc n-logs))))]
                  (try
                    (with-open [rdr (io/reader n)]
                      (set (map (fn [line]
                                  (let [[_ x] (re-matches #".*testing: ([0-9]+)$" line)]
                                    (Integer/parseInt x)))
                                (line-seq rdr))))
                    (catch java.io.FileNotFoundException e (set []))))))))
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]))

;;; Log using format-style args
(defmacro logf*
  [context-src config level & args]
  `(with-a-frame-log-context ~context-src
     (timbre/log! ~level  :f ~args ~{:?line (@#'timbre/fline &form) :config config})))

(defmacro logf
  [context-src level & args]
  `(with-a-frame-log-context ~context-src
     (timbre/log! ~level  :f ~args ~{:?line (@#'timbre/fline &form)})))
palletops/log-config
(ns com.palletops.log-config.timbre.async-channel-test
  (:require
   [clojure.core.async :refer [<!! chan close! sliding-buffer]]
   [clojure.test :refer :all]
   [com.palletops.log-config.timbre
    :refer [domain-msg logging-threshold-fixture with-domain]]
   [com.palletops.log-config.timbre.async-channel :refer :all]
   [taoensso.timbre :as timbre]))

(deftest memory-sink-test
  (testing "default keys"
    (let [m (atom nil)
          c (chan (sliding-buffer 2))
          sink (memory-sink c m 3)]
      (timbre/merge-config!
       {:appenders {:async (make-async-channel-appender c {})}
        :middlware nil})
      (timbre/logf :debug "Hello %s" 'there {:x 1})
      (close! c)
      (is (= [{:throwable nil
               :args ["Hello %s" 'there {:x 1}]
               :ns "com.palletops.log-config.timbre.async-channel-test"}]
             (map #(dissoc % :hostname) (<!! sink))))))
  (testing "with domain"
    (let [m (atom nil)
          c (chan (sliding-buffer 2))
          sink (memory-sink c m 3)]
      (timbre/merge-config!
       {:appenders {:async (make-async-channel-appender
                            c {:kws [:hostname :ns :args :throwable
                                     :profile-stats :domain]})}
        :middleware [domain-msg]})
      (with-domain :d
        (timbre/logf :debug "Hello %s" 'there {:x 1}))
      (close! c)
      (is (= [{:throwable nil
               :args ["Hello %s" 'there {:x 1}]
               :ns "com.palletops.log-config.timbre.async-channel-test"
               :domain :d}]
             (map #(dissoc % :hostname) (<!! sink)))))))