示例#1
0
// Use adds a default go-logging logger to the context.
func (lc *LoggerConfig) Use(c context.Context) context.Context {
	return logging.SetFactory(c, func(c context.Context) logging.Logger {
		l := lc.getImpl()
		l.c = c
		return l
	})
}
示例#2
0
// Use adds a memory backed Logger to Context, with concrete type
// *MemLogger. Casting to the concrete type can be used to inspect the
// log output after running a test case, for example.
func Use(c context.Context) context.Context {
	lock := sync.Mutex{}
	data := []LogEntry{}
	return logging.SetFactory(c, func(ic context.Context) logging.Logger {
		return &MemLogger{&lock, &data, logging.GetFields(ic)}
	})
}
示例#3
0
// Use installs a cloud logging Logger into the supplied Context.
func Use(ctx context.Context, config Config, client cloudlogging.Client) context.Context {
	counter := &atomicCounter{}

	return logging.SetFactory(ctx, func(factoryCtx context.Context) logging.Logger {
		return &boundCloudLogger{
			Config: &config,
			ctx:    factoryCtx,
			client: client,
			index:  counter.next(),
		}
	})
}
示例#4
0
// getLogger chooses a base logger instance to use.
//
// If we're running on GCE, we will use a "cloud logging" logger; otherwise,
// we will use a console logger.
//
// The returned function should be called on application termination to flush
// the logger.
func (c *loggerConfig) use(ctx context.Context, client *http.Client) (context.Context, func(), error) {
	if c.projectName == "" {
		return ctx, nil, errors.New("logging: You must supply a project name")
	}
	if c.resourceType == "" {
		return ctx, nil, errors.New("logging: You must supply a resource type")
	}
	if c.logsID == "" {
		return ctx, nil, errors.New("logging: You must supply a logs ID")
	}

	configCopy := *c
	configCopy.labels = make(map[string]string)
	for k, v := range c.labels {
		configCopy.labels[k] = v
	}
	if c.resourceType != "" {
		configCopy.labels["compute.googleapis.com/resource_type"] = c.resourceType
	}
	if c.resourceID != "" {
		configCopy.labels["compute.googleapis.com/resource_id"] = c.resourceID
	}

	if configCopy.sessionID == "" {
		sessionID := make([]byte, loggingSessionIDSize)
		if _, err := rand.Read(sessionID); err != nil {
			return ctx, nil, err
		}

		configCopy.sessionID = base64.URLEncoding.EncodeToString(sessionID)
	}

	// Load GCE credentials for cloud logger.
	service, err := cloudlog.New(client)
	if err != nil {
		return ctx, nil, err
	}

	clog := newCloudLogging(ctx, &configCopy, service)

	ctx = log.SetFactory(ctx, func(c context.Context) log.Logger {
		return clog.bind(c)
	})
	return ctx, clog.finish, nil
}
示例#5
0
文件: logger.go 项目: shkw/gae
// useLogging adds a logging.Logger implementation to the context which logs to
// appengine's log handler.
func useLogging(c context.Context) context.Context {
	return logging.SetFactory(c, func(ic context.Context) logging.Logger {
		return &loggerImpl{AEContext(ic), ic}
	})
}
示例#6
0
// Use adds a logging.Logger implementation to the context which logs to
// appengine's log handler.
func Use(c context.Context, gaeCtx appengine.Context) context.Context {
	return logging.SetFactory(c, func(ic context.Context) logging.Logger {
		return &gaeLoggerImpl{gaeCtx, ic}
	})
}