 
 (defn puppetdb-instance
   "Stands up a puppetdb instance with `config`, tears down once `f` returns.
-   Adjusts *server* and *base-url* to refer to the instance."
+  `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]
-   (let [config (conf/adjust-tk-config config)
+  ([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])]
-     (tkbs/with-app-with-config server
-       [jetty9-service puppetdb-service message-listener-service command-service webrouting-service metrics-service]
-       config
-       (binding [*server* server
-                 *base-url* (merge {:protocol "http"
-                                    :host "localhost"
-                                    :port (current-port server)}
-                                   (when prefix {:prefix prefix}))]
-         (f))))))
+     (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)
+             (utils/println-err "-------Begin PuppetDB Instance Log--------------------\n"
+                                log-contents
+                                "\n-------End PuppetDB Instance Log----------------------"))))))))
 
 (defmacro with-puppetdb-instance
   "Convenience macro to launch a puppetdb instance"

(ns puppetlabs.puppetdb.testutils.jetty
  (:require [puppetlabs.puppetdb.testutils :refer [temp-dir]]
            [puppetlabs.puppetdb.fixtures :as fixt]
            [puppetlabs.trapperkeeper.app :as tka]
            [puppetlabs.trapperkeeper.testutils.bootstrap :as tkbs]

   :web-router-service {:puppetlabs.puppetdb.cli.services/puppetdb-service ""
                        :puppetlabs.puppetdb.metrics/metrics-service "/metrics"}})

(defn assoc-logging-config
  "Adds a dynamically created logback.xml with a test log. The
  generated log file name is returned for printing to the console."
  [config]
  (let [logback-file (fs/absolute-path (temp-file "logback" ".xml"))
        log-file (fs/absolute-path (temp-file "jett-test" ".log"))]
    (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])
