All Downloads are FREE. Search and download functionalities are using the official Maven repository.

angch.venice.1.12.30.source-code.zip-tomcat-logs.venice Maven / Gradle / Ivy

There is a newer version: 1.12.34
Show newest version
;;;;   __    __         _
;;;;   \ \  / /__ _ __ (_) ___ ___
;;;;    \ \/ / _ \ '_ \| |/ __/ _ \
;;;;     \  /  __/ | | | | (_|  __/
;;;;      \/ \___|_| |_|_|\___\___|
;;;;
;;;;
;;;; 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))))




© 2015 - 2024 Weber Informatics LLC | Privacy Policy