Change var in other namespace

218 views
Skip to first unread message

vitalyper

unread,
Nov 17, 2011, 5:17:12 PM11/17/11
to Clojure
clojure.tools.logging defines *logger-factory* and initializes it with
first logger implementation on the class path

(def ^{:doc
"An instance satisfying the impl/LoggerFactory protocol. Used
internally to
obtain an impl/Logger. Defaults to the value returned from impl/
find-factory."
:dynamic true}
*logger-factory*
(impl/find-factory))

In my own namespace I want to redefine *logger-factory* to log4j one.
Tried different variations (def, set!, etc) in 1.3.0 with no avail.
(ns my.foo
(:gen-class)
(:use
[clojure.tools.logging :only (*logger-factory* info debug)])
(:require
[clojure.string :as s1]
[clojure.tools.logging.impl :as log-impl])
)

(defn init-logging
"Force log4j factory for core tools logging"
[]
(def *logger-factory* (log-impl/log4j-factory)))
; CompilerException java.lang.IllegalStateException: *logger-factory*
already refers to: #'clojure.tools.logging/*logger-factory* in
namespace: infrared.common

Mark Rathwell

unread,
Nov 17, 2011, 10:04:26 PM11/17/11
to clo...@googlegroups.com
You rebind dynamic vars with binding, so your use would look something
like this:

(binding [*logger-factory* (log-impl/log4j-factory)]
(do-stuff-with-the-logger-factory-rebound))

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

Sean Corfield

unread,
Nov 17, 2011, 11:32:58 PM11/17/11
to clo...@googlegroups.com
Note: if you just want something that will execute at startup and
force _all_ logging to use log4j, instead of wrapping code in (binding
..) then you probably want something like this:

(ns your.namespace
(:require [clojure.tools.logging :as log])
(:require [clojure.tools.logging.impl :as impl]))

(alter-var-root (var log/*logger-factory*) (constantly (impl/log4j-factory)))

This is what we ended with in our code to ensure log4j was selected at
startup...

Sean

vitalyper

unread,
Nov 18, 2011, 9:35:00 AM11/18/11
to Clojure
Thanks, Sean. Exactly what I was looking for.
IMO, clojure.tools.logging could of made this easier: log
implementation from classpath by default with override by client when
needed.

On Nov 17, 11:32 pm, Sean Corfield <seancorfi...@gmail.com> wrote:
> Note: if you just want something that will execute at startup and
> force _all_ logging to use log4j, instead of wrapping code in (binding
> ..) then you probably want something like this:
>
> (ns your.namespace
>   (:require [clojure.tools.logging :as log])
>   (:require [clojure.tools.logging.impl :as impl]))
>
> (alter-var-root (var log/*logger-factory*) (constantly (impl/log4j-factory)))
>
> This is what we ended with in our code to ensure log4j was selected at
> startup...
>
> Sean
>
>
>
>
>
>
>
> On Thu, Nov 17, 2011 at 7:04 PM, Mark Rathwell <mark.rathw...@gmail.com> wrote:
> > You rebind dynamic vars with binding, so your use would look something
> > like this:
>
> > (binding [*logger-factory* (log-impl/log4j-factory)]
> >  (do-stuff-with-the-logger-factory-rebound))
>

Sean Corfield

unread,
Nov 18, 2011, 7:47:13 PM11/18/11
to clo...@googlegroups.com
On Fri, Nov 18, 2011 at 6:35 AM, vitalyper <vita...@yahoo.com> wrote:
> IMO, clojure.tools.logging could of made this easier: log
> implementation from classpath by default with override by client when
> needed.

I agree but when I brought the issue up, there didn't seem to be much
support for making it easier... Perhaps a ticket in JIRA might garner
some votes?
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

himangshu

unread,
Nov 13, 2012, 8:59:35 PM11/13/12
to clo...@googlegroups.com
this reply was god sent to me. I use tomcat and war to deploy my clojure code. the default thing was just not compiling. Although I do not have slf4j in my classpath. find logger was still giving slf4j and when it encountered first logging statement, the code used to crap out saying class not found exception
Reply all
Reply to author
Forward
0 new messages