<commit_before>// Copyright 2022 The Authors (see AUTHORS file)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package logging sets up and configures standard logging.
package logging

import (
	"context"
	"fmt"
	"os"
	"strings"
	"sync"
	"time"

	"cloud.google.com/go/compute/metadata"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"go.uber.org/zap/zaptest"
	"google.golang.org/grpc"
	grpcmetadata "google.golang.org/grpc/metadata"
)

// contextKey is a private string type to prevent collisions in the context map.
type contextKey string

const (
	// loggerKey points to the value in the context where the logger is stored.
	loggerKey = contextKey("logger")

	// googleCloudTraceHeader is the header with trace data.
	googleCloudTraceHeader = "X-Cloud-Trace-Context"

	// googleCloudTraceKey is the key in the structured log where trace information
	// is expected to be present.
	googleCloudTraceKey = "logging.googleapis.com/trace"
)

var (
	// defaultLogger is the default logger. It is initialized once per package
	// include upon calling DefaultLogger.
	defaultLogger     *zap.SugaredLogger
	defaultLoggerOnce sync.Once
)

// NewFromEnv creates a new logger from env vars.
// Set envPrefix+"LOG_LEVEL" to overwrite log level. Default log level is warning.
// Set envPrefix+"LOG_MODE" to overwrite log mode. Default log mode is production.
func NewFromEnv(envPrefix string) *zap.SugaredLogger {
	level := os.Getenv(envPrefix + "LOG_LEVEL")
	devMode := strings.ToLower(strings.TrimSpace(os.Getenv(envPrefix+"LOG_MODE"))) == "development"
	var cfg zap.Config
	if devMode {
		cfg = zap.NewDevelopmentConfig()
		cfg.EncoderConfig = developmentEncoderConfig
	} else {
		cfg = zap.NewProductionConfig()
		cfg.EncoderConfig = productionEncoderConfig
	}

	var l zapcore.Level
	if err := l.Set(level); err != nil {
		// Invalid level? Default to warn.
		l = zapcore.WarnLevel
	}
	cfg.Level = zap.NewAtomicLevelAt(l)

	logger, err := cfg.Build()
	if err != nil {
		logger = zap.NewNop()
	}

	return logger.Sugar()
}

// Default creates a default logger.
// To overwrite log level and mode, set AUDIT_CLIENT_LOG_LEVEL and AUDIT_CLIENT_LOG_MODE.
// This is mostly used for audit client. The caller doesn't have to explicitly set up a logger.
func Default() *zap.SugaredLogger {
	defaultLoggerOnce.Do(func() {
		defaultLogger = NewFromEnv("AUDIT_CLIENT_")
	})
	return defaultLogger
}

// WithLogger creates a new context with the provided logger attached.
func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context {
	return context.WithValue(ctx, loggerKey, logger)
}

// FromContext returns the logger stored in the context. If no such logger
// exists, a default logger is returned.
func FromContext(ctx context.Context) *zap.SugaredLogger {
	if logger, ok := ctx.Value(loggerKey).(*zap.SugaredLogger); ok {
		return logger
	}
	return Default()
}

// TestLogger returns a logger configured for tests. It will only output log
// information if specific test fails or is run in verbose mode. See [zaptest]
// for more information.
//
//	func TestMyThing(t *testing.T) {
//		logger := logging.TestLogger(t)
//		thing := &MyThing{logger: logger}
//	}
//
// [zaptest]: https://pkg.go.dev/go.uber.org/zap/zaptest
func TestLogger(tb zaptest.TestingT) *zap.SugaredLogger {
	return zaptest.NewLogger(tb, zaptest.Level(zap.WarnLevel)).Sugar()
}

// GRPCInterceptor returns a gRPC interceptor that populates a logger in the context.
func GRPCInterceptor(logger *zap.SugaredLogger) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
		var th string
		md, ok := grpcmetadata.FromIncomingContext(ctx)
		if ok {
			if len(md.Get(googleCloudTraceHeader)) > 0 {
				th = md.Get(googleCloudTraceHeader)[0]
			}
		}

		// Best effort to get the current project ID.
		projectID, _ := metadata.ProjectID()

		// This allows us to associate logs to traces.
		// See "logging.googleapis.com/trace" at:
		// https://cloud.google.com/logging/docs/agent/logging/configuration#special-fields<commit_msg>Accept optional options in TestLogger (#36)<commit_after>// Copyright 2022 The Authors (see AUTHORS file)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package logging sets up and configures standard logging.
package logging

