Skip to content

Commit

Permalink
properly handle stdout, stderr, and null (#1029)
Browse files Browse the repository at this point in the history
* properly handle stdout, stderr, and null

* update snapshots

* fix test
  • Loading branch information
Cerebrovinny committed Feb 9, 2025
1 parent 4267453 commit e24ba78
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 13 deletions.
17 changes: 14 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,25 @@ func setupLogger(atmosConfig *schema.AtmosConfiguration) {
log.SetLevel(log.InfoLevel)
}

if atmosConfig.Logs.File != "/dev/stderr" {
var output io.Writer

switch atmosConfig.Logs.File {
case "/dev/stderr":
output = os.Stderr
case "/dev/stdout":
output = os.Stdout
case "/dev/null":
output = io.Discard // More efficient than opening os.DevNull
default:
logFile, err := os.OpenFile(atmosConfig.Logs.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o666)
if err != nil {
log.Fatal("Failed to open log file:", err)
}
defer logFile.Close()
log.SetOutput(logFile)
output = logFile
}

log.SetOutput(output)
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down Expand Up @@ -183,7 +194,7 @@ func init() {
"Errors can be redirected to any file or any standard file descriptor (including '/dev/null'): atmos <command> --redirect-stderr /dev/stdout")

RootCmd.PersistentFlags().String("logs-level", "Info", "Logs level. Supported log levels are Trace, Debug, Info, Warning, Off. If the log level is set to Off, Atmos will not log any messages")
RootCmd.PersistentFlags().String("logs-file", "/dev/stdout", "The file to write Atmos logs to. Logs can be written to any file or any standard file descriptor, including '/dev/stdout', '/dev/stderr' and '/dev/null'")
RootCmd.PersistentFlags().String("logs-file", "/dev/stderr", "The file to write Atmos logs to. Logs can be written to any file or any standard file descriptor, including '/dev/stdout', '/dev/stderr' and '/dev/null'")

// Set custom usage template
err := templates.SetCustomUsageFunc(RootCmd)
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var (
BasePath: "stacks/workflows",
},
Logs: schema.Logs{
File: "/dev/stdout",
File: "/dev/stderr",
Level: "Info",
},
Schemas: schema.Schemas{
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/scenarios/valid-log-level/atmos.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
logs:
level: Trace
file: /dev/stdout
file: /dev/stderr

stacks:
base_path: stacks
included_paths:
- "**/*"
- "**/*"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/snapshots/TestCLICommands_atmos_--help.stdout.golden

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/test-cases/log-level-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ tests:
stdout:
- '^\n👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9_]+\n\n$'
stderr:
- "^$"
# This is actually an invalid debug statement, we are not passing the log level in the command line
- "DEBU Using command line argument"
exit_code: 0

- name: "Valid Log Level in Environment Variable"
Expand Down

0 comments on commit e24ba78

Please sign in to comment.