@@ -465,25 +465,46 @@ APACHE_GROUP=${APACHE_GROUP:-$APACHE_USER}
465465# Set LOGFILE to turn on logging
466466# We append '.xxxxxxxx' to the given name to maintain history
467467# where xxxxxxxx is a representation of the date the file was created
468+ if [[ -n " $LOGFILE " || -n " $SCREEN_LOGDIR " ]]; then
469+ LOGDAYS=${LOGDAYS:- 7}
470+ TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:- " %F-%H%M%S" }
471+ CURRENT_LOG_TIME=$( date " +$TIMESTAMP_FORMAT " )
472+ fi
473+
468474if [[ -n " $LOGFILE " ]]; then
469475 # First clean up old log files. Use the user-specified LOGFILE
470476 # as the template to search for, appending '.*' to match the date
471477 # we added on earlier runs.
472- LOGDAYS=${LOGDAYS:- 7}
473478 LOGDIR=$( dirname " $LOGFILE " )
474479 LOGNAME=$( basename " $LOGFILE " )
475480 mkdir -p $LOGDIR
476481 find $LOGDIR -maxdepth 1 -name $LOGNAME .\* -mtime +$LOGDAYS -exec rm {} \;
477482
478- TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:- " %F-%H%M%S" }
479- LOGFILE=$LOGFILE .$( date " +$TIMESTAMP_FORMAT " )
483+ LOGFILE=$LOGFILE .${CURRENT_LOG_TIME}
480484 # Redirect stdout/stderr to tee to write the log file
481485 exec 1> >( tee " ${LOGFILE} " ) 2>&1
482486 echo " stack.sh log $LOGFILE "
483487 # Specified logfile name always links to the most recent log
484488 ln -sf $LOGFILE $LOGDIR /$LOGNAME
485489fi
486490
491+ # Set up logging of screen windows
492+ # Set SCREEN_LOGDIR to turn on logging of screen windows to the
493+ # directory specified in SCREEN_LOGDIR, we will log to the the file
494+ # screen-$SERVICE_NAME-$TIMESTAMP.log in that dir and have a link
495+ # screen-$SERVICE_NAME.log to the latest log file.
496+ # Logs are kept for as long specified in LOGDAYS.
497+ if [[ -n " $SCREEN_LOGDIR " ]]; then
498+
499+ # We make sure the directory is created.
500+ if [[ -d " $SCREEN_LOGDIR " ]]; then
501+ # We cleanup the old logs
502+ find $SCREEN_LOGDIR -maxdepth 1 -name screen-\* .log -mtime +$LOGDAYS -exec rm {} \;
503+ else
504+ mkdir -p $SCREEN_LOGDIR
505+ fi
506+ fi
507+
487508# So that errors don't compound we exit on any errors so you see only the
488509# first error that occurred.
489510trap failed ERR
@@ -1357,6 +1378,12 @@ function screen_it {
13571378 # creating a new window in screen and then sends characters, so if
13581379 # bash isn't running by the time we send the command, nothing happens
13591380 sleep 1.5
1381+
1382+ if [[ -n ${SCREEN_LOGDIR} ]]; then
1383+ screen -S stack -p $1 -X logfile ${SCREEN_LOGDIR} /screen-${1} .${CURRENT_LOG_TIME} .log
1384+ screen -S stack -p $1 -X log on
1385+ ln -sf ${SCREEN_LOGDIR} /screen-${1} .${CURRENT_LOG_TIME} .log ${SCREEN_LOGDIR} /screen-${1} .log
1386+ fi
13601387 screen -S stack -p $1 -X stuff " $2 $NL "
13611388 fi
13621389}
0 commit comments