Why is logging a useful practice:

  • Good idea to log network sessions output into a text file, from troubleshooting and technical point of view
  • In case of troubleshooting a reliable source to review the sequence of commands and its outcome
  • Task documentation at its source

A Z-shell zsh function to get logging session instantly by calling function with a mandatory parameter logfile :

function logfile(){
        # Give the session name parameter
        readonly sessionname=${1:?" Syntax: logfile sessionname"}
        # Set the datum variable, change the dir, print status.
        local datum=$(date '+%Y%m%d-%H%M%S')
        cd ~/netlog/
        echo "logging to $datum-$1.log file. Type exit to end the netlog session!"
        # Record the session
        script -c '/bin/sh --login' $datum-$1.log
}
user % logfile change-4711

logging to 20200427-222535-change-4711.log file. Type exit to end the netlog session! Script started, file is 20200427-222535-change-4711.log sh-4.4 $ ssh R1 ...

Ending the logging session is notified. End by using the exit command:

user $ exit

Script done, file is 20200427-222535-change-4711.log ...

Log files will look like in example below. This is also useful if greping for some a valuable piece of information:

user % ~/netlog % ls -lah

total 293K drwxr-xr-x 2 user user 4.0K Apr 27 22:25 . drwxr-xr-x 162 user user 16K Apr 27 22:29 .. -rw-r--r-- 1 user user 33.8K Apr 26 17:02 20200426-170201-change-6500.log -rw-r--r-- 1 user user 21.9K Apr 26 17:07 20200426-170739-ticket-123401.log -rw-r--r-- 1 user user 27.6K Apr 26 17:08 20200426-170848-ticket-130813.log -rw-r--r-- 1 user user 42.2K Apr 26 17:11 20200426-171108-ticket-130931.log -rw-r--r-- 1 user user 88.8K Apr 26 17:13 20200426-171214-change-5520.log -rw-r--r-- 1 user user 77.9K Apr 27 22:27 20200427-222535-change-4711.log

user % logfile

logfile:2: 1: Syntax: logfile sessionname

📄 File ~/.profile

TERM=vt100 PS1='\t ~> '

user % logfile change-4711

logging to 20200428-162231-change-4712.log file. Type exit to end the netlog session! Script started, file is 20200428-162231-change-4712.log 16:22:31 ~> ssh R0 ...

The same result can be accomplished differently, f.e. by using the tee command or logging by using screen with a logging parameter. Find your own solution.