Skip to content

Instantly share code, notes, and snippets.

@apod
Created February 10, 2018 10:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apod/a4ed0e175ce3ccd269b82f688a878fdf to your computer and use it in GitHub Desktop.
Save apod/a4ed0e175ce3ccd269b82f688a878fdf to your computer and use it in GitHub Desktop.
App config component
(ns backlog.components.app-config
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[com.stuartsierra.component :as component]
[taoensso.timbre :as timbre]
[taoensso.timbre.appenders.core :as appenders]))
(defn- enable-logging [{:keys [spit-appender-options min-level]
:or {min-level :trace}}]
(timbre/merge-config!
{:level min-level
:appenders
{:spit (appenders/spit-appender spit-appender-options)}}))
(defn- disable-logging []
(timbre/merge-config! {:appenders {:spit nil}}))
(defrecord AppConfig [config-path]
component/Lifecycle
(start [component]
(if (:options component)
component
(let [config-file (io/file config-path)]
(if (.exists config-file)
(let [options (edn/read-string (slurp (io/file config-path)))]
(enable-logging (get options :logging))
(timbre/info "[AppConfig] Logging started")
(assoc component :options options :logging-enabled? true))
(throw (ex-info
(str "App configuration file not found: " config-path)
{:cause :config-file-not-found}))))))
(stop [component]
(if-let [options (:options component)]
(do
(timbre/info "[AppConfig] Logging stopped")
(disable-logging)
(assoc component :options nil :logging-enabled? false))
component)))
(defn app-config [config-path]
(map->AppConfig {:config-path config-path}))
{:env :dev
:logging {:spit-appender-options
{:fname "log/dev.log"
:append? true}
:min-level :debug}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment