Of course, a complete web app needs to write logs. Here I use slf4j + logback.
First, add two dependencies to the project. CLJ file in the my-website directory: Tools. Logging & logback-classic
(defproject my-website "0.1.0-SNAPSHOT" :description "FIXME: write this!" :dependencies [[org.clojure/clojure "1.4.0"] [com.novemberain/monger "1.4.1"] [org.clojure/tools.logging "0.2.4"] [ch.qos.logback/logback-classic "1.0.7"] [noir "1.3.0-beta3"]] :main my-website.server)
Create a logback. xml file under SRC.
<configuration debug="false"> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %-10contextName %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>/opt/web.log</file> <append>true</append> <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <logger name="clojure-example-logback-integration" level="DEBUG"/> <root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> </root></configuration>
Create the log. CLJ file in the src/my_website directory. The Code is as follows :( note that the my-website.log namespace is defined here)
(ns my-website.log (:require [clojure.tools.logging :as log]))(defn set-log-level! "Pass keyword :error :info :debug" [level] (let [logger (logimpl/get-logger log/*logger-factory* "clojure-logback-integration")] (case level :debug (.setLevel logger Level/DEBUG) :info (.setLevel logger Level/INFO) :error (.setLevel logger Level/ERROR))))(defmacro debug [& args] `(log/debug (str ~@args)))(defmacro info [& args] `(log/info (str ~@args)))(defmacro warn [e & args] `(log/warn (str ~@args) ~e))(defmacro warn [& args] `(log/warn (str ~@args)))(defmacro error [& args] `(log/error (str ~@args)))(defmacro error [e & args] `(log/error ~e (str ~@args)))
Now you can use Log in server. CLJ :( note that the my-website.log namespace is introduced here)
(ns my-website.server (:require [noir.server :as server]) (:require [my-website.log :as log]))(server/load-views-ns 'my-website.views)(server/load-views-ns 'my-website.rest.test)(defn -main [& m] (log/info "enter -main") (let [mode (keyword (or (first m) :dev)) port (Integer. (get (System/getenv) "PORT" "8080"))] (server/start port {:mode mode :ns 'my-website})))
However, unfortunately, this logback currently supports bugs, and it is always reported that logimpl cannot be found.
I have already reported issue. Check when to fix it.
This article will be updated after the problem is resolved.