Skip to content

Commit

Permalink
✨ can log to a new file per session
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed May 3, 2024
1 parent 8bd9345 commit 4d19249
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 10 deletions.
22 changes: 22 additions & 0 deletions tests.d/1100-self-config/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ VALET_CONFIG_ENABLE_LOG_TIMESTAMP="${VALET_CONFIG_ENABLE_LOG_TIMESTAMP:-}"
# The file descriptor to use for the logs (default to 2 to output to stderr).
VALET_CONFIG_LOG_FD="${VALET_CONFIG_LOG_FD:-}"
# A path to directory in which we will create 1 log file per valet execution, which
# will contain the valet logs.
VALET_CONFIG_LOG_TO_DIRECTORY="${VALET_CONFIG_LOG_TO_DIRECTORY:-}"
# A string that will be evaluated to set a variable 'logFile' which represents
# the name of the file in which to write the logs.
# Only used if VALET_CONFIG_LOG_TO_DIRECTORY is set.
# The default is equivalent to setting this string to:
# printf -v logFile '%s%(%F_%Hh%Mm%Ss)T%s' 'valet-' ${EPOCHSECONDS} '.log'
VALET_CONFIG_LOG_FILENAME_PATTERN="${VALET_CONFIG_LOG_FILENAME_PATTERN:-}"
# -----------
# Log icons configuration
# -----------
Expand Down Expand Up @@ -342,6 +353,17 @@ VALET_CONFIG_ENABLE_LOG_TIMESTAMP="${VALET_CONFIG_ENABLE_LOG_TIMESTAMP:-true}"
# The file descriptor to use for the logs (default to 2 to output to stderr).
VALET_CONFIG_LOG_FD="${VALET_CONFIG_LOG_FD:-}"
# A path to directory in which we will create 1 log file per valet execution, which
# will contain the valet logs.
VALET_CONFIG_LOG_TO_DIRECTORY="${VALET_CONFIG_LOG_TO_DIRECTORY:-}"
# A string that will be evaluated to set a variable 'logFile' which represents
# the name of the file in which to write the logs.
# Only used if VALET_CONFIG_LOG_TO_DIRECTORY is set.
# The default is equivalent to setting this string to:
# printf -v logFile '%s%(%F_%Hh%Mm%Ss)T%s' 'valet-' ${EPOCHSECONDS} '.log'
VALET_CONFIG_LOG_FILENAME_PATTERN="${VALET_CONFIG_LOG_FILENAME_PATTERN:-}"
# -----------
# Log icons configuration
# -----------
Expand Down
31 changes: 31 additions & 0 deletions tests.d/1300-valet-cli/05.logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,40 @@ function resetLogOptions() {
export GLOBAL_COLUMNS=120
}

