N equivalent of GNU Screen’s “log” command in tmux

gnu-screenlogstmux

I make heavy use of screen's "log" command to log the output of a session to a file, when I am making changes in a given environment. I searched through tmux's man page, but couldn't find an equivalent. Is anyone aware of a similar feature in tmux, or do I have to write my own wrapper scripts to do this?

EDIT: I'm aware of 'script' and other utilities that allow me to log a session. The reason that screen's functionality is so useful is the ability to define a logfile variable which uses string escapes to uniquely identify each session.

e.g. I have a shell function which, given a hostname, will SSH to that host in a new screen window and set the window title to the hostname. When I start a log of that session, it is prefixed with the window title.

If this functionality doesn't exist in tmux, I'll have to create a new set of shell functions to set up 'scripts' of sessions I want to log. This isn't hugely difficult, but it may not be worth the effort given that screen does exactly what I need already.

Best Answer

Let me see if I have deciphered your screen configuration correctly:

  • You use something like logfile "%t-screen.log" (probably in a .screenrc file) to configure the name of the log file that will be started later.
  • You use the title <hostname> (C-a A) screen command to set the title of a new window, or
    you do screen -t <hostname> ssh0 <hostname> to start a new screen session.
  • You use the C-a H (C-a :log) screen command to toggle logging to the configured file.

If so, then is nearly equivalent (requires tmux 1.3+ to support #W in the pipe-pane shell command; pipe-pane is available in tmux 1.0+):

  • In a configuration file (e.g. .tmux.conf):

    bind-key H pipe-pane -o "exec cat >>$HOME/'#W-tmux.log'"
    
  • Use tmux rename-window <hostname> (C-b ,) to rename an existing window, or
    use tmux new-window -n <hostname> 'ssh <hostname>' to start a new tmux window, or
    use tmux new-session -n <hostname> 'ssh <hostname>' to start a new tmux session.
  • Use C-b H to toggle the logging.

There is no notification that the log has been toggled, but you could add one if you wanted:

bind-key H pipe-pane -o "exec cat >>$HOME/'#W-tmux.log'" \; display-message 'Toggled logging to $HOME/#W-tmux.log'

Note: The above line is shown as if it were in a configuration file (either .tmux.conf or one you source). tmux needs to see both the backslash and the semicolon; if you want to configure this from the a shell (e.g. tmux bind-key …), then you will have to escape or quote both characters appropriately so that they are delivered to tmux intact. There does not seem to be a convenient way to show different messages for toggling on/off when using only a single binding (you might be able to rig something up with if-shell, but it would probably be ugly). If two bindings are acceptable, then try this:

bind-key H pipe-pane "exec cat >>$HOME/'#W-tmux.log'" \; display-message 'Started logging to $HOME/#W-tmux.log'
bind-key h pipe-pane \; display-message 'Ended logging to $HOME/#W-tmux.log'
Related Question