Skip to content

Add option to disable logs #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ $ builder-playground cook l1 --latest-fork --output ~/my-builder-testnet --genes
- `--dry-run` (bool): Generates the artifacts and manifest but does not deploy anything (also enabled with the `--mise-en-place` flag)
- `--log-level` (string): Log level to use (debug, info, warn, error, fatal). Defaults to `info`.
- `--labels` (key=val): Custom labels to apply to your deployment.
- `--disable-logs` (bool): Disable the logs for the services. Defaults to `false`.

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

Expand Down
43 changes: 26 additions & 17 deletions internal/local_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type LocalRunner struct {

// labels is the list of labels to apply to each resource being created
labels map[string]string

// logInternally outputs the logs of the service to the artifacts folder
logInternally bool
}

type task struct {
Expand Down Expand Up @@ -107,7 +110,7 @@ func newDockerClient() (*client.Client, error) {
}

// TODO: add a runner config struct
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string, labels map[string]string) (*LocalRunner, error) {
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string, labels map[string]string, logInternally bool) (*LocalRunner, error) {
client, err := newDockerClient()
if err != nil {
return nil, fmt.Errorf("failed to create docker client: %w", err)
Expand All @@ -124,23 +127,24 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
// Create the concrete instances to run
instances := []*instance{}
for _, service := range manifest.Services() {
log_output, err := out.LogOutput(service.Name)
if err != nil {
return nil, fmt.Errorf("error getting log output: %w", err)
}
logs := &serviceLogs{
logRef: log_output,
path: log_output.Name(),
}
component := FindComponent(service.ComponentName)
if component == nil {
return nil, fmt.Errorf("component not found '%s'", service.ComponentName)
}
instance := &instance{
service: service,
logs: logs,
component: component,
}
if logInternally {
log_output, err := out.LogOutput(service.Name)
if err != nil {
return nil, fmt.Errorf("error getting log output: %w", err)
}
instance.logs = &serviceLogs{
logRef: log_output,
path: log_output.Name(),
}
}
instances = append(instances, instance)
}

Expand Down Expand Up @@ -213,6 +217,7 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
networkName: networkName,
instances: instances,
labels: labels,
logInternally: logInternally,
}

if interactive {
Expand Down Expand Up @@ -872,12 +877,14 @@ func (d *LocalRunner) trackContainerStatusAndLogs() {
case events.ActionStart:
d.updateTaskStatus(name, taskStatusStarted)

// the container has started, we can track the logs now
go func() {
if err := d.trackLogs(name, event.Actor.ID); err != nil {
log.Warn("error tracking logs", "error", err)
}
}()
if d.logInternally {
// the container has started, we can track the logs now
go func() {
if err := d.trackLogs(name, event.Actor.ID); err != nil {
log.Warn("error tracking logs", "error", err)
}
}()
}
case events.ActionDie:
d.updateTaskStatus(name, taskStatusDie)
log.Info("container died", "name", name)
Expand Down Expand Up @@ -971,7 +978,9 @@ func (d *LocalRunner) Run() error {

// generate the output log file for each service so that it is available after Run is done
for _, instance := range d.instances {
d.tasks[instance.service.Name].logs = instance.logs.logRef
if instance.logs != nil {
d.tasks[instance.service.Name].logs = instance.logs.logRef
}
}

// First start the services that are running in docker-compose
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var bindExternal bool
var withPrometheus bool
var networkName string
var labels internal.MapStringFlag
var disableLogs bool

var rootCmd = &cobra.Command{
Use: "playground",
Expand Down Expand Up @@ -175,6 +176,8 @@ func main() {
recipeCmd.Flags().BoolVar(&withPrometheus, "with-prometheus", false, "whether to gather the Prometheus metrics")
recipeCmd.Flags().StringVar(&networkName, "network", "", "network name")
recipeCmd.Flags().Var(&labels, "labels", "list of labels to apply to the resources")
recipeCmd.Flags().BoolVar(&disableLogs, "disable-logs", false, "disable logs")

cookCmd.AddCommand(recipeCmd)
}

Expand Down Expand Up @@ -252,7 +255,7 @@ func runIt(recipe internal.Recipe) error {
}
}

dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName, labels)
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName, labels, !disableLogs)
if err != nil {
return fmt.Errorf("failed to create docker runner: %w", err)
}
Expand Down