log4j: Change format of loggers configured in another library
Using clojure, I've been able to successfully setup log4j very simply by using this log4j.properties file, and including log4j in my classpath.
# BEGIN log4j.properties
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d{MMdd HHmmss SSS} %5p %c [%t] %m\n
log4j.rootLogger=DEBUG, STDOUT
Then after :use'ing clojure.contrib.logging, I'm able to print a statement with the desired formatting as expected like so:
(info "About to print this")
(debug "This is debug-level")
My question is how to achieve a consistent formatting for logging statements made from loggers configured in other libraries. I thought I could find existing loggers using org.apache.log4j.LogManager.getCurrentLoggers() and change the PatternLayouts there, but I'm not able to iterate over that enumeration in clojure, as I get the following error:
Dont know how to create ISeq from: java.util.Vector$1
I assu开发者_开发百科me this is possible somehow, and probably very simply. How?
Thanks much.
Use enumeration-seq
instead of seq
:
(enumeration-seq (org.apache.log4j.LogManager/getCurrentLoggers))
For the curious, org.apache.log4j.LogManager.getCurrentLoggers()
returns a java.util.Enumeration
; seq
doesn't know how to operate on that, but enumeration-seq
does.
A simpler case not involving logging:
(seq (.elements (java.util.Vector. [1 2 3]))
; => throws an exception
(enumeration-seq (.elements (java.util.Vector. [1 2 3])))
; => returns (1 2 3)
精彩评论