Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

with-logger macro #6

Closed
wants to merge 5 commits into from

3 participants

@ikitommi

One can override the default namespace used in logging with a with-logger macro (or binding *logger-ns*).

(deftest logp-macro-with-logger
  (with-logger "kikka"
    (info "kukka"))
  (is (= ["kikka"
          :info
          nil
          "kukka"]
        (peek @*entries*))))

I have a case where I need to log explicitly to different logs, this seems to be nice way to do it.

Added also few tests for the logp and logf -based macro loggings.

regards,

Tommi

@seancorfield
Collaborator

Hi Tommi, thanks for the suggestions but Clojure Contrib projects cannot accept pull requests per http://clojure.org/contributing

Pull requests are often a good way to provide a basis for discussion around possible new features that leads to tickets in JIRA and patches from folks with a signed Contributor's Agreement on file. You can find links to JIRA on this page for each Contrib project: http://dev.clojure.org/display/design/Where+Did+Clojure.Contrib+Go

I'll leave this open for the project lead to review and discuss with you.

@ikitommi

hi,

and thanks for explaining the process, didn't know that. I'll wait for the review and if the changes are good and interesting enough, I'll use the normal process to get them in.

Added also test coverage.

br,

Tommi

@ataggart
Collaborator

The proposed solution changes the log-ns for every log invocation in the call tree, not just for the invocations within the lexical scope of with-logger. The log macro already allows for specifying the log-ns.

@ataggart ataggart closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 14, 2013
  1. @ikitommi

    ignore eclipse & built files

    ikitommi authored
  2. @ikitommi
  3. @ikitommi
  4. @ikitommi

    typo

    ikitommi authored
Commits on Feb 15, 2013
  1. @ikitommi

    better branch coverage

    ikitommi authored
This page is out of date. Refresh to see the latest.
View
4 .gitignore
@@ -1 +1,5 @@
target
+.classpath
+.settings
+.project
+*.jar
View
17 src/main/clojure/clojure/tools/logging.clj
@@ -35,6 +35,11 @@
agent. Defaults to nil. See log* for details." :dynamic true}
*force* nil)
+(def ^{:doc
+ "Overrides the namespace used in logging. Defaults to nil. See with-log for details."
+ :dynamic true}
+ *logger-ns* nil)
+
(defn log*
"Attempts to log a message, either directly or via an agent; does not check if
the level is enabled.
@@ -66,7 +71,7 @@
([level message]
`(log ~level nil ~message))
([level throwable message]
- `(log ~*ns* ~level ~throwable ~message))
+ `(log (or *logger-ns* ~*ns*) ~level ~throwable ~message))
([logger-ns level throwable message]
`(log *logger-factory* ~logger-ns ~level ~throwable ~message))
([logger-factory logger-ns level throwable message]
@@ -81,7 +86,7 @@
[level x & more]
(if (or (instance? String x) (nil? more)) ; optimize for common case
`(log ~level (print-str ~x ~@more))
- `(let [logger# (impl/get-logger *logger-factory* ~*ns*)]
+ `(let [logger# (impl/get-logger *logger-factory* (or *logger-ns* ~*ns*))]
(if (impl/enabled? logger# ~level)
(let [x# ~x]
(if (instance? Throwable x#) ; type check only when enabled
@@ -95,7 +100,7 @@
[level x & more]
(if (or (instance? String x) (nil? more)) ; optimize for common case
`(log ~level (format ~x ~@more))
- `(let [logger# (impl/get-logger *logger-factory* ~*ns*)]
+ `(let [logger# (impl/get-logger *logger-factory* (or *logger-ns* ~*ns*))]
(if (impl/enabled? logger# ~level)
(let [x# ~x]
(if (instance? Throwable x#) ; type check only when enabled
@@ -194,6 +199,12 @@
(log-stream ~err-level ~logger-ns))]
~@body))))
+(defmacro with-logger
+ "Overrides the default namespace to be used in logging."
+ [^String logger-ns & body]
+ (if (and logger-ns (seq body))
+ `(binding [*logger-ns* ~logger-ns]
+ ~@body)))
;; level-specific macros
View
54 src/test/clojure/clojure/tools/test_logging.clj
@@ -397,3 +397,57 @@
warnf :warn
errorf :error
fatalf :fatal)))
+
+(deftest logp-macro
+ (info "kukka")
+ (is (= ["clojure.tools.test-logging"
+ :info
+ nil
+ "kukka"]
+ (peek @*entries*))))
+
+(deftest logf-macro
+ (infof "kukka")
+ (is (= ["clojure.tools.test-logging"
+ :info
+ nil
+ "kukka"]
+ (peek @*entries*))))
+
+(deftest logp-macro-with-logger
+ (with-logger "kikka"
+ (info "kukka"))
+ (is (= ["kikka"
+ :info
+ nil
+ "kukka"]
+ (peek @*entries*))))
+
+(deftest logf-macro-with-logger
+ (with-logger "kikka"
+ (infof "kukka"))
+ (is (= ["kikka"
+ :info
+ nil
+ "kukka"]
+ (peek @*entries*))))
+
+(deftest logp-macro-exception-with-logger
+ (let [exception (RuntimeException. "bang")]
+ (with-logger "kikka"
+ (info exception "kukka"))
+ (is (= ["kikka"
+ :info
+ exception
+ "kukka"]
+ (peek @*entries*)))))
+
+(deftest logf-macro-exception-with-logger
+ (let [exception (RuntimeException. "bang")]
+ (with-logger "kikka"
+ (infof exception "kukka"))
+ (is (= ["kikka"
+ :info
+ exception
+ "kukka"]
+ (peek @*entries*)))))
Something went wrong with that request. Please try again.