Skip to content

Commit f8d5f18

Browse files
committed
feat(logs): improve output for server logs
1 parent 62b8c64 commit f8d5f18

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

internal/cmd/server/log/log.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8585
log := resp.GetOutput()
8686
lines := strings.Split(log, "\n")
8787

88-
if len(lines) > int(*model.Length) {
89-
// Truncate output and show most recent logs
90-
start := len(lines) - int(*model.Length)
91-
return outputResult(params.Printer, model.OutputFormat, serverLabel, strings.Join(lines[start:], "\n"))
88+
maxLines := int(*model.Length)
89+
if len(lines) <= maxLines {
90+
return outputResult(params.Printer, serverLabel, lines)
9291
}
9392

94-
return outputResult(params.Printer, model.OutputFormat, serverLabel, log)
93+
recentLogs := lines[len(lines)-maxLines:]
94+
return outputResult(params.Printer, serverLabel, recentLogs)
9595
},
9696
}
9797
configureFlags(cmd)
@@ -132,9 +132,15 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
132132
return apiClient.GetServerLog(ctx, model.ProjectId, model.Region, model.ServerId)
133133
}
134134

135-
func outputResult(p *print.Printer, outputFormat, serverLabel, log string) error {
136-
return p.OutputResult(outputFormat, log, func() error {
137-
p.Outputf("Log for server %q\n%s", serverLabel, log)
138-
return nil
139-
})
135+
func outputResult(p *print.Printer, serverLabel string, logLines []string) error {
136+
p.Outputf("Log for server %q\n", serverLabel)
137+
for _, line := range logLines {
138+
// Skip empty lines
139+
if strings.TrimSpace(line) == "" {
140+
continue
141+
}
142+
p.Outputln(line)
143+
}
144+
145+
return nil
140146
}

internal/cmd/server/log/log_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ func TestBuildRequest(t *testing.T) {
184184

185185
func TestOutputResult(t *testing.T) {
186186
type args struct {
187-
outputFormat string
188-
serverLabel string
189-
log string
187+
serverLabel string
188+
logLines []string
190189
}
191190
tests := []struct {
192191
name string
@@ -203,7 +202,7 @@ func TestOutputResult(t *testing.T) {
203202
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
204203
for _, tt := range tests {
205204
t.Run(tt.name, func(t *testing.T) {
206-
if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.log); (err != nil) != tt.wantErr {
205+
if err := outputResult(p, tt.args.serverLabel, tt.args.logLines); (err != nil) != tt.wantErr {
207206
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
208207
}
209208
})

0 commit comments

Comments
 (0)