-
-
Save apod/a4ed0e175ce3ccd269b82f688a878fdf to your computer and use it in GitHub Desktop.
App config component
This file contains 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 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})) |
This file contains 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
{: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