From 2f22b733c1800ee6c07a23e866355c9932602982 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Wed, 12 Feb 2020 21:13:36 +0000 Subject: [PATCH] Tidy up alphordered includes, standardised on use 'log' as alias for timbre. --- src/smeagol/authenticate.clj | 20 ++++++++++---------- src/smeagol/configuration.clj | 12 ++++++------ src/smeagol/formatting.clj | 30 +++++++++++++++++++++++++----- src/smeagol/handler.clj | 12 ++++++------ src/smeagol/history.clj | 9 +++++---- src/smeagol/layout.clj | 3 +-- src/smeagol/middleware.clj | 14 +++++++------- src/smeagol/util.clj | 4 ++-- 8 files changed, 62 insertions(+), 42 deletions(-) diff --git a/src/smeagol/authenticate.clj b/src/smeagol/authenticate.clj index e4219e9..fec7e7a 100644 --- a/src/smeagol/authenticate.clj +++ b/src/smeagol/authenticate.clj @@ -5,7 +5,7 @@ [environ.core :refer [env]] [noir.io :as io] [smeagol.configuration :refer [config]] - [taoensso.timbre :as timbre])) + [taoensso.timbre :as log])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -52,7 +52,7 @@ "Return `true` if this `username`/`password` pair match, `false` otherwise" [username password] (let [user ((keyword username) (get-users))] - (timbre/info (str "Authenticating " username " against " password-file-path)) + (log/info (str "Authenticating " username " against " password-file-path)) (and user (:password user) (or @@ -92,7 +92,7 @@ Return `true` if password was successfully changed. Subsequent to user change, their password will be encrypted." [username oldpass newpass] - (timbre/info (format "Changing password for user %s" username)) + (log/info (format "Changing password for user %s" username)) (let [users (get-users) keywd (keyword username) user (keywd users) @@ -110,10 +110,10 @@ {keywd (merge user {:password (password/encrypt newpass)})}))) - (timbre/info (str "Successfully changed password for user " username)) + (log/info (str "Successfully changed password for user " username)) true)) (catch Exception any - (timbre/error any + (log/error any (format "Changing password failed for user %s failed: %s (%s)" username (.getName (.getClass any)) (.getMessage any))) false)))) @@ -138,7 +138,7 @@ `email` address and `admin` flag; *or*, modify an existing user. Return true if user is successfully stored, false otherwise." [username newpass email admin] - (timbre/info "Trying to add user " username) + (log/info "Trying to add user " username) (cond (not (string? username)) (throw (Exception. "Username must be a string.")) (zero? (count username)) (throw (Exception. "Username cannot be zero length")) @@ -160,10 +160,10 @@ (locking password-file-path (spit password-file-path (assoc users (keyword username) (merge user full-details))) - (timbre/info "Successfully added user " username) + (log/info "Successfully added user " username) true) (catch Exception any - (timbre/error any + (log/error any (format "Adding user %s failed: %s (%s)" username (.getName (.getClass any)) (.getMessage any))) false))))) @@ -177,10 +177,10 @@ (locking password-file-path (spit password-file-path (dissoc users (keyword username))) - (timbre/info (str "Successfully deleted user " username)) + (log/info (str "Successfully deleted user " username)) true) (catch Exception any - (timbre/error any + (log/error any (format "Deleting user %s failed: %s (%s)" username (.getName (.getClass any)) (.getMessage any))) false)))) diff --git a/src/smeagol/configuration.clj b/src/smeagol/configuration.clj index 025292a..24d1e55 100644 --- a/src/smeagol/configuration.clj +++ b/src/smeagol/configuration.clj @@ -5,7 +5,7 @@ [clojure.string :as s] [environ.core :refer [env]] [noir.io :as io] - [taoensso.timbre :as timbre])) + [taoensso.timbre :as log])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -73,7 +73,7 @@ and optionally a key :transform, whose value is a function of one argument to be used to transform the value of that key." [m tuples] - (timbre/debug + (log/debug "transform-map:\n" (with-out-str (clojure.pprint/pprint m))) (reduce @@ -112,11 +112,11 @@ file is read (if it is specified and present), but that individual values can be overridden by environment variables." (try - (timbre/info (str "Reading configuration from " config-file-path)) + (log/info (str "Reading configuration from " config-file-path)) (let [file-contents (try (read-string (slurp config-file-path)) (catch Exception x - (timbre/error + (log/error (str "Failed to read configuration from " config-file-path @@ -138,12 +138,12 @@ :smeagol-site-title) config-env-transforms))] (if (env :dev) - (timbre/debug + (log/debug "Loaded configuration\n" (with-out-str (clojure.pprint/pprint config)))) config) (catch Exception any - (timbre/error any "Could not load configuration") + (log/error any "Could not load configuration") {}))) (def config (build-config)) diff --git a/src/smeagol/formatting.clj b/src/smeagol/formatting.clj index 87d8f45..8eff3ec 100644 --- a/src/smeagol/formatting.clj +++ b/src/smeagol/formatting.clj @@ -44,6 +44,7 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Error to show if text to be rendered is nil. +;; TODO: this should go through i18n (def no-text-error "No text: does the file exist?") @@ -150,28 +151,43 @@ ;; I need to put the backticks back in. remarked (if (odd? index) (str "```" fragment "\n```") fragment) first-token (get-first-token fragment) + kw (if-not (empty? first-token) (keyword first-token)) formatter (if-not (empty? first-token) (try - (let [kw (keyword first-token)] - (read-string (-> config :formatters kw :formatter))) + (read-string (-> config :formatters kw :formatter)) (catch Exception _ (do - (log/info "No formatter found for extension `" first-token "`") + (log/info "No formatter found for extension `" kw "`") ;; no extension registered - there sometimes won't be, ;; and it doesn't matter nil))))] (cond (empty? fragments) + ;; We've come to the end of the list of fragments. Reassemble them into + ;; a single HTML text and pass it back. (assoc result :text (local-links (md/md-to-html-string (cs/join "\n\n" (reverse processed)) :heading-anchors true))) formatter - (apply-formatter index result fragments processed fragment first-token formatter) + ;; We've found a formatter to apply to the current fragment, and recurse + ;; on down the list + (let [result (apply-formatter + index + result + fragments + processed + fragment + first-token + formatter)] + (assoc result :extensions (cons kw (:extensions result)))) true - (process-markdown-fragment index result remarked (rest fragments) processed))))) + ;; Otherwise process the current fragment as markdown and recurse on + ;; down the list + (process-markdown-fragment + index result remarked (rest fragments) processed))))) (defn reintegrate-inclusions @@ -182,6 +198,10 @@ ([inclusions text] (let [ks (keys inclusions)] (if (empty? (keys inclusions)) + ;; TODO: this is one opportunity to add scripts at the end of the + ;; constructed text. I've a feeling that that would be a mistake and + ;; that instead we should hand back a map comprising the text and the + ;; keys of the extensions text (let [kw (first ks)] (reintegrate-inclusions diff --git a/src/smeagol/handler.clj b/src/smeagol/handler.clj index 9cbaca5..db580e6 100644 --- a/src/smeagol/handler.clj +++ b/src/smeagol/handler.clj @@ -16,7 +16,7 @@ [smeagol.routes.wiki :refer [wiki-routes]] [smeagol.middleware :refer [load-middleware]] [smeagol.session-manager :as session-manager] - [taoensso.timbre :as timbre] + [taoensso.timbre :as log] [taoensso.timbre.appenders.3rd-party.rotor :as rotor])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -55,9 +55,9 @@ "destroy will be called when your application shuts down, put any clean up code here" [] - (timbre/info "smeagol is shutting down...") + (log/info "smeagol is shutting down...") (cronj/shutdown! session-manager/cleanup-job) - (timbre/info "shutdown complete!")) + (log/info "shutdown complete!")) (defn init @@ -67,7 +67,7 @@ put any initialization code here" [] (try - (timbre/merge-config! + (log/merge-config! {:appenders {:rotor (rotor/rotor-appender {:path "smeagol.log" @@ -80,10 +80,10 @@ (cronj/start! session-manager/cleanup-job) (if (env :dev) (parser/cache-off!)) ;;start the expired session cleanup job - (timbre/info "\n-=[ smeagol started successfully" + (log/info "\n-=[ smeagol started successfully" (when (env :dev) "using the development profile") "]=-") (catch Exception any - (timbre/error any "Failure during startup") + (log/error any "Failure during startup") (destroy)))) ;; timeout sessions after 30 minutes diff --git a/src/smeagol/history.clj b/src/smeagol/history.clj index aca6dbe..e567db3 100644 --- a/src/smeagol/history.clj +++ b/src/smeagol/history.clj @@ -1,10 +1,10 @@ (ns ^{:doc "Explore the history of a page." :author "Simon Brooke"} smeagol.history - (:require [taoensso.timbre :as timbre] - [clj-jgit.porcelain :as git] + (:require [clj-jgit.porcelain :as git] [clj-jgit.internal :as i] - [clj-jgit.querying :as q]) + [clj-jgit.querying :as q] + [taoensso.timbre :as log]) (:import [org.eclipse.jgit.api Git] [org.eclipse.jgit.lib Repository ObjectId] [org.eclipse.jgit.revwalk RevCommit RevTree RevWalk] @@ -39,7 +39,7 @@ "If this `log-entry` contains a reference to this `file-path`, return the entry; else nil." [^String log-entry ^String file-path] - (timbre/info (format "searching '%s' for '%s'" log-entry file-path)) + (log/info (format "searching '%s' for '%s'" log-entry file-path)) (cond (seq (filter (fn* [p1__341301#] (= (first p1__341301#) file-path)) (:changed_files log-entry))) log-entry)) @@ -54,6 +54,7 @@ (try (git/load-repo git-directory-path) (catch java.io.FileNotFoundException fnf + (log/info "Initialising Git repository at" git-directory-path) (git/git-init git-directory-path) (let [repo (git/load-repo git-directory-path)] (git/git-add-and-commit repo "Initial commit") diff --git a/src/smeagol/layout.clj b/src/smeagol/layout.clj index 34a7d0a..920a9b1 100644 --- a/src/smeagol/layout.clj +++ b/src/smeagol/layout.clj @@ -12,8 +12,7 @@ [selmer.parser :as parser] [smeagol.configuration :refer [config]] [smeagol.sanity :refer :all] - [smeagol.util :as util] - [taoensso.timbre :as timbre])) + [smeagol.util :as util])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; diff --git a/src/smeagol/middleware.clj b/src/smeagol/middleware.clj index 92cfac0..4ca288d 100644 --- a/src/smeagol/middleware.clj +++ b/src/smeagol/middleware.clj @@ -1,17 +1,17 @@ (ns ^{:doc "In truth, boilerplate provided by LuminusWeb." :author "Simon Brooke"} smeagol.middleware - (:require [taoensso.timbre :as timbre] - [environ.core :refer [env]] - [selmer.middleware :refer [wrap-error-page]] + (:require [environ.core :refer [env]] + [noir-exception.core :refer [wrap-internal-error]] [prone.middleware :refer [wrap-exceptions]] [ring.middleware.anti-forgery :refer [wrap-anti-forgery]] [ring.middleware.file :refer [wrap-file]] [ring.middleware.resource :refer [wrap-resource]] [ring.middleware.content-type :refer [wrap-content-type]] [ring.middleware.not-modified :refer [wrap-not-modified]] - [noir-exception.core :refer [wrap-internal-error]] - [smeagol.util :as util])) + [selmer.middleware :refer [wrap-error-page]] + [smeagol.util :as util] + [taoensso.timbre :as log])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -39,7 +39,7 @@ (defn log-request [handler] (fn [req] - (timbre/debug req) + (log/debug req) (handler req))) @@ -49,7 +49,7 @@ (def production-middleware - [#(wrap-internal-error % :log (fn [e] (timbre/error e))) + [#(wrap-internal-error % :log (fn [e] (log/error e))) #(wrap-resource % "public") #(wrap-file % util/content-dir {:index-files? false :prefer-handler? true}) diff --git a/src/smeagol/util.clj b/src/smeagol/util.clj index 1ef44df..0a5863a 100644 --- a/src/smeagol/util.clj +++ b/src/smeagol/util.clj @@ -10,7 +10,7 @@ [smeagol.authenticate :as auth] [smeagol.configuration :refer [config]] [smeagol.formatting :refer [md->html]] - [taoensso.timbre :as timbre])) + [taoensso.timbre :as log])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -69,7 +69,7 @@ messages (try (i18n/get-messages specifier "i18n" "en-GB") (catch Exception any - (timbre/error + (log/error any (str "Failed to parse accept-language header '"