function testFileLogging() {
io::createTempDirectory
logDirectory="${RETURNED_VALUE}"

echo "→ VALET_CONFIG_LOG_TO_DIRECTORY=${logDirectory} VALET_CONFIG_DISABLE_LOG_TIME=true valet self mock1 logging-level"
resetLogOptions
(VALET_CONFIG_LOG_TO_DIRECTORY="${logDirectory}" VALET_CONFIG_DISABLE_LOG_TIME=true "${GLOBAL_VALET_HOME}/valet" self mock1 logging-level)
echo
echo "→ io::countArgs ${logDirectory}/*"
io::countArgs "${logDirectory}"/*
echo "${RETURNED_VALUE}"
endTest "Testing that we can output the logs to a file additionally to console" 0

echo "→ VALET_CONFIG_LOG_FILENAME_PATTERN='logFile=test.log' VALET_CONFIG_LOG_TO_DIRECTORY=${logDirectory} VALET_CONFIG_DISABLE_LOG_TIME=true valet self mock1 logging-level"
resetLogOptions
(VALET_CONFIG_LOG_FILENAME_PATTERN='logFile=test.log' VALET_CONFIG_LOG_TO_DIRECTORY="${logDirectory}" VALET_CONFIG_DISABLE_LOG_TIME=true "${GLOBAL_VALET_HOME}/valet" self mock1 logging-level)
echo
echo "→ cat ${logDirectory}/test.log"
io::cat "${logDirectory}/test.log"
endTest "Testing that we can output the logs to a specific file name additionally to console" 0

echo "→ VALET_CONFIG_LOG_FD=${logDirectory}/test2.log VALET_CONFIG_DISABLE_LOG_TIME=true valet self mock1 logging-level"
resetLogOptions
(VALET_CONFIG_LOG_FD="${logDirectory}/test2.log" VALET_CONFIG_DISABLE_LOG_TIME=true "${GLOBAL_VALET_HOME}/valet" self mock1 logging-level)
echo
echo "→ cat ${logDirectory}/test2.log"
io::cat "${logDirectory}/test2.log"
endTest "Testing that we can output the logs to a file directly instead of the console err stream" 0
}

function main() {
testLogging
testLog::printRawAndFile
testFileLogging
}

main
Expand Down
89 changes: 87 additions & 2 deletions tests.d/1300-valet-cli/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,91 @@ INFO This is to test the printFile function from a string.
░ d upon is being carried out.
```

### Testing that we can output the logs to a file additionally to console

Exit code: `0`

**Standard** output:

```plaintext
→ VALET_CONFIG_LOG_TO_DIRECTORY=/tmp/valet.d/d2-0 VALET_CONFIG_DISABLE_LOG_TIME=true valet self mock1 logging-level
→ io::countArgs /tmp/valet.d/d2-0/*
1
```

**Error** output:

```log
TRACE This is an error trace message which is always displayed.
INFO This is an info message with a super long sentence. The value of life is not in its duration, but in its
donation. You are not important because of how long you live, you are important because of how effective you
live. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
Surround yourself with the best people you can find, delegate authority, and don't interfere as long as the
policy you've decided upon is being carried out.
SUCCESS This is a success message.
WARNING This is a warning message.
With a second line.
```

### Testing that we can output the logs to a specific file name additionally to console

Exit code: `0`

**Standard** output:

```plaintext
→ VALET_CONFIG_LOG_FILENAME_PATTERN='logFile=test.log' VALET_CONFIG_LOG_TO_DIRECTORY=/tmp/valet.d/d2-0 VALET_CONFIG_DISABLE_LOG_TIME=true valet self mock1 logging-level
→ cat /tmp/valet.d/d2-0/test.log
TRACE This is an error trace message which is always displayed.
INFO This is an info message with a super long sentence. The value of life is not in its duration, but in its
donation. You are not important because of how long you live, you are important because of how effective you
live. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
Surround yourself with the best people you can find, delegate authority, and don't interfere as long as the
policy you've decided upon is being carried out.
SUCCESS This is a success message.
WARNING This is a warning message.
With a second line.
```

**Error** output:

```log
TRACE This is an error trace message which is always displayed.
INFO This is an info message with a super long sentence. The value of life is not in its duration, but in its
donation. You are not important because of how long you live, you are important because of how effective you
live. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
Surround yourself with the best people you can find, delegate authority, and don't interfere as long as the
policy you've decided upon is being carried out.
SUCCESS This is a success message.
WARNING This is a warning message.
With a second line.
```

### Testing that we can output the logs to a file directly instead of the console err stream

Exit code: `0`

**Standard** output:

```plaintext
→ VALET_CONFIG_LOG_FD=/tmp/valet.d/d2-0/test2.log VALET_CONFIG_DISABLE_LOG_TIME=true valet self mock1 logging-level
→ cat /tmp/valet.d/d2-0/test2.log
TRACE This is an error trace message which is always displayed.
INFO This is an info message with a super long sentence. The value of life is not in its duration, but in its
donation. You are not important because of how long you live, you are important because of how effective you
live. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
Surround yourself with the best people you can find, delegate authority, and don't interfere as long as the
policy you've decided upon is being carried out.
SUCCESS This is a success message.
WARNING This is a warning message.
With a second line.
```

## Test script 06.misc

### Testing version option
Expand Down Expand Up @@ -744,9 +829,9 @@ Exit code: `1`
**Error** output:

```log
INFO The valet user directory ⌜/tmp/valet.d/d2-0/non-existing⌝ does not contain a built ⌜commands⌝ file.
INFO The valet user directory ⌜/tmp/valet.d/d3-0/non-existing⌝ does not contain a built ⌜commands⌝ file.
Now building it using ⌜valet self build⌝ command.
WARNING Skipping user directory ⌜/tmp/valet.d/d2-0/non-existing⌝ because it does not exist.
WARNING Skipping user directory ⌜/tmp/valet.d/d3-0/non-existing⌝ because it does not exist.
TRACE This is an error trace message which is always displayed.
INFO This is an info message with a super long sentence. The value of life is not in its duration, but in its donation. You are not important because of how long you live, you are important because of how effective you live. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime. Surround yourself with the best people you can find, delegate authority, and don't interfere as long as the policy you've decided upon is being carried out.
SUCCESS This is a success message.
Expand Down
11 changes: 11 additions & 0 deletions valet.d/commands.d/self-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ VALET_CONFIG_ENABLE_LOG_TIMESTAMP=\"\${VALET_CONFIG_ENABLE_LOG_TIMESTAMP:-${EXPO
# The file descriptor to use for the logs (default to 2 to output to stderr).
VALET_CONFIG_LOG_FD=\"\${VALET_CONFIG_LOG_FD:-${EXPORTED_VALET_CONFIG_LOG_FD:-}}\"
# A path to directory in which we will create 1 log file per valet execution, which
# will contain the valet logs.
VALET_CONFIG_LOG_TO_DIRECTORY=\"\${VALET_CONFIG_LOG_TO_DIRECTORY:-${EXPORTED_VALET_CONFIG_LOG_TO_DIRECTORY:-}}\"
# A string that will be evaluated to set a variable 'logFile' which represents
# the name of the file in which to write the logs.
# Only used if VALET_CONFIG_LOG_TO_DIRECTORY is set.
# The default is equivalent to setting this string to:
# printf -v logFile '%s%(%F_%Hh%Mm%Ss)T%s' 'valet-' \${EPOCHSECONDS} '.log'
VALET_CONFIG_LOG_FILENAME_PATTERN=\"\${VALET_CONFIG_LOG_FILENAME_PATTERN:-${EXPORTED_VALET_CONFIG_LOG_FILENAME_PATTERN:-}}\"
# -----------
# Log icons configuration
# -----------
Expand Down
35 changes: 28 additions & 7 deletions valet.d/core
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,27 @@ function log::createPrintFunction() {
printf "%s" "${toPrint:-}" 1>>'"${VALET_CONFIG_LOG_FD}"
fi

# check if we need to additionally output the logs to a file
if [[ -n ${VALET_CONFIG_LOG_TO_DIRECTORY:-} ]]; then
# compute the name of the output file
local logFile
if [[ -z ${VALET_CONFIG_LOG_FILENAME_PATTERN:-} ]]; then
printf -v logFile "%s%(%F_%Hh%Mm%Ss)T%s" "valet-" "${EPOCHSECONDS}" ".log"
else
eval "${VALET_CONFIG_LOG_FILENAME_PATTERN}"
fi
if [[ -n ${logFile:-} ]]; then
if [[ ! -d "${VALET_CONFIG_LOG_TO_DIRECTORY}" ]]; then
mkdir -p "${VALET_CONFIG_LOG_TO_DIRECTORY}" 1>/dev/null
fi
# append the log to the file
printStatement+='
printf "'"${printfFormat}"'" '"${printfArguments}"' 1>>'"${VALET_CONFIG_LOG_TO_DIRECTORY%/}/${logFile}"
standardPrintStatement+='
printf "%s" "${toPrint:-}" 1>>'"${VALET_CONFIG_LOG_TO_DIRECTORY%/}/${logFile}"
fi
fi

# string with the number of spaces necessary to align with other log messages
local messageLeftPadding
messageLeftPadding=""
Expand Down Expand Up @@ -482,7 +503,7 @@ function log::error() {
# Usage:
# log::warning "This is a warning message."
function log::warning() {
if ((${LOG_LEVEL_INT:-1} > 3)); then
if ((${LOG_LEVEL_INT:-1} > 3)); then
return 0
fi
log::print "WARNING" "${VALET_CONFIG_ICON_WARNING:-$'\uf071'}" "WARNING" "$@"
Expand All @@ -496,7 +517,7 @@ function log::warning() {
# Usage:
# log::success "This is a success message."
function log::success() {
if ((${LOG_LEVEL_INT:-1} > 2)); then
if ((${LOG_LEVEL_INT:-1} > 2)); then
return 0
fi
log::print "SUCCESS" "${VALET_CONFIG_ICON_SUCCESS:-$'\uf14a'}" "SUCCESS" "$@"
Expand All @@ -510,7 +531,7 @@ function log::success() {
# Usage:
# log::info "This is an info message."
function log::info() {
if ((${LOG_LEVEL_INT:-1} > 1)); then
if ((${LOG_LEVEL_INT:-1} > 1)); then
return 0
fi
log::print "INFO" "${VALET_CONFIG_ICON_INFO:-$'\uf05a'}" "INFO" "$@"
Expand All @@ -524,7 +545,7 @@ function log::info() {
# Usage:
# log::debug "This is a debug message."
function log::debug() {
if ((${LOG_LEVEL_INT:-1} > 0)); then
if ((${LOG_LEVEL_INT:-1} > 0)); then
return 0
fi
log::print "DEBUG" "${VALET_CONFIG_ICON_DEBUG:-$'\uf188'}" "DEBUG" "$@"
Expand Down Expand Up @@ -595,11 +616,11 @@ function log::printCallStack() {
for ((i = levelToSkip; i < stackSize; i++)); do
func="${FUNCNAME[${i}]}"
if [[ -z ${func} ]]; then
func=ROOT;
func=ROOT
else
func="${func}()"
fi
if (( i > 0 )); then
if ((i > 0)); then
linen="${BASH_LINENO[$((i - 1))]}"
fi
src="${BASH_SOURCE[${i}]}"
Expand All @@ -610,7 +631,7 @@ function log::printCallStack() {
elif [[ -f "${src/#.\//"${GLOBAL_VALET_HOME}"\/}" ]]; then
src="${src/#.\//"${GLOBAL_VALET_HOME}"\/}"
fi
if (( i == stackSize - 1)); then
if ((i == stackSize - 1)); then
treeString="└─"
fi
log::printString "${treeString} in ${func} at ${src}:${linen}" ""
Expand Down
2 changes: 1 addition & 1 deletion valet.d/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.13.2
0.13.49

0 comments on commit 4d19249

Please sign in to comment.