-
-
Save usametov/ff5b0cc1f1486edb3fe7c2a71811a1d6 to your computer and use it in GitHub Desktop.
Custom log4j appender in Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns woof.core | |
(:gen-class) | |
(:require [clojure.tools.logging :as log]) | |
(:import StringAppender)) | |
(defn -main | |
"I don't do a whole lot ... yet." | |
[& args] | |
(log/info "Hello, world!") | |
(println "Done") | |
(println (StringAppender/getLogs))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log4j.rootLogger=INFO, console | |
log4j.logger.example=DEBUG | |
log4j.appender.console=StringAppender | |
log4j.appender.console.layout=org.apache.log4j.PatternLayout | |
log4j.appender.console.layout.ConversionPattern=%-5p %c: %m%n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defproject woof "0.1.0-SNAPSHOT" | |
:description "FIXME: write description" | |
:url "http://example.com/FIXME" | |
:license {:name "Eclipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"} | |
:dependencies [[org.clojure/clojure "1.8.0"] | |
[org.slf4j/slf4j-log4j12 "1.7.25"] | |
[org.clojure/tools.logging "0.4.0"]] | |
:main ^:skip-aot woof.core | |
:aot [woof.woof] | |
:target-path "target/%s" | |
:profiles {:uberjar {:aot :all}}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns woof.woof | |
(:import (org.apache.log4j.spi LoggingEvent)) | |
(:gen-class | |
:extends org.apache.log4j.AppenderSkeleton | |
:name StringAppender | |
:methods [^:static [getLogs [] String]])) | |
(def logs (atom "")) | |
(defn -append [this ^LoggingEvent logging-event] | |
(swap! logs str (.getMessage logging-event) "\n")) | |
(defn -close [this]) | |
(defn -requiresLayout [this] false) | |
(defn -getLogs [] | |
@logs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment