diff --git a/src/taoensso/timbre.clj b/src/taoensso/timbre.clj index f1e24ae..7cd7114 100644 --- a/src/taoensso/timbre.clj +++ b/src/taoensso/timbre.clj @@ -3,9 +3,9 @@ (:require [clojure.string :as str] [io.aviso.exception :as aviso-ex] [taoensso.encore :as enc]) - (:import java.io.File - [java.util Date Locale] - [java.text SimpleDateFormat])) + (:import [java.util Date Locale] + [java.text SimpleDateFormat] + [java.io File])) ;;;; Encore version check @@ -97,6 +97,27 @@ (:current-level config) @level-atom)))) +;;;; + +(def ^:private get-hostname + (enc/memoize* 60000 + (fn [] + (-> + (future ; Android doesn't like this on the main thread + (try (.. java.net.InetAddress getLocalHost getHostName) + (catch java.net.UnknownHostException _ + "UnknownHost"))) + (deref 5000 "UnknownHost"))))) + +(def ^:private ensure-spit-dir-exists! + (enc/memoize* 60000 + (fn [fname] + (when-not (str/blank? fname) + (let [file (File. ^String fname) + dir (.getParentFile (.getCanonicalFile file))] + (when-not (.exists dir) + (.mkdirs dir))))))) + ;;;; Default configuration and appenders (defn default-fmt-output-fn @@ -187,7 +208,8 @@ :min-level nil :enabled? false :async? false :rate-limit nil :fn (fn [{:keys [ap-config output]}] ; Can use any appender args (when-let [filename (:spit-filename ap-config)] - (try (spit filename (str output "\n") :append true) + (try (ensure-spit-dir-exists! filename) + (spit filename (str output "\n") :append true) (catch java.io.IOException _))))}}}) (enc/defonce* config (atom example-config)) @@ -260,31 +282,11 @@ (fn [apfn-args] ; Runtime: (send-off agent (fn [_] (apfn apfn-args))))))))))) -(def ^:private get-hostname - (enc/memoize* 60000 - (fn [] - (-> - (future ; Android doesn't like this on the main thread - (try (.. java.net.InetAddress getLocalHost getHostName) - (catch java.net.UnknownHostException _ - "UnknownHost"))) - (deref 5000 "UnknownHost"))))) - -(defn- exists? - [path] - (when-not (str/blank? path) (.exists (File. ^String path)))) - -(defn- mkdirs "Creates all parent directories for the passed file." - [^File f] - (.mkdirs (.getParentFile (.getCanonicalFile f)))) - (defn- wrap-appender-juxt "Wraps compile-time appender juxt with additional runtime capabilities (incl. middleware) controlled by compile-time config. Like `wrap-appender-fn` but operates on the entire juxt at once." [config juxtfn] - (when-let [spit-file (get-in config [:shared-appender-config :spit-filename])] - (when-not (exists? spit-file) (mkdirs (File. spit-file)))) (->> ; Wrapping applies per juxt, bottom-to-top juxtfn