com/github/kyleburton/sandbox/log4j.clj

Line Coverage Rate: 0.0
     Lines Covered: 0
Branch Coverage Rate: 0.0
    Branches Covered: 0
1;; TODO: switch to commons-logging
2;; commons-logging.properties
3(ns com.github.kyleburton.sandbox.log4j
4  (:import
5   (org.apache.log4j Logger PropertyConfigurator)
6   (org.apache.commons.logging Log LogFactory))
7  (:require [com.github.kyleburton.sandbox.utils :as kutils])
8  (:use [clojure.contrib.str-utils :as str]))
9
10(def *log4j-properties* (str (kutils/$HOME ".clojure/log4j.properties")))
11
12(def *configured* (atom false))
13
14(defn reset-configuration! []
15  (reset! *configured* false))
16
17;; (reset-configuration!)
18
19(defn ensure-configured []
20  (if @*configured*
21    true
22    (do
23      (PropertyConfigurator/configureAndWatch *log4j-properties* 1000)
24      (reset! *configured* true))))
25
26(defn logger [category]
27  ;; TODO: move the ensure out of here, initialization should be a
28  ;; concern handled outside this moudle...
29  (ensure-configured)
30  (Logger/getLogger category))
31
32;; TODO: wrap the expansions with if(Log.isDebug){...} to avoid
33;; argument evaluation when logging is otherwise disabled, keep that
34;; hidden in behind the macros...
35(defmacro LOG []
36  `(do (def ~'*log* (logger (.toString ~'*ns*)))
37       (defn ~'log-fatal [& args#] (.fatal ~'*log* (str args#)))
38       (defn ~'log-error [& args#] (.error ~'*log* (str args#)))
39       (defn ~'log-warn  [& args#] (.warn  ~'*log* (str args#)))
40       (defn ~'log-info  [& args#] (.info  ~'*log* (str args#)))
41       (defn ~'log-debug [& args#] (.debug ~'*log* (str args#)))
42       (defn ~'log-trace [& args#] (.trace ~'*log* (str args#)))))
43