Permalink
Browse files

adding logo and start to log files

  • Loading branch information...
1 parent 093c501 commit 4cf9699ae84a5f582b3a38d2148234a647ce3211 @slagyr slagyr committed Sep 1, 2012
Showing with 167 additions and 3 deletions.
  1. +2 −3 TODO.md
  2. +90 −0 api/spec/hyperion/log_spec.clj
  3. +75 −0 api/src/hyperion/log.clj
  4. BIN hyperion_logo.png
View
@@ -1,7 +1,7 @@
+* Logging: For debugging. For runtime monitoring. For warning of dangerous actions.
* Use Around Save/Around Load callbacks to implement associations
* Embedded record for document datastores
* Website?
-* Documentation
* Indexes:
* Riak: currently indexes ALL fields. That's inefficient.
Yet it can't search efficiently without index. If defentity allowed
@@ -13,7 +13,6 @@
they exist. Indexes are not needed at runtime.
* GAE: Indexes are handled externally. Would be useless.
* SQL: Same as Mongo I suppose.
-* Logging: For debugging. For runtime monitoring. For warning of dangerous actions.
* Ragtime integration for migrations (https://github.com/weavejester/ragtime)
-* Datomic
+* Datomic - maybe?
@@ -0,0 +1,90 @@
+(ns hyperion.log-spec
+ (:require [speclj.core :refer :all ]
+ [hyperion.log :refer :all ]))
+
+(deftype FakeLogger [received level]
+ Logger
+ (log! [this level message]
+ (swap! received conj {:level level :message message}))
+ (set-level [this level-to-set]
+ (reset! level level-to-set)))
+
+(defn new-fake-logger []
+ (FakeLogger. (atom []) (atom nil)))
+
+(describe "Log"
+
+ (it "default logger exists"
+ (should-not= nil *logger*)
+ (should= java.util.logging.Logger (class *logger*))
+ (should= "hyperion" (.getName *logger*))
+ (should= java.util.logging.Level/WARNING (.getLevel *logger*)))
+
+ (context "to string"
+
+ (with stream (java.io.ByteArrayOutputStream.))
+ (before (remove-all-handlers!))
+
+ (it "logs are formatted"
+ (let [handler (java.util.logging.StreamHandler. @stream FORMATTER)]
+ (add-handler! handler)
+ (warn "foo")
+ (.flush handler)
+ (should= "WARNING blah" (String. (.toByteArray @stream)))))
+
+ )
+
+ (it "doesn't crash when logging"
+ (should-not-throw (debug "foo")))
+
+ (context "with fake logger"
+ (with fake-logger (new-fake-logger))
+ (around [it]
+ (binding [*logger* @fake-logger]
+ (it)))
+
+ (it "logs info statements"
+ (info "My information")
+ (should= "My information" (:message (first @(.received @fake-logger))))
+ (should= java.util.logging.Level/INFO (:level (first @(.received @fake-logger)))))
+
+ (it "logged objects turn into strings"
+ (info "foo:" 1 2 3)
+ (should= "foo: 1 2 3" (:message (first @(.received @fake-logger)))))
+
+ (it "logs warnings"
+ (warn "warning!" 1)
+ (should= "warning! 1" (:message (first @(.received @fake-logger))))
+ (should= java.util.logging.Level/WARNING (:level (first @(.received @fake-logger)))))
+
+ (it "logs debug"
+ (should (.isInstance java.util.logging.Level DEBUG))
+ (should= "DEBUG" (.getName DEBUG))
+ (should= (- (.intValue java.util.logging.Level/CONFIG) 50) (.intValue DEBUG))
+ (debug "debug!" 1)
+ (should= "debug! 1" (:message (first @(.received @fake-logger))))
+ (should= DEBUG (:level (first @(.received @fake-logger)))))
+
+ (it "logs error"
+ (should (.isInstance java.util.logging.Level ERROR))
+ (should= "ERROR" (.getName ERROR))
+ (should= (.intValue java.util.logging.Level/SEVERE) (.intValue ERROR))
+ (error "error!" 1)
+ (should= "error! 1" (:message (first @(.received @fake-logger))))
+ (should= ERROR (:level (first @(.received @fake-logger)))))
+
+ (it "sets the level"
+ (warn-on!)
+ (should= java.util.logging.Level/WARNING @(.level @fake-logger))
+ (info-on!)
+ (should= java.util.logging.Level/INFO @(.level @fake-logger))
+ (debug-on!)
+ (should= DEBUG @(.level @fake-logger)))
+
+ (it "turns to logger off!"
+ (off!)
+ (should= java.util.logging.Level/OFF @(.level @fake-logger)))
+
+ )
+
+ )
View
@@ -0,0 +1,75 @@
+(ns hyperion.log
+ (:import [java.util.logging Level Formatter]))
+
+(defprotocol Logger
+ (log! [this level message])
+ (set-level [this level]))
+
+(extend-protocol Logger
+ java.util.logging.Logger
+ (log! [this level message] (.log this level message))
+ (set-level [this level] (.setLevel this level)))
+
+(def ^{:dynamic true :tag hyperion.log.Logger} *logger*
+ (doto
+ (java.util.logging.Logger/getLogger "hyperion")
+ (set-level Level/WARNING)))
+
+(def DEBUG (proxy [Level] ["DEBUG" (- (.intValue Level/CONFIG) 50)]))
+(def ERROR (proxy [Level] ["ERROR" (.intValue Level/SEVERE)]))
+(def FORMATTER (proxy [Formatter] []
+ (format [this record] "foo")))
+; (clojure.core/format "%1$7s %2$tH:%2$tM:%2$tS:%2$tL %3$s: %4$s\n"
+; (.getLevel record)
+; (.getMillis record)
+; (.getLoggerName record)
+; (.getMessage record)))))
+
+
+;private static class LimelightFormatter extends Formatter
+; {
+; @Override
+; public String format(LogRecord logRecord)
+; {
+; return String.format("%1$7s %2$tH:%2$tM:%2$tS:%2$tL %3$s: %4$s\n",
+; logRecord.getLevel(),
+; logRecord.getMillis(),
+; logRecord.getLoggerName(),
+; logRecord.getMessage());
+; }
+; }
+
+(defn- logify [args]
+ (binding [*print-readably* nil]
+ (apply pr-str args)))
+
+(defn info [& args]
+ (log! *logger* Level/INFO (logify args)))
+
+(defn warn [& args]
+ (log! *logger* Level/WARNING (logify args)))
+
+(defn debug [& args]
+ (log! *logger* DEBUG (logify args)))
+
+(defn error [& args]
+ (log! *logger* Level/SEVERE (logify args)))
+
+(defn warn-on! []
+ (set-level *logger* Level/WARNING))
+
+(defn info-on! []
+ (set-level *logger* Level/INFO))
+
+(defn debug-on! []
+ (set-level *logger* DEBUG))
+
+(defn off! []
+ (set-level *logger* Level/OFF))
+
+(defn remove-all-handlers! []
+ (doseq [handler (.getHandlers *logger*)]
+ (.removeHandler *logger* handler)))
+
+(defn add-handler! [handler]
+ (.addHandler *logger* handler))
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4cf9699

Please sign in to comment.