<commit_before>package log

import (
	"log"
	"os"
)

// AccessStatus is the status of an access request from clients.
type AccessStatus string

const (
	AccessAccepted = AccessStatus("accepted")
	AccessRejected = AccessStatus("rejected")
)

type accessLogger interface {
	Log(from, to string, status AccessStatus, reason string)
}

type noOpAccessLogger struct {
}

func (logger *noOpAccessLogger) Log(from, to string, status AccessStatus, reason string) {
	// Swallow
}

type accessLog struct {
	From   string
	To     string
	Status AccessStatus
	Reason string
}

type fileAccessLogger struct {
	queue  chan *accessLog
	logger *log.Logger
	file   *os.File
}

func (logger *fileAccessLogger) close() {
	logger.file.Close()
}

func (logger *fileAccessLogger) Log(from, to string, status AccessStatus, reason string) {
	logger.queue <- &accessLog{
		From:   from,
		To:     to,
		Status: status,
		Reason: reason,
	}
}

func (logger *fileAccessLogger) Run() {
	for entry := range logger.queue {
		logger.logger.Println(entry.From + " " + string(entry.Status) + " " + entry.To + " " + entry.Reason)
	}
}

func newFileAccessLogger(path string) accessLogger {
	file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
	if err != nil {
		log.Printf("Unable to create or open file (%s): %v\n", path, err)
		return nil
	}<commit_msg>Don't block main thread when writing access log.<commit_after>package log

import (
	"log"
	"os"
)

// AccessStatus is the status of an access request from clients.
type AccessStatus string

const (
	AccessAccepted = AccessStatus("accepted")
	AccessRejected = AccessStatus("rejected")
)

type accessLogger interface {
	Log(from, to string, status AccessStatus, reason string)
}

type noOpAccessLogger struct {
}

func (logger *noOpAccessLogger) Log(from, to string, status AccessStatus, reason string) {
	// Swallow
}

type accessLog struct {
	From   string
	To     string
	Status AccessStatus
	Reason string
}

type fileAccessLogger struct {
	queue  chan *accessLog
	logger *log.Logger
	file   *os.File
}

func (logger *fileAccessLogger) close() {
	logger.file.Close()
}

func (logger *fileAccessLogger) Log(from, to string, status AccessStatus, reason string) {
	select {
	case logger.queue <- &accessLog{
		From:   from,
		To:     to,
		Status: status,
		Reason: reason,
	}:
	default:
		// We don't expect this to happen, but don't want to block main thread as well.
	}
}

func (logger *fileAccessLogger) Run() {
	for entry := range logger.queue {
		logger.logger.Println(entry.From + " " + string(entry.Status) + " " + entry.To + " " + entry.Reason)
	}
}

func newFileAccessLogger(path string) accessLogger {
	file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
	if err != nil {
		log.Printf("Unable to create or open file (%s): %v\n", path, err)
		return nil
	}
{'fraction_non_alphanumeric': 0.11448763250883393, 'fraction_numerical': 0.0028268551236749115, 'mean_word_length': 4.073476702508961, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3057599', 'n_tokens_mistral': 969, 'n_tokens_neox': 939, 'n_words': 283}