N00B Java Question

7 views
Skip to first unread message

Peter Wolf

unread,
Nov 24, 2009, 4:39:38 PM11/24/09
to clo...@googlegroups.com
Hi all,

Here is a N00B question, but I can not find the answer by Googling, or
reading Stuart's book. So, I assume that others will want to find this
FAQ in the future.

I am calling legacy code, and I need to set the level on the Java Logger.

In Java it would look like this

import java.util.logging.Logger
import java.util.logging.Level

Logger.getLogger("").setLevel(Level.WARNING)

What is the Clojure equivalent? In particular, what is the equivalent
of Level.WARNING?

Level.WARNING is a static field of Level, and is an instance of Level.
There is no getter method. I can't find anything about accessing static
fields in the docs.

Thanks in advance
Peter

Christophe Grand

unread,
Nov 24, 2009, 4:54:05 PM11/24/09
to clo...@googlegroups.com
Hi,


On Tue, Nov 24, 2009 at 10:39 PM, Peter Wolf <opu...@gmail.com> wrote:
Here is a N00B question, but I can not find the answer by Googling, or
reading Stuart's book.  So, I assume that others will want to find this
FAQ in the future.

I am calling legacy code, and I need to set the level on the Java Logger.

In Java it would look like this

  import java.util.logging.Logger
  import java.util.logging.Level

  Logger.getLogger("").setLevel(Level.WARNING)

What is the Clojure equivalent?  In particular, what is the equivalent
of Level.WARNING?

(.setLevel (Logger/getLogger "") Level/WARNING)
or (-> (Logger/getLogger "") (.setLevel Level/WARNING))

see http://clojure.org/java_interop#toc5
 
Christophe


--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

Sean Devlin

unread,
Nov 24, 2009, 4:54:50 PM11/24/09
to Clojure
Static field are accessed with the / operator

user=>(import (java.util.logging Logger Level))

user=>(let [a-logger (Logger/getLogger "")]
(.setLevel a-logger Level/WARNING))

Or, this could be chained as

user=>(.setLevel (Logger/getLogger "") Level/WARNING)

Hope this helps,
Sean

David Brown

unread,
Nov 24, 2009, 4:57:02 PM11/24/09
to clo...@googlegroups.com
On Tue, Nov 24, 2009 at 04:39:38PM -0500, Peter Wolf wrote:

>Here is a N00B question, but I can not find the answer by Googling, or
>reading Stuart's book. So, I assume that others will want to find this
>FAQ in the future.

I think it's also change a bit since the book.

>I am calling legacy code, and I need to set the level on the Java Logger.

Clojure doesn't really consider calling Java to be legacy. Interop
with Java is fully embraced.

>In Java it would look like this
>
> import java.util.logging.Logger
> import java.util.logging.Level
>
> Logger.getLogger("").setLevel(Level.WARNING)
>
>What is the Clojure equivalent? In particular, what is the equivalent
>of Level.WARNING?

(.setLevel (Logger/getLogger "") Level/WARNING)

Although there are other variants that will work.

David

Meikel Brandmeyer

unread,
Nov 24, 2009, 4:56:14 PM11/24/09
to clo...@googlegroups.com
Hi,

Am 24.11.2009 um 22:39 schrieb Peter Wolf:

> Logger.getLogger("").setLevel(Level.WARNING)

(.setLevel (Logger/getLogger "") Level/WARNING)

Methods: obj.method(args) => (.method obj args)
Static methods: Class.method(args) => (Class/method args)
Static members: Class.MEMBER => Class/MEMBER

Sincerely
Meikel

dannyo152

unread,
Nov 24, 2009, 8:51:44 PM11/24/09
to Clojure
Peter:

I recommend pages 60-62 in Stuart's book for demonstrating how to do
this. The notes about (. Math PI) or Math/PI (equivalent notations)
are very much to point.

To demonstrate:

;first the import
user=> (import '(java.util.logging Logger Level))
nil

;create a logger
user=> (def our-logger (. Logger getLogger ""))
#'user/our-logger


;set the level
user=> (. our-logger setLevel Level/WARNING)
nil

;check
user=> (. our-logger getLevel)
#<Level WARNING>

I only knew where to look because as a java-slinging N00B myself, I
had many questions about java interop.

Dan

opus111

unread,
Nov 25, 2009, 8:18:12 AM11/25/09
to Clojure
Wow! Many thanks for all the replies :-D

If there are any speech recognition enthusiasts out there, this code
is part of a Clojure example for Sphinx4.

http://cmusphinx.svn.sourceforge.net/viewvc/cmusphinx/trunk/sphinx4/src/scripts/clojure/ClojureTranscriber.clj?view=markup

Suggestions/improvements/additions welcome.

Thanks again
P

devender

unread,
Nov 25, 2009, 8:39:07 AM11/25/09
to Clojure
user=> (import '(java.util.logging Logger Level))
nil
user=> (def my-logger (Logger/getLogger "mylogger"))
#'user/my-logger
user=> (. my-logger setLevel Level/WARNING)
nil
user=> (. my-logger warning "this is a warning")
Nov 25, 2009 5:38:25 AM sun.reflect.NativeMethodAccessorImpl invoke0
WARNING: this is a warning
nil

On Nov 24, 1:39 pm, Peter Wolf <opus...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages