CaptureLogs returns logs for a site's given container. See docker.LogsOptions for more information about valid tailLines values.
(service string, timestamps bool, tailLines string)
| 2721 | // CaptureLogs returns logs for a site's given container. |
| 2722 | // See docker.LogsOptions for more information about valid tailLines values. |
| 2723 | func (app *DdevApp) CaptureLogs(service string, timestamps bool, tailLines string) (string, error) { |
| 2724 | ctx, apiClient, err := dockerutil.GetDockerClient() |
| 2725 | if err != nil { |
| 2726 | return "", err |
| 2727 | } |
| 2728 | |
| 2729 | var c *container.Summary |
| 2730 | // Let people access ddev-router and ddev-ssh-agent logs as well. |
| 2731 | if service == "ddev-router" || service == "ddev-ssh-agent" { |
| 2732 | c, err = dockerutil.FindContainerByLabels(map[string]string{ |
| 2733 | "com.docker.compose.service": service, |
| 2734 | "com.docker.compose.oneoff": "False", |
| 2735 | }) |
| 2736 | } else { |
| 2737 | c, err = app.FindContainerByType(service) |
| 2738 | } |
| 2739 | if err != nil { |
| 2740 | return "", err |
| 2741 | } |
| 2742 | if c == nil { |
| 2743 | util.Warning("No running service container %s was found", service) |
| 2744 | return "", nil |
| 2745 | } |
| 2746 | |
| 2747 | var stdout bytes.Buffer |
| 2748 | logOpts := client.ContainerLogsOptions{ |
| 2749 | ShowStdout: true, |
| 2750 | ShowStderr: true, |
| 2751 | Follow: false, |
| 2752 | Timestamps: timestamps, |
| 2753 | } |
| 2754 | |
| 2755 | if tailLines != "" { |
| 2756 | logOpts.Tail = tailLines |
| 2757 | } |
| 2758 | |
| 2759 | rc, err := apiClient.ContainerLogs(ctx, c.ID, logOpts) |
| 2760 | if err != nil { |
| 2761 | return "", err |
| 2762 | } |
| 2763 | defer rc.Close() |
| 2764 | |
| 2765 | _, err = stdcopy.StdCopy(&stdout, &stdout, rc) |
| 2766 | if err != nil { |
| 2767 | return "", fmt.Errorf("failed to copy container logs: %v", err) |
| 2768 | } |
| 2769 | |
| 2770 | return stdout.String(), nil |
| 2771 | } |
| 2772 | |
| 2773 | // DockerEnv sets environment variables for a docker-compose run. |
| 2774 | func (app *DdevApp) DockerEnv() map[string]string { |