Home Projects Blog

How to log like me

Logging is important. It can save the day. Here's how to log like me.

Windows

Make your prompt look like this: DD/MM/YYYY 21:18:25 user pwd >

By doing this:


  function prompt { write-host $(get-date)$(whoami) $(pwd)'>' -nonewline ; return " "}
  #OR 
  echo "function prompt { write-host $(get-date)$(pwd)'>' -nonewline ; return `" `"}" > C:\Users\AAAA\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 
          
        

Log your output with Start-Transcript and Stop-Transcript.

Linux

Make your prompt look like this - [DD-MMM-YY 21:30:40] user@host:pwd >

By doing this:

        
  export PS1='[`date  +"%d-%b-%y %T"`] \u@\h:\w > '

  export PS1='[`date  +"%d-%b-%y %T"`] \u@\h\[\033[01;31m\] `DGTWINT=$(ip route get 1.1.1.1 | grep via | cut -f 5 -d " ") && FASTINT=$(grep ^ /dev/null  /sys/class/net/*/speed 2> /dev/null | sort -t: -k 2 | tail -1 | cut -f 5 -d "/") && if [ -n "${DGTWINT}" ] ; then TESTINT="${DGTWINT}"; else TESTINT="${FASTINT}"; fi && IPADDR=$(ip addr show dev "${TESTINT}" | grep "inet " | sed -e "s/.*inet //" | cut -f 1 -d "/") && echo "${TESTINT:0:1}:${IPADDR}"`\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\] > '

  #OR 

  cat << EOF >> ~/.bashrc
  export PS1='[`date  +"%d-%b-%y %T"`] \u@\h:\w > '
  EOF

  cat << EOF >> ~/.bashrc
  export PS1='[`date  +"%d-%b-%y %T"`] \u@\h\[\033[01;31m\] `DGTWINT=$(ip route get 1.1.1.1 | grep via | cut -f 5 -d " ") && FASTINT=$(grep ^ /dev/null  /sys/class/net/*/speed 2> /dev/null | sort -t: -k 2 | tail -1 | cut -f 5 -d "/") && if [ -n "${DGTWINT}" ] ; then TESTINT="${DGTWINT}"; else TESTINT="${FASTINT}"; fi && IPADDR=$(ip addr show dev "${TESTINT}" | grep "inet " | sed -e "s/.*inet //" | cut -f 1 -d "/") && echo "${TESTINT:0:1}:${IPADDR}"`\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\] > '
  EOF

  #WITH 
  
  source ~/.bashrc

  . ~/.bashrc
      
    

Log your output with: script -a logfilename.txt

If you try and view the script file anywhere but in your terminal, it will also print out the ANSI colour codes and random control characters. You can try and filter them out with:

| sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,3})*)?[mGK]//g" | tr -dc '[[:print:][:space:]]'

but it's not a great fix.