Skip to content

Strip escape codes out of logs #2742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ permalink: /docs/en-US/changelog/
* Fixed uses of config.vm instead of override.vm in the Vagrantfile ( #2754 )
* Added nonempty to mount_options for Parallels, and added mount_options to all synced folders ( #2757 )
* Added Xdebug port mapping for the docker provider ( #2748 )
* Provisioner logs no longer log escape codes ( #2742 )

### Maintenance

Expand Down
66 changes: 50 additions & 16 deletions provision/provision-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,57 @@ export -f network_check
#
# @arg $1 string name of the provisioner
function log_to_file() {
local date_time
local provisioner="$1"
local date_time

if [[ ! -s /vagrant/provisioned_at ]]; then
echo "Error: /vagrant/provisioned_at is missing or empty" >&2
return 1
fi

date_time=$(cat /vagrant/provisioned_at)
local logfolder="/var/log/provisioners/${date_time}"
local logfile="${logfolder}/${1}.log"
mkdir -p "${logfolder}"
touch "${logfile}"
# reset output otherwise it will log to previous files. from backup made in provisioners.sh
exec 1>&6
exec 2>&7
# pipe to file
if [[ "${1}" == "provisioner-main" ]]; then
exec > >( tee -a "${logfile}" ) # main provisioner outputs everything
else
exec > >( tee -a "${logfile}" >/dev/null ) # others, only stderr
fi
exec 2> >( tee -a "${logfile}" >&2 )
VVV_CURRENT_LOG_FILE="${logfile}"
local logfolder="/var/log/provisioners/${date_time}"
local logfile="${logfolder}/${provisioner}.log"


mkdir -p "${logfolder}" || return 1
touch "${logfile}" || return 1

# reset output otherwise it will log to previous files. from backup made in provisioners.sh
exec 1>&6
exec 2>&7

local SED_STRIP_ANSI='s/\x1B\[[0-9;]*[a-zA-Z]//g'

# pipe to file
if [[ "${provisioner}" == "provisioner-main" ]]; then
# Preserve color in terminal, strip in log
exec > >(
while IFS= read -r line; do
printf '%s\n' "$line" | sed -r "${SED_STRIP_ANSI}" >> "${logfile}"
printf '%s\n' "$line"
done
)
else
# Suppress stdout to terminal but log stripped version
exec > >(
while IFS= read -r line; do
printf '%s\n' "$line" | sed -r "${SED_STRIP_ANSI}" >> "${logfile}"
done
)
fi

# stderr: preserve color in terminal, strip in log
exec 2> >(
while IFS= read -r line; do
printf '%s\n' "$line" | sed -r "${SED_STRIP_ANSI}" >> "${logfile}"
printf '%s\n' "$line" >&2
done
)

VVV_CURRENT_LOG_FILE="${logfile}"

return 0
}
export -f log_to_file

Expand Down