Skip to content
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

gum log is always outputted to stderr #677

Open
MokkaCicc opened this issue Sep 14, 2024 · 3 comments
Open

gum log is always outputted to stderr #677

MokkaCicc opened this issue Sep 14, 2024 · 3 comments

Comments

@MokkaCicc
Copy link

Describe the bug
The output of gum log is always outputted to stderr, this can be frustrating to work with inside bash scripts as error handling is polluted with logs.
Also, other bash feature don't work as expected: gum log cannot be manipulated with |, >, >>, 1>, ...

To redirect to a file, the -o flag is here (or we can use 2>/2>>) but this is not flexible enough for some scripting usages.

To Reproduce
Steps to reproduce the behavior:

  1. Run the command gum log "This is a test" > out (Log level don't change anything)
  2. Nothing inside the out file and the log was outputted in the TTY

Expected behavior
The output of gum log should be manipulable with |, >, >>, 1>, ...

Desktop (please complete the following information):

  • OS: Debian 12
  • Gum: v0.14.5 (1917023)
@piero-vic
Copy link
Contributor

You can redirect to /dev/stdout with the -o flag.

gum log "This is a test" -o /dev/stdout > out

@MokkaCicc
Copy link
Author

You can redirect to /dev/stdout with the -o flag.

gum log "This is a test" -o /dev/stdout > out

Another solution I am using in my script is using 2>&1 after each gum log to redirect the stderr to stdout. But either your solution and my solution is not very practical when scripting. And not very logical, I lost a couple hours bashing my head on why my script wasn't working, while it was just the log that was not outputted were I was expecting them to go.

@pythoninthegrass
Copy link

pythoninthegrass commented Oct 2, 2024

I lost a couple hours bashing my head

Amusing choice in diction haha (emphasis mine).

Agreed that it'd be nice if it automatically chose the right stdout/stderr path. Both 2>&1 and -o /dev/stdout > out are sufficient for my purposes 🙌

EDIT

FWIW this is how I'm using gum log:

#!/usr/bin/env bash

# Function to log using gum
logger() {
	local level msg OPTIND log_file log_dir log_path
	level=""
	msg=""
	log_dir="/tmp"
	log_file="gum.log"
	log_path="${log_dir}/${log_file}"

	while getopts ":l:" opt; do
		case ${opt} in
			l )
				level=$OPTARG
				;;
			\? )
				echo "Invalid option: $OPTARG" 1>&2
				return 1
				;;
			: )
				echo "Invalid option: $OPTARG requires an argument" 1>&2
				return 1
				;;
		esac
	done
	shift $((OPTIND -1))

	msg="$1"

	gum_log() {
		local cmd
		cmd=(gum log --time rfc1123 --structured)
		case "$level" in
			"")
				cmd+=("$msg")
				;;
			"debug" | "info" | "warn" | "error" | "fatal")
				cmd+=("--level" "$level" "$msg")
				;;
			*)
				cmd+=("--level" "error" "Invalid log level: $level")
				;;
		esac
		# "${cmd[@]}" 2>&1
		"${cmd[@]}" -o /dev/stdout
	}
	case "${LOG:-}" in
		stdout)
			gum_log
			;;
		log)
			gum_log >> "$log_path"
			;;
		both)
			gum_log | tee -a "$log_path"
			;;
		"")
			gum_log
			;;
		*)
			echo "Invalid LOG option: ${LOG}. Using 'none'." >&2
			;;
	esac
}

LOG=both
n=42
logger -l info "The secret to life is: $n"

https://gist.github.com/pythoninthegrass/e763dfc659e2501d5b3c1a2cd1278ded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants