             [clj-http.client :as client]
             [clojure.string :as str]
             [fs.core :as fs]
-            [slingshot.slingshot :refer [throw+]]))
+            [slingshot.slingshot :refer [throw+]]
+            [clojure.tools.logging :as log]))
 
 ;; See utils.clj for more information about base-urls.
 (def ^:dynamic *base-url* nil) ; Will not have a :version.

    (spit logback-file (log-config log-file "ERROR"))
    [log-file (assoc-in config [:global :logging-config] logback-file)]))

(defn current-port
  "Given a trapperkeeper server, return the port of the running jetty instance.
  Note there can be more than one port (i.e. SSL + non-SSL connector). This only
  returns the first one."
  [server]
  (-> @(tka/app-context server)
      (get-in [:WebserverService :jetty9-servers :default :server])
      .getConnectors
      first
      .getLocalPort))

(def ^:dynamic *server*)

(defn puppetdb-instance
  "Stands up a puppetdb instance with `config`, tears down once `f` returns.
  `services` is a seq of additional services that should be started in addition
  to the core PuppetDB services. Binds *server* and *base-url* to refer to
  the instance."
  ([f] (puppetdb-instance (create-config) f))
  ([config f] (puppetdb-instance config [] f))
  ([config services f]
   (let [[log-file config] (-> config conf/adjust-tk-config assoc-logging-config)
         prefix (get-in config
                        [:web-router-service
                         :puppetlabs.puppetdb.cli.services/puppetdb-service])]
     (try
       (tkbs/with-app-with-config server
         (concat [jetty9-service puppetdb-service message-listener-service command-service webrouting-service metrics-service]
                 services)
         config
         (binding [*server* server
                   *base-url* (merge {:protocol "http"
                                      :host "localhost"
                                      :port (current-port server)}
                                     (when prefix {:prefix prefix}))]
           (f)))
       (finally
         (let [log-contents (slurp log-file)]
           (when-not (str/blank? log-contents)

   :jetty {:port 0}
   :database (fixt/create-db-map)
   :command-processing {}
   :web-router-service {::svcs/puppetdb-service ""
                        :puppetlabs.puppetdb.metrics/metrics-service "/metrics"}})

(defn assoc-logging-config
