LoginSignup
6
3

More than 5 years have passed since last update.

tmuxで自動でペイン毎のログ取る時のファイル数に閾値を設ける。

Last updated at Posted at 2018-06-19

なにこれ

tmuxでペイン毎にログとりたいけど、ログファイル数が一定量いったら消したい時に*rcに書く的なそれ。
消すタイミングは、zshrcを起動するタイミングなのでcronもいりません。
調べて先人様のやつをアレンジして作りました。

先人様

tmuxを使いながらログを取る
tmuxメモ : Tmux Loggingでロギングとキャプチャ

こうやる

bashrcかzshrc
########################################
# tmuxの設定
# 自動ロギング
if [[ $TERM = screen ]] || [[ $TERM = screen-256color ]] ; then
    local LOGDIR=$HOME/Documents/term_logs
    local LOGFILE=$(hostname)_$(date +%Y-%m-%d_%H%M%S_%N.log)
    local FILECOUNT=0
    local MAXFILECOUNT=500 #ここを好きな保存ファイル数に変える。
    # zsh起動時に自動で$MAXFILECOUNTのファイル数以上ログファイルあれば消す
    for file in `\find "$LOGDIR" -maxdepth 1 -type f -name "*.log" | sort --reverse`; do
        FILECOUNT=`expr $FILECOUNT + 1`
        if [ $FILECOUNT -ge $MAXFILECOUNT ]; then
            rm -f $file
        fi
    done
    [ ! -d $LOGDIR ] && mkdir -p $LOGDIR
    tmux  set-option default-terminal "screen" \; \
    pipe-pane        "cat >> $LOGDIR/$LOGFILE" \; \
    display-message  "💾Started logging to $LOGDIR/$LOGFILE"
fi

########################################

これでtmuxのログのファイル数に制限を掛けれます。
これでいいならプラグインなしでロギングできます。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3