refactor(logs): update log formatting for systemd services and handle merging in the client

This commit is contained in:
Pasha Sviderski committed 2026-04-21 22:00:38 +10:00
1 parent 7e1b91c372
commit b852d34068
5 files changed
+140 -88

No files matched your search

+35 -30
View File
@@ -16,35 +16,39 @@ func NewLogsCommand() *cobra.Command {
var options logs.Options
cmd := &cobra.Command{
Use: "logs [UNIT...]",
Use: "logs [SERVICE...]",
Aliases: []string{"log"},
Short: "View systemd service logs.",
Long: `View logs from all replicas of the specified units(s) (uncloud, docker or uncloud-corrosion) across all machines in the cluster.
Long: `View logs from the specified systemd service(s) across all machines in the cluster.
Use -m to restrict to specific machines.
If no units are specified, streams logs from the uncloud unit.`,
Example: ` # View recent logs for a system service.
uc logs uncloud
Supported services:
uncloud the Uncloud daemon
docker the Docker daemon
uncloud-corrosion the Corrosion distributed state store
If no services are specified, streams logs from the uncloud service.`,
Example: ` # View recent logs for the uncloud service.
uc machine logs
uc machine logs uncloud
# Stream logs in real-time (follow mode).
uc logs -f uncloud
uc machine logs -f uncloud
# View logs from multiple services.
uc logs web uncloud docker
uc machine logs uncloud docker uncloud-corrosion
# View logs from uncloud
uc logs
# Show last 20 lines per replica (default is 100).
uc logs -n 20 docker
# Show last 20 lines per machine (default is 100).
uc machine logs -n 20 docker
# Show all logs without line limit.
uc logs -n all docker
uc machine logs -n all docker
# View logs from a specific time range.
uc logs --since 3h --until 1h30m docker
uc machine logs --since 3h --until 1h30m docker
# View logs only from replicas running on specific machines.
uc logs -m machine1,machine2 docker corrosion`,
# View logs only from specific machines.
uc machine logs -m machine1,machine2 uncloud uncloud-corrosion`,
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
return runLogs(cmd.Context(), uncli, args, options)
@@ -62,7 +66,7 @@ func runLogs(ctx context.Context, uncli *cli.CLI, units []string, opts logs.Opti
for _, unit := range units {
if !journal.ValidUnit(unit) {
return fmt.Errorf("invalid unit '%s'", unit)
return fmt.Errorf("invalid systemd service '%s'", unit)
}
}
@@ -85,8 +89,10 @@ func runLogs(ctx context.Context, uncli *cli.CLI, units []string, opts logs.Opti
Machines: cli.ExpandCommaSeparatedValues(opts.Machines),
}
// Fetch machine names for all machines we want the unit logs from.
machines, err := c.ListMachines(ctx, &api.MachineFilter{NamesOrIDs: opts.Machines})
// Resolve machine records for the formatter's column width computation.
machines, err := c.ListMachines(ctx, &api.MachineFilter{
NamesOrIDs: logsOpts.Machines,
})
if err != nil {
return fmt.Errorf("list machines: %w", err)
}
@@ -95,23 +101,22 @@ func runLogs(ctx context.Context, uncli *cli.CLI, units []string, opts logs.Opti
machineNames = append(machineNames, m.Machine.Name)
}
// Collect log streams from the units on the machine machines.
unitStreams := make([]<-chan api.ServiceLogEntry, 0, len(units)+len(machines))
for _, machine := range machineNames {
for _, unit := range units {
ch, err := c.MachineLogs(ctx, machine, unit, logsOpts)
if err != nil {
return fmt.Errorf("stream logs for systemd service '%s': %w", unit, err)
}
unitStreams = append(unitStreams, ch)
// Collect one log stream per unit. MachineLogs merges across machines internally.
unitStreams := make([]<-chan api.ServiceLogEntry, 0, len(units))
for _, unit := range units {
ch, err := c.MachineLogs(ctx, unit, logsOpts)
if err != nil {
return fmt.Errorf("stream logs for systemd service '%s': %w", unit, err)
}
unitStreams = append(unitStreams, ch)
}
var stream <-chan api.ServiceLogEntry
if len(units) == 1 {
if len(unitStreams) == 1 {
stream = unitStreams[0]
} else {
// Each MachineLogs stream already runs its own inner merger with stall detection,
// so the outer merger across units skips it to avoid duplicate warnings.
merger := client.NewLogMerger(unitStreams, client.LogMergerOptions{})
stream = merger.Stream()
}