angch.venice.1.12.27.source-code.zip-tomcat-logs.venice Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of venice Show documentation
Show all versions of venice Show documentation
Venice, a sandboxed Lisp implemented in Java.
;;;; __ __ _
;;;; \ \ / /__ _ __ (_) ___ ___
;;;; \ \/ / _ \ '_ \| |/ __/ _ \
;;;; \ / __/ | | | | (_| __/
;;;; \/ \___|_| |_|_|\___\___|
;;;;
;;;;
;;;; Copyright 2017-2024 Venice
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License");
;;;; you may not use this file except in compliance with the License.
;;;; You may obtain a copy of the License at
;;;;
;;;; http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS,
;;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;;; See the License for the specific language governing permissions and
;;;; limitations under the License.
;;;; ---------------------------------------------------------------------------
;;;; Zips the last month's Tomcat log files
;;;;
;;;; > java -jar venice-1.7.19.jar -file zip-tomcat-logs.venice ./logs
;;;; ---------------------------------------------------------------------------
(do
(defn tomcat-log-file-filter [prefix year month]
(let [regex (str/format "%s[.]%d-%02d-[0-9][0-9][.]log" prefix year month)]
(fn [f] (match? (io/file-name f) regex))))
(defn tomcat-log-file-zip [prefix dir year month]
(io/file dir (str/format "%s.%d-%02d.zip" prefix year month)))
(defn zip-files [dir zip files]
(with-sh-throw
(with-sh-dir dir
(apply sh (concat ["zip" (:name zip)] (map #(:name %) files))))))
(defn zip-tomcat-logs [prefix dir year month]
(try
(let [zip (tomcat-log-file-zip prefix dir year month)
filter (tomcat-log-file-filter prefix year month)
logs (io/list-files dir filter)]
(printf "Compacting %s ...\n" prefix)
(printf " Found %d log files\n" (count logs))
(when-not (empty? logs)
(zip-files dir zip logs)
(printf " Zipped to %s\n" (:name zip))
(apply io/delete-file logs)
(printf " Removed %d files\n" (count logs))))
(catch :com.github.jlangch.venice.ShellException ex
(printf "Error compacting %s: %s" prefix (:message ex)))))
(defn first-day-of-last-month []
(-> (time/local-date)
(time/first-day-of-month)
(time/plus :month -1)))
(let [dir (io/file (nth *ARGV* 2))
date (first-day-of-last-month)
year (time/year date)
month (time/month date)]
(if (io/exists-dir? dir)
(do
(printf "Compacting %d-%02d logs from '%s' ...\n" year month dir)
(zip-tomcat-logs "localhost_access_log" dir year month)
(zip-tomcat-logs "host-manager" dir year month)
(zip-tomcat-logs "manager" dir year month)
(zip-tomcat-logs "localhost" dir year month)
(zip-tomcat-logs "catalina" dir year month)
(println "Done."))
(printf "Error: The Tomcat log dir '%s' does not exist" dir))))