func Init()

in logger/logger.go [99:158]


func Init(ctx context.Context, opts LogOpts) error {
	if opts.LoggerName == "" {
		return fmt.Errorf("logger name must be set")
	}

	loggerName = opts.LoggerName
	debugEnabled = opts.Debug
	formatFunction = opts.FormatFunction
	writers = opts.Writers

	if !opts.DisableLocalLogging {
		if err := localSetup(loggerName); err != nil {
			return fmt.Errorf("logger Init localSetup error: %v", err)
		}
	}

	if !opts.DisableCloudLogging && opts.ProjectName != "" {
		var err error
		cOpts := []option.ClientOption{}
		if opts.UserAgent != "" {
			cOpts = append(cOpts, option.WithUserAgent(opts.UserAgent))
		}
		cloudLoggingClient, err = logging.NewClient(ctx, opts.ProjectName, cOpts...)
		if err != nil {
			Errorf("Continuing without cloud logging due to error in initialization: %v", err.Error())
			// Log but don't return this error, as it doesn't prevent continuing.
			return nil
		}

		// Override default error handler. Must be a func and not nil.
		cloudLoggingClient.OnError = func(e error) { return }

		// The logger automatically detects and associates with a GCE
		// resource. However instance_name is not included in this
		// resource, so add an instance_name label to all log Entries.
		name, err := metadata.InstanceName()
		labels := make(map[string]string)
		if err == nil {
			labels["instance_name"] = name
		}

		// Add MIG labels if provided.
		migLabels := parseMIGLabels(opts.MIG)
		for k, v := range migLabels {
			labels[k] = v
		}

		// Initialize the logger.
		cloudLogger = cloudLoggingClient.Logger(loggerName, logging.CommonLabels(labels))

		go func() {
			for {
				time.Sleep(5 * time.Second)
				cloudLogger.Flush()
			}
		}()
	}

	return nil
}