11//===----------------------------------------------------------------------===//
2- // Copyright © 2025 Apple Inc. and the container project authors . All rights reserved.
2+ // Copyright © 2025 Mazdak Rezvani and contributors . All rights reserved.
33//
44// Licensed under the Apache License, Version 2.0 (the "License");
55// you may not use this file except in compliance with the License.
@@ -40,6 +40,12 @@ struct ComposeLogs: AsyncParsableCommand {
4040
4141 @Flag ( name: [ . customLong( " timestamps " ) , . customShort( " t " ) ] , help: " Show timestamps " )
4242 var timestamps : Bool = false
43+
44+ @Flag ( name: . long, help: " Disable log prefixes (container-name |) " )
45+ var noLogPrefix : Bool = false
46+
47+ @Flag ( name: . long, help: " Disable colored output " )
48+ var noColor : Bool = false
4349
4450 @Argument ( help: " Services to display logs for " )
4551 var services : [ String ] = [ ]
@@ -64,6 +70,8 @@ struct ComposeLogs: AsyncParsableCommand {
6470
6571 // Create orchestrator
6672 let orchestrator = Orchestrator ( log: log)
73+ // Install Ctrl-C handler to exit gracefully while following logs
74+ installDefaultTerminationHandlers ( )
6775
6876 // Get logs stream
6977 let logStream = try await orchestrator. logs (
@@ -73,19 +81,26 @@ struct ComposeLogs: AsyncParsableCommand {
7381 tail: tail,
7482 timestamps: timestamps
7583 )
76-
77- // Print logs with service names and optional timestamps
84+
85+ // Compute padding width for aligned prefixes
86+ let nameWidth = noLogPrefix ? nil : try await TargetsUtil . computePrefixWidth ( project: project, services: services)
87+
88+ // Print logs with container-name prefixes and optional timestamps
7889 let dateFormatter = DateFormatter ( )
7990 dateFormatter. dateFormat = " yyyy-MM-dd HH:mm:ss.SSS "
8091
8192 for try await entry in logStream {
82- var output = " [ \( entry. serviceName) ] "
83-
93+ var output = " "
94+ if !noLogPrefix {
95+ output += LogPrefixFormatter . coloredPrefix ( for: entry. containerName, width: nameWidth, colorEnabled: !noColor)
96+ }
8497 if timestamps {
85- output += " \( dateFormatter. string ( from: entry. timestamp) ) "
98+ // If no prefix, don't double space
99+ if !output. isEmpty { output += " " }
100+ output += dateFormatter. string ( from: entry. timestamp)
86101 }
87-
88- output += " \( entry. message) "
102+ if !output . isEmpty { output += " " }
103+ output += entry. message
89104
90105 switch entry. stream {
91106 case . stdout:
0 commit comments