// NewClient returns a new log client, logging to the named log in the
// provided project.
//
// The exported fields on the returned client may be modified before
// the client is used for logging. Once log entries are in flight,
// the fields must not be modified.
func NewClient(ctx context.Context, projectID, logName string, opts ...cloud.ClientOption) (*Client, error) {
	httpClient, endpoint, err := transport.NewHTTPClient(ctx, append([]cloud.ClientOption{
		cloud.WithEndpoint(prodAddr),
		cloud.WithScopes(Scope),
		cloud.WithUserAgent(userAgent),
	}, opts...)...)
	if err != nil {
		return nil, err
	}
	svc, err := api.New(httpClient)
	if err != nil {
		return nil, err
	}
	svc.BasePath = endpoint
	c := &Client{
		svc:     svc,
		logs:    api.NewProjectsLogsEntriesService(svc),
		logName: logName,
		projID:  projectID,
	}
	for i := range c.writer {
		level := Level(i)
		c.writer[level] = levelWriter{level, c}
		c.logger[level] = log.New(c.writer[level], "", 0)
	}
	return c, nil
}
Beispiel #2
0
// sendLogEntries sends a logEntry bundle to the Cloud Logging system.
func (l *cloudLogging) sendLogEntries(entries []*logEntry, index int64) error {
	if len(entries) == 0 {
		return nil
	}

	logEntries := make([]*cloudlog.LogEntry, len(entries))
	for idx, e := range entries {
		logEntries[idx] = l.buildLogEntry(e, index, int64(idx))
	}

	req := cloudlog.WriteLogEntriesRequest{
		CommonLabels: l.labels,
		Entries:      logEntries,
	}

	svc := cloudlog.NewProjectsLogsEntriesService(l.service)
	call := svc.Write(l.projectName, l.logsID, &req)
	return retry.Retry(l.ctx, func() error {
		_, err := call.Do()
		return err
	}, func(err error, delay time.Duration) {
		// Write error to STDERR.
		fmt.Fprintf(os.Stderr, "WARNING: Failed to send log entries {err=%s, delay=%s, entries=%s}\n",
			err, delay, entries)
	})
}
Beispiel #3
0
// NewClient returns new object that knows how to push log entries to a single
// log in Cloud Logging.
func NewClient(opts ClientOptions, client *http.Client) (Client, error) {
	if err := opts.Validate(); err != nil {
		return nil, err
	}

	if opts.ResourceType == "" {
		opts.ResourceType = DefaultResourceType
	}
	if opts.ResourceID == "" {
		var err error
		hostname, err := os.Hostname()
		if err != nil {
			return nil, err
		}
		opts.ResourceID = hostname
	}
	if opts.LogID == "" {
		return nil, errors.New("cloudlogging: no LogID is provided")
	}

	service, err := cloudlog.New(client)
	if err != nil {
		return nil, err
	}
	if opts.UserAgent != "" {
		service.UserAgent = opts.UserAgent
	}

	c := clientImpl{
		ClientOptions: &opts,
		service:       cloudlog.NewProjectsLogsEntriesService(service),
		commonLabels:  make(map[string]string, len(opts.CommonLabels)),
	}
	for k, v := range opts.CommonLabels {
		c.commonLabels[k] = v
	}
	if c.ResourceType != "" {
		c.commonLabels["compute.googleapis.com/resource_type"] = c.ResourceType
	}
	if c.ResourceID != "" {
		c.commonLabels["compute.googleapis.com/resource_id"] = c.ResourceID
	}
	return &c, nil
}
Beispiel #4
0
// NewClient returns a new log client, logging to the named log.  The
// log must exist in the Google Cloud Platform project ID associated
// with the provided context. Use the google.golang.org/cloud package
// to create a context.
//
// The exported fields on the returned client may be modified before
// the client is used for logging. Once log entries are in flight,
// the fields must not be modified.
func NewClient(ctx context.Context, logName string) (*Client, error) {
	projID := internal.ProjID(ctx)
	httpClient := internal.HTTPClient(ctx)
	if projID == "" || httpClient == nil {
		return nil, errors.New("logging: invalid or non-google.golang.org/cloud Context")
	}
	svc, err := api.New(httpClient)
	if err != nil {
		return nil, err
	}
	c := &Client{
		svc:     svc,
		logs:    api.NewProjectsLogsEntriesService(svc),
		logName: logName,
		projID:  projID,
	}
	for i := range c.writer {
		level := Level(i)
		c.writer[level] = levelWriter{level, c}
		c.logger[level] = log.New(c.writer[level], "", 0)
	}
	return c, nil
}