Skip to content

Commit 463e8c8

Browse files
authored
Add option to disable logs (#140)
* Add option to disable logs * Update README
1 parent a718398 commit 463e8c8

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ $ builder-playground cook l1 --latest-fork --output ~/my-builder-testnet --genes
6666
- `--dry-run` (bool): Generates the artifacts and manifest but does not deploy anything (also enabled with the `--mise-en-place` flag)
6767
- `--log-level` (string): Log level to use (debug, info, warn, error, fatal). Defaults to `info`.
6868
- `--labels` (key=val): Custom labels to apply to your deployment.
69+
- `--disable-logs` (bool): Disable the logs for the services. Defaults to `false`.
6970

7071
To stop the playground, press `Ctrl+C`.
7172

internal/local_runner.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ type LocalRunner struct {
7777

7878
// labels is the list of labels to apply to each resource being created
7979
labels map[string]string
80+
81+
// logInternally outputs the logs of the service to the artifacts folder
82+
logInternally bool
8083
}
8184

8285
type task struct {
@@ -107,7 +110,7 @@ func newDockerClient() (*client.Client, error) {
107110
}
108111

109112
// TODO: add a runner config struct
110-
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string, labels map[string]string) (*LocalRunner, error) {
113+
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string, labels map[string]string, logInternally bool) (*LocalRunner, error) {
111114
client, err := newDockerClient()
112115
if err != nil {
113116
return nil, fmt.Errorf("failed to create docker client: %w", err)
@@ -124,23 +127,24 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
124127
// Create the concrete instances to run
125128
instances := []*instance{}
126129
for _, service := range manifest.Services() {
127-
log_output, err := out.LogOutput(service.Name)
128-
if err != nil {
129-
return nil, fmt.Errorf("error getting log output: %w", err)
130-
}
131-
logs := &serviceLogs{
132-
logRef: log_output,
133-
path: log_output.Name(),
134-
}
135130
component := FindComponent(service.ComponentName)
136131
if component == nil {
137132
return nil, fmt.Errorf("component not found '%s'", service.ComponentName)
138133
}
139134
instance := &instance{
140135
service: service,
141-
logs: logs,
142136
component: component,
143137
}
138+
if logInternally {
139+
log_output, err := out.LogOutput(service.Name)
140+
if err != nil {
141+
return nil, fmt.Errorf("error getting log output: %w", err)
142+
}
143+
instance.logs = &serviceLogs{
144+
logRef: log_output,
145+
path: log_output.Name(),
146+
}
147+
}
144148
instances = append(instances, instance)
145149
}
146150

@@ -213,6 +217,7 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
213217
networkName: networkName,
214218
instances: instances,
215219
labels: labels,
220+
logInternally: logInternally,
216221
}
217222

218223
if interactive {
@@ -872,12 +877,14 @@ func (d *LocalRunner) trackContainerStatusAndLogs() {
872877
case events.ActionStart:
873878
d.updateTaskStatus(name, taskStatusStarted)
874879

875-
// the container has started, we can track the logs now
876-
go func() {
877-
if err := d.trackLogs(name, event.Actor.ID); err != nil {
878-
log.Warn("error tracking logs", "error", err)
879-
}
880-
}()
880+
if d.logInternally {
881+
// the container has started, we can track the logs now
882+
go func() {
883+
if err := d.trackLogs(name, event.Actor.ID); err != nil {
884+
log.Warn("error tracking logs", "error", err)
885+
}
886+
}()
887+
}
881888
case events.ActionDie:
882889
d.updateTaskStatus(name, taskStatusDie)
883890
log.Info("container died", "name", name)
@@ -971,7 +978,9 @@ func (d *LocalRunner) Run() error {
971978

972979
// generate the output log file for each service so that it is available after Run is done
973980
for _, instance := range d.instances {
974-
d.tasks[instance.service.Name].logs = instance.logs.logRef
981+
if instance.logs != nil {
982+
d.tasks[instance.service.Name].logs = instance.logs.logRef
983+
}
975984
}
976985

977986
// First start the services that are running in docker-compose

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var bindExternal bool
2828
var withPrometheus bool
2929
var networkName string
3030
var labels internal.MapStringFlag
31+
var disableLogs bool
3132

3233
var rootCmd = &cobra.Command{
3334
Use: "playground",
@@ -175,6 +176,8 @@ func main() {
175176
recipeCmd.Flags().BoolVar(&withPrometheus, "with-prometheus", false, "whether to gather the Prometheus metrics")
176177
recipeCmd.Flags().StringVar(&networkName, "network", "", "network name")
177178
recipeCmd.Flags().Var(&labels, "labels", "list of labels to apply to the resources")
179+
recipeCmd.Flags().BoolVar(&disableLogs, "disable-logs", false, "disable logs")
180+
178181
cookCmd.AddCommand(recipeCmd)
179182
}
180183

@@ -252,7 +255,7 @@ func runIt(recipe internal.Recipe) error {
252255
}
253256
}
254257

255-
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName, labels)
258+
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName, labels, !disableLogs)
256259
if err != nil {
257260
return fmt.Errorf("failed to create docker runner: %w", err)
258261
}

0 commit comments

Comments
 (0)