github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/service_logs.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/url"
     7  	"time"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	timetypes "github.com/docker/docker/api/types/time"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  // ServiceLogs returns the logs generated by a service in an io.ReadCloser.
    15  // It's up to the caller to close the stream.
    16  func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
    17  	query := url.Values{}
    18  	if options.ShowStdout {
    19  		query.Set("stdout", "1")
    20  	}
    21  
    22  	if options.ShowStderr {
    23  		query.Set("stderr", "1")
    24  	}
    25  
    26  	if options.Since != "" {
    27  		ts, err := timetypes.GetTimestamp(options.Since, time.Now())
    28  		if err != nil {
    29  			return nil, errors.Wrap(err, `invalid value for "since"`)
    30  		}
    31  		query.Set("since", ts)
    32  	}
    33  
    34  	if options.Timestamps {
    35  		query.Set("timestamps", "1")
    36  	}
    37  
    38  	if options.Details {
    39  		query.Set("details", "1")
    40  	}
    41  
    42  	if options.Follow {
    43  		query.Set("follow", "1")
    44  	}
    45  	query.Set("tail", options.Tail)
    46  
    47  	resp, err := cli.get(ctx, "/services/"+serviceID+"/logs", query, nil)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return resp.body, nil
    52  }