Skip to content

Commit e24ba78

Browse files
committed
properly handle stdout, stderr, and null (#1029)
* properly handle stdout, stderr, and null * update snapshots * fix test
1 parent 4267453 commit e24ba78

9 files changed

+26
-13
lines changed

cmd/root.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,25 @@ func setupLogger(atmosConfig *schema.AtmosConfiguration) {
101101
log.SetLevel(log.InfoLevel)
102102
}
103103

104-
if atmosConfig.Logs.File != "/dev/stderr" {
104+
var output io.Writer
105+
106+
switch atmosConfig.Logs.File {
107+
case "/dev/stderr":
108+
output = os.Stderr
109+
case "/dev/stdout":
110+
output = os.Stdout
111+
case "/dev/null":
112+
output = io.Discard // More efficient than opening os.DevNull
113+
default:
105114
logFile, err := os.OpenFile(atmosConfig.Logs.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o666)
106115
if err != nil {
107116
log.Fatal("Failed to open log file:", err)
108117
}
109118
defer logFile.Close()
110-
log.SetOutput(logFile)
119+
output = logFile
111120
}
121+
122+
log.SetOutput(output)
112123
}
113124

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

185196
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")
186-
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'")
197+
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'")
187198

188199
// Set custom usage template
189200
err := templates.SetCustomUsageFunc(RootCmd)

pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var (
7575
BasePath: "stacks/workflows",
7676
},
7777
Logs: schema.Logs{
78-
File: "/dev/stdout",
78+
File: "/dev/stderr",
7979
Level: "Info",
8080
},
8181
Schemas: schema.Schemas{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
logs:
22
level: Trace
3-
file: /dev/stdout
3+
file: /dev/stderr
44

55
stacks:
66
base_path: stacks
77
included_paths:
8-
- "**/*"
8+
- "**/*"

tests/snapshots/TestCLICommands_Valid_Log_Level_in_Config_File.stderr.golden

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/snapshots/TestCLICommands_Valid_Log_Level_in_Environment_Variable.stderr.golden

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/snapshots/TestCLICommands_atmos_--help.stdout.golden

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/snapshots/TestCLICommands_atmos_validate_editorconfig_--help.stdout.golden

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/snapshots/TestCLICommands_atmos_validate_editorconfig_help.stdout.golden

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test-cases/log-level-validation.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ tests:
5454
stdout:
5555
- '^\n👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9_]+\n\n$'
5656
stderr:
57-
- "^$"
57+
# This is actually an invalid debug statement, we are not passing the log level in the command line
58+
- "DEBU Using command line argument"
5859
exit_code: 0
5960

6061
- name: "Valid Log Level in Environment Variable"

0 commit comments

Comments
 (0)