日期:2014-05-16  浏览次数:20634 次

linux 日志保存到文件函数同时还可以显示在屏幕上
set_log()
{
        LOGFILE=${LOGFILE:-contrail.log}
        VERBOSE=${VERBOSE:-True}
        if [[ -n "$LOGFILE" ]]; then

            # Redirect output according to config

            # Copy stdout to fd 3
            exec 3>&1
            if [[ "$VERBOSE" == "True" ]]; then
                # Redirect stdout/stderr to tee to write the log file
                exec 1> >( awk '
                        {
                            cmd ="date +\"%Y-%m-%d %H:%M:%S \""
                            cmd | getline now
                            close("date +\"%Y-%m-%d %H:%M:%S \"")
                            sub(/^/, now)
                            print
                            fflush()
                        }' | tee "${LOGFILE}" ) 2>&1
                # Set up a second fd for output
                #exec 6> >( tee "${SUMFILE}" )
            else
                # Set fd 1 and 2 to primary logfile
                exec 1> "${LOGFILE}" 2>&1
                # Set fd 6 to summary logfile and stdout
                #exec 6> >( tee "${SUMFILE}" >&3 )
            fi

        else
            # Set up output redirection without log files
            # Copy stdout to fd 3
            exec 3>&1
            if [[ "$VERBOSE" != "True" ]]; then
                # Throw away stdout and stderr
                exec 1>/dev/null 2>&1
            fi
        fi
}