<commit_before>package log

//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg log -path App,Log

import (
	"context"
	"sync"

	"v2ray.com/core/app"
	"v2ray.com/core/app/log/internal"
	"v2ray.com/core/common"
	"v2ray.com/core/common/log"
)

type Instance struct {
	sync.RWMutex
	config       *Config
	accessLogger internal.LogWriter
	errorLogger  internal.LogWriter
}

func New(ctx context.Context, config *Config) (*Instance, error) {
	return &Instance{
		config: config,
	}, nil
}

func (*Instance) Interface() interface{} {
	return (*Instance)(nil)
}

func (g *Instance) initAccessLogger() error {
	switch g.config.AccessLogType {
	case LogType_File:
		logger, err := internal.NewFileLogWriter(g.config.AccessLogPath)
		if err != nil {
			return err
		}
		g.accessLogger = logger
	case LogType_Console:
		g.accessLogger = internal.NewStdOutLogWriter()
	default:
	}
	return nil
}

func (g *Instance) initErrorLogger() error {
	switch g.config.ErrorLogType {
	case LogType_File:
		logger, err := internal.NewFileLogWriter(g.config.ErrorLogPath)
		if err != nil {
			return err
		}
		g.errorLogger = logger
	case LogType_Console:
		g.errorLogger = internal.NewStdOutLogWriter()
	default:
	}
	return nil
}

func (g *Instance) Start() error {
	if err := g.initAccessLogger(); err != nil {
		return newError("failed to initialize access logger").Base(err).AtWarning()
	}
	if err := g.initErrorLogger(); err != nil {
		return newError("failed to initialize error logger").Base(err).AtWarning()
	}
	return nil
}

func (g *Instance) Handle(msg log.Message) {
	switch msg := msg.(type) {
	case *log.AccessMessage:
		g.RLock()
		defer g.RUnlock()
		if g.accessLogger != nil {
			g.accessLogger.Log(msg)
		}
	case *log.GeneralMessage:
		if msg.Severity.SevererThan(g.config.ErrorLogLevel) {
			g.RLock()
			defer g.RUnlock()
			if g.errorLogger != nil {
				g.errorLogger.Log(msg)
			}
		}
	default:
		// Swallow<commit_msg>register logger on start<commit_after>package log

//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg log -path App,Log

import (
	"context"
	"sync"

	"v2ray.com/core/app"
	"v2ray.com/core/app/log/internal"
	"v2ray.com/core/common"
	"v2ray.com/core/common/log"
)

type Instance struct {
	sync.RWMutex
	config       *Config
	accessLogger internal.LogWriter
	errorLogger  internal.LogWriter
}

func New(ctx context.Context, config *Config) (*Instance, error) {
	return &Instance{
		config: config,
	}, nil
}

func (*Instance) Interface() interface{} {
	return (*Instance)(nil)
}

func (g *Instance) initAccessLogger() error {
	switch g.config.AccessLogType {
	case LogType_File:
		logger, err := internal.NewFileLogWriter(g.config.AccessLogPath)
		if err != nil {
			return err
		}
		g.accessLogger = logger
	case LogType_Console:
		g.accessLogger = internal.NewStdOutLogWriter()
	default:
	}
	return nil
}

func (g *Instance) initErrorLogger() error {
	switch g.config.ErrorLogType {
	case LogType_File:
		logger, err := internal.NewFileLogWriter(g.config.ErrorLogPath)
		if err != nil {
			return err
		}
		g.errorLogger = logger
	case LogType_Console:
		g.errorLogger = internal.NewStdOutLogWriter()
	default:
	}
	return nil
}

func (g *Instance) Start() error {
	if err := g.initAccessLogger(); err != nil {
		return newError("failed to initialize access logger").Base(err).AtWarning()
	}
	if err := g.initErrorLogger(); err != nil {
		return newError("failed to initialize error logger").Base(err).AtWarning()
	}
	log.RegisterHandler(g)
	return nil
}

func (g *Instance) Handle(msg log.Message) {
	switch msg := msg.(type) {
	case *log.AccessMessage:
		g.RLock()
		defer g.RUnlock()
		if g.accessLogger != nil {
			g.accessLogger.Log(msg)
		}
	case *log.GeneralMessage:
		if msg.Severity.SevererThan(g.config.ErrorLogLevel) {
			g.RLock()
			defer g.RUnlock()
			if g.errorLogger != nil {
				g.errorLogger.Log(msg)
			}
		}
	default:
		// Swallow
{'fraction_non_alphanumeric': 0.13202747392520986, 'fraction_numerical': 0.0025438819638768763, 'mean_word_length': 3.921151439299124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3252483', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1406, 'n_words': 315}