SSブログ

Mewのログ・ローテーションへの機能追加 [Linux]

Mew のログ・ローテーションへの機能追加を行った。
詳細は、下記の通りである。

1. 追加した機能


・ローテーション後のログ・ファイルの圧縮を可能にする(gzip/bzip2 を使用)。
・保存するログ・ファイルの世代数を指定可能にする。

【動作確認を行ったバージョン】
・Mew 5.2/6.3/6.4 (~/Mail/{Refilelog,Smtplog} で確認)


2. 使用方法


(1) mew-log-util.el の導入


load-path に設定されているディレクトリに、mew-log-util.el として保存する。

;;; -*- tab-width: 4 -*-
;;; mew-log-util.el --- utilities to rotate log file of Mew

;; This is for Mew-3.1 or later.

(require 'mew)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; variables
;;

(defvar mew-log-max-rotation 4
  "*A value for the max number of times of log rotation.
If the value is not more than 0, log files are not rotated.
If the value is less than 0, it is changed to 0 when used.

Rotated log file is the following.
- file name: file.<number>.<compress_suffix> or file.<number>
- number: 0 origin")

(defvar mew-log-compression t
  "*If non-nil, compress rotated log files.")

(defvar mew-log-compress-cmd "gzip"
  "*Command to use compress rotated log files.
Only 'gzip' and 'bzip2' are supported.")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; functions
;;

(defun mew-rotate-log-files-enhanced (file-name)
  "Rotate log files.
This is enhancement of mew-rotate-log-files.

The following features are supported.
- compression of rotated log files (use gzip or bzip2)
- ability to change the maximum number of rotated log file"
  (when (< mew-log-max-rotation 0)
    (setq mew-log-max-rotation 0))
  (let ((i (- mew-log-max-rotation 2))
        (file (expand-file-name file-name mew-conf-path))
        src dst (suffix ""))
    (when mew-log-compression
      ;; set suffix
      (if (equal mew-log-compress-cmd "bzip2")
          (setq suffix ".bz2")
        (setq suffix ".gz")))
    (when (and (file-exists-p file)
               (>= (mew-file-get-size file) mew-log-max-size))
      ;; rotate files
      (while (>= i 0)
        (setq src (format "%s.%d%s" file i suffix))
        (setq dst (format "%s.%d%s" file (1+ i) suffix))
        (when (file-exists-p src)
          (rename-file src dst t))
        (setq i (1- i)))
      (when (> mew-log-max-rotation 0)
        (setq dst (format "%s.0" file))
        (rename-file file dst)
        (when mew-log-compression
          (call-process mew-log-compress-cmd nil nil nil "-f" dst))))
    ;; delete old log files
    (mew-delete-old-log-files file-name)))

(defun mew-delete-old-log-files (file-name)
  "Delete old log files."
  (let (flist fname index offset offset2 (suffix ""))
    ;; get file list
    (setq flist
          (append
           (directory-files
            mew-conf-path nil
            (format "%s\\.[0-9]\\(\\|\\.gz\\|\\.bz2\\)$" file-name))
           (directory-files
            mew-conf-path nil
            (format "%s\\.[1-9][0-9]\\(\\|\\.gz\\|\\.bz2\\)$" file-name))))
    ;; delete old log file
    (while flist
      (setq fname (car (last flist)))
      (setq flist (butlast flist))
      ;; check whether old log file
      (setq offset2 (string-match "\\.\\(gz\\|bz2\\)$" fname))
      (when (null offset2)
        (setq offset2 (length fname)))
      (setq offset (string-match "\\." fname))
      (setq index (string-to-number (substring fname (1+ offset) offset2)))
      (if (>= index mew-log-max-rotation)
          (delete-file (expand-file-name fname mew-conf-path))
        (setq flist nil)))
    ;; get file list of old format log
    (cond
     (mew-log-compression
      (if (equal mew-log-compress-cmd "bzip2")
          (setq suffix ".gz")
        (setq suffix ".bz2"))
      (setq flist
            (append
             (directory-files
              mew-conf-path nil
              (format "%s\\.[0-9]+%s$" file-name suffix))
             (directory-files
              mew-conf-path nil
              (format "%s\\.[0-9]+$" file-name)))))
     (t
      (setq flist
            (directory-files
             mew-conf-path nil
             (format "%s\\.[0-9]+\\.\\(gz\\|bz2\\)$" file-name)))))
    ;; delete old format log file
    (while flist
      (setq fname (pop flist))
      (delete-file (expand-file-name fname mew-conf-path)))))

;; enable to enhance mew-rotate-log-files
(defalias 'mew-rotate-log-files 'mew-rotate-log-files-enhanced)

(provide 'mew-log-util)

;;; mew-log-util.el ends here


(2) ~/.mew.el への設定(例)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; setting to rotate log file of Mew
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; enable to enhance mew-rotate-log-files
(require 'mew-log-util)

;; set the max number of rotated log files (default: 4)
(setq mew-log-max-rotation 3)

;; set max size of log file in bytes (default: 512000)
(setq mew-log-max-size 100000)


・ローテーション後のログ・ファイルの圧縮を行わない場合
 (setq mew-log-compression nil)

・ローテーション後のログ・ファイルの圧縮に bzip2 を使用する場合
 (setq mew-log-compress-cmd "bzip2")



nice!(2)  コメント(0)  トラックバック(0) 
共通テーマ:パソコン・インターネット

nice! 2

コメント 0

コメントを書く

お名前:[必須]
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

※ブログオーナーが承認したコメントのみ表示されます。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。