diff --git a/project.clj b/project.clj index 3bc203b..fd522f4 100644 --- a/project.clj +++ b/project.clj @@ -35,7 +35,9 @@ [com.draines/postal "1.11.4"] [irclj "0.5.0-alpha4"] [org.graylog2/gelfclient "1.3.1"] - [org.julienxx/clj-slack "0.5.3"]]} + [org.julienxx/clj-slack "0.5.3"] + [org.clojure/java.jdbc "0.5.0"] + [com.mchange/c3p0 "0.9.5.2"]]} :dev [:1.7 :test {:dependencies [[org.clojure/clojurescript "1.7.28"]] diff --git a/src/taoensso/timbre/appenders/3rd_party/postgresdb.clj b/src/taoensso/timbre/appenders/3rd_party/postgresdb.clj index 3dcde87..1d60488 100644 --- a/src/taoensso/timbre/appenders/3rd_party/postgresdb.clj +++ b/src/taoensso/timbre/appenders/3rd_party/postgresdb.clj @@ -1,47 +1,53 @@ (ns taoensso.timbre.appenders.3rd-party.postgresdb - "postgresql database appender" - (:require [clojure.java.jdbc :as j] - [environ.core :refer [env]] - [taoensso.timbre :as timbre] - [taoensso.encore :as encore]) - (:import com.mchange.v2.c3p0.ComboPooledDataSource)) - -(def default-args {:type "postgresql" :host "127.0.0.1" :port 5432}) + "PostgreSQL database appender. + Requires https://github.com/clojure/java.jdbc, + https://github.com/swaldman/c3p0" + {:author "Yue Liu (@yuliu-mdsol)"} + (:require + [taoensso.timbre :as timbre] + [taoensso.encore :as enc] + [clojure.java.jdbc :as jdbc])) (defn pool [spec] - (let [cpds (doto (ComboPooledDataSource.) - (.setJdbcUrl (str "jdbc:" (:type spec) "://" (:host spec) ":" (:port spec) "/" (:database spec))) - (.setUser (:username spec)) - (.setPassword (:password spec)) - ;; expire excess connections after 30 minutes of inactivity: - (.setMaxIdleTimeExcessConnections (* 30 60)) - ;; expire connections after 3 hours of inactivity: - (.setMaxIdleTime (* 3 60 60)))] + (let [cpds + (doto (com.mchange.v2.c3p0.ComboPooledDataSource.) + (.setJdbcUrl (str "jdbc:" (:type spec) "://" (:host spec) ":" (:port spec) "/" (:database spec))) + (.setUser (:username spec)) + (.setPassword (:password spec)) + ;; expire excess connections after 30 minutes of inactivity: + (.setMaxIdleTimeExcessConnections (* 30 60)) + ;; expire connections after 3 hours of inactivity: + (.setMaxIdleTime (* 3 60 60)))] {:datasource cpds})) -(defn connect [{:keys [server]}] - (let [args (merge default-args server)] - @(delay (pool args)))) +(def default-pool-spec {:type "postgresql" :host "127.0.0.1" :port 5432}) +(defn connect [config] + (let [spec (merge default-pool-spec (:server config))] + (pool spec))) (def conn (atom nil)) (defn ensure-conn [config] (swap! conn #(or % (connect config)))) -(defn log-message [data config] - (let [entry {:instant (java.sql.Timestamp. (.getTime (:instant data))) - :level (str (:level data)) - :namespace (str (:?ns-str data)) - :hostname (str @(:hostname_ data)) - :content (str @(:vargs_ data)) - :error (str @(:?err_ data))}] - (ensure-conn config) - (j/insert! @conn :logs entry))) +(defn log-message [config data] + (let [entry + {:instant (java.sql.Timestamp. (.getTime ^java.util.Date (:instant data))) + :level (str (:level data)) + :namespace (str (:?ns-str data)) + :hostname (str @(:hostname_ data)) + :content (str @(:vargs_ data)) + :error (str @(:?err_ data))}] -(defn pglog-appender - "Returns a postgresqlDB appender. + (ensure-conn config) + (jdbc/insert! @conn :logs entry))) + +(defn pgsql-appender + "Returns a PostgreSQL appender for `clojure.java.jdbc`. (pglog-appender {:server {:host \"127.0.0.1\" :port 5432}}) - For log table creation and rollback, here are sql ddl script to use: + SQL ddl script for table creation and rollback + ---------------------------------------------- + ``` CREATE TABLE IF NOT EXISTS logs ( log_id bigserial primary key, instant timestamp NOT NULL, @@ -53,21 +59,20 @@ ); DROP TABLE IF EXISTS logs; + ``` - To automate database migration, you can use migratus for Leiningen, - or use ragtime for Boot. + To automate database migration + ------------------------------ - Leiningen migratus (in profiles.clj) - ------------------------------------ - :database-url \"postgresql://:@:/\" + * Using Migratus + Leiningen (in profiles.clj): + :database-url \"postgresql://:@:/\" - Boot ragtime (in build.boot) - ---------------------------- - ragtime {:driver-class \"org.postgresql.Driver\" - :database (str \"jdbc:postgresql://:/\" - \"\" - \"?user=\" - \"&password=\")})" + * Using Boot + Ragtime (in build.boot): + ragtime {:driver-class \"org.postgresql.Driver\" + :database (str \"jdbc:postgresql://:/\" + \"\" + \"?user=\" + \"&password=\")})" [db-config] {:enabled? true @@ -75,4 +80,4 @@ :min-level nil :rate-limit nil :output-fn :inherit - :fn (fn [data] (log-message data db-config))}) \ No newline at end of file + :fn (fn [data] (log-message db-config data))}) diff --git a/src/taoensso/timbre/appenders/example.clj b/src/taoensso/timbre/appenders/example.clj index e1b02e2..139e3f4 100644 --- a/src/taoensso/timbre/appenders/example.clj +++ b/src/taoensso/timbre/appenders/example.clj @@ -2,12 +2,14 @@ "You can copy this namespace if you'd like a starting template for writing your own Timbre appender. PRs for new appenders welcome! + TODO Please document any dependency GitHub links here + - Peter Taoussanis" - {:author "Your Name (@your-github-username)"} + {:author "TODO Your Name (@your-github-username)"} (:require [taoensso.timbre :as timbre])) -;; If you add any special ns imports above, please remember to update +;; TODO If you add any special ns imports above, please remember to update ;; Timbre's `project.clj` to include the necessary dependencies under ;; the `:test` profile