import (
	"context"
	"fmt"
	"os"
	"strings"
	"sync"
	"time"

	"cloud.google.com/go/compute/metadata"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"go.uber.org/zap/zaptest"
	"google.golang.org/grpc"
	grpcmetadata "google.golang.org/grpc/metadata"
)

// contextKey is a private string type to prevent collisions in the context map.
type contextKey string

const (
	// loggerKey points to the value in the context where the logger is stored.
	loggerKey = contextKey("logger")

	// googleCloudTraceHeader is the header with trace data.
	googleCloudTraceHeader = "X-Cloud-Trace-Context"

	// googleCloudTraceKey is the key in the structured log where trace information
	// is expected to be present.
	googleCloudTraceKey = "logging.googleapis.com/trace"
)

var (
	// defaultLogger is the default logger. It is initialized once per package
	// include upon calling DefaultLogger.
	defaultLogger     *zap.SugaredLogger
	defaultLoggerOnce sync.Once
)

// NewFromEnv creates a new logger from env vars.
// Set envPrefix+"LOG_LEVEL" to overwrite log level. Default log level is warning.
// Set envPrefix+"LOG_MODE" to overwrite log mode. Default log mode is production.
func NewFromEnv(envPrefix string) *zap.SugaredLogger {
	level := os.Getenv(envPrefix + "LOG_LEVEL")
	devMode := strings.ToLower(strings.TrimSpace(os.Getenv(envPrefix+"LOG_MODE"))) == "development"
	var cfg zap.Config
	if devMode {
		cfg = zap.NewDevelopmentConfig()
		cfg.EncoderConfig = developmentEncoderConfig
	} else {
		cfg = zap.NewProductionConfig()
		cfg.EncoderConfig = productionEncoderConfig
	}

	var l zapcore.Level
	if err := l.Set(level); err != nil {
		// Invalid level? Default to warn.
		l = zapcore.WarnLevel
	}
	cfg.Level = zap.NewAtomicLevelAt(l)

	logger, err := cfg.Build()
	if err != nil {
		logger = zap.NewNop()
	}

	return logger.Sugar()
}

// Default creates a default logger.
// To overwrite log level and mode, set AUDIT_CLIENT_LOG_LEVEL and AUDIT_CLIENT_LOG_MODE.
// This is mostly used for audit client. The caller doesn't have to explicitly set up a logger.
func Default() *zap.SugaredLogger {
	defaultLoggerOnce.Do(func() {
		defaultLogger = NewFromEnv("AUDIT_CLIENT_")
	})
	return defaultLogger
}

// WithLogger creates a new context with the provided logger attached.
func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context {
	return context.WithValue(ctx, loggerKey, logger)
}

// FromContext returns the logger stored in the context. If no such logger
// exists, a default logger is returned.
func FromContext(ctx context.Context) *zap.SugaredLogger {
	if logger, ok := ctx.Value(loggerKey).(*zap.SugaredLogger); ok {
		return logger
	}
	return Default()
}

// TestLogger returns a logger configured for tests. It will only output log
// information if specific test fails or is run in verbose mode. See [zaptest]
// for more information.
//
//	func TestMyThing(t *testing.T) {
//		logger := logging.TestLogger(t)
//		thing := &MyThing{logger: logger}
//	}
//
// [zaptest]: https://pkg.go.dev/go.uber.org/zap/zaptest
func TestLogger(tb zaptest.TestingT, opts ...zaptest.LoggerOption) *zap.SugaredLogger {
	warnLevelOpt := zaptest.Level(zap.WarnLevel)
	opts = append([]zaptest.LoggerOption{warnLevelOpt}, opts...)
	return zaptest.NewLogger(tb, opts...).Sugar()
}

// GRPCInterceptor returns a gRPC interceptor that populates a logger in the context.
func GRPCInterceptor(logger *zap.SugaredLogger) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
		var th string
		md, ok := grpcmetadata.FromIncomingContext(ctx)
		if ok {
			if len(md.Get(googleCloudTraceHeader)) > 0 {
				th = md.Get(googleCloudTraceHeader)[0]
			}
		}

		// Best effort to get the current project ID.
		projectID, _ := metadata.ProjectID()

		// This allows us to associate logs to traces.
		// See "logging.googleapis.com/trace" at:
		// https://cloud.google.com/logging/docs/agent/logging/configuration#special-fields
{'fraction_non_alphanumeric': 0.10668229777256741, 'fraction_numerical': 0.0023446658851113715, 'mean_word_length': 3.991489361702128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3780034', 'n_tokens_mistral': 2968, 'n_tokens_neox': 2886, 'n_words': 947}