<commit_before>package log

import (
	"reflect"
)

const LOG_TIME_FORMAT = "2006-01-02T15:04:05.000000"
const LOG_LEVEL_DEBUG = "DEBUG"
const LOG_LEVEL_INFO  = "INFO"

type Config struct {
	Level string
	Formatter func(level string, message string, tags map[string]string) string
	Output func(formattedMessage string)
	Program string
	DateFormat string
	Tags map[string]string
}

type Log struct {
	config *Config
}
type LogFormattingFailed string
func (err LogFormattingFailed) String() string {
	return "LogFormattingFailed"
}

func (log Log) Info(message string, tags interface{}) {
	log.config.Output(log.config.Formatter(LOG_LEVEL_INFO, message, mergeTags(log.config.Tags, tags)))
}

func (log Log) Debug(message string, tags interface{}) {
	if log.config.Level == LOG_LEVEL_DEBUG {
		log.config.Output(log.config.Formatter(LOG_LEVEL_DEBUG, message, mergeTags(log.config.Tags, tags)))
	}
}

func (log Log) ChildLogger(function string, additionalTags map[string]string) *Log {
	childConfig := new(Config)
	childConfig.Level = log.config.Level
	childConfig.Formatter = log.config.Formatter
	childConfig.Output = log.config.Output
	childConfig.DateFormat = log.config.DateFormat
	childConfig.Program = log.config.Program
	childConfig.Tags = log.config.Tags
	childConfig.Tags["function"] = function
	for name, value := range additionalTags {
		childConfig.Tags[name] = value
	}
	return NewLogger(childConfig)

}

func NewLogger(config *Config) *Log {
	config.Tags["program"] = config.Program
	config.Tags["function"] = "main"
	logger := new(Log)
	logger.config = config
	return logger
}

func mergeTags(tags map[string]string, additionalTags interface{}) map[string]string {
	outputTags := map[string]string{}
	for name, value := range tags {
		outputTags[name] = value
	}

	reflectedContext := reflect.TypeOf(&additionalTags).Elem()
	reflectedValue := reflect.ValueOf(&additionalTags).Elem()
	if reflectedValue.Kind() == reflect.Map {
		for _, name := range reflectedValue.MapKeys() {
			outputTags[name] = reflectedValue.MapIndex(name).String()
		}
	} else if reflectedValue.Kind() == reflect.Struct {
		for i := 0; i < reflectedContext.NumField(); i++ {
			currentField := reflectedContext.Field(i)
			outputTags[currentField.Name] = reflectedValue.FieldByName(currentField.Name).String()
		}
	}

	return outputTags
}
<commit_msg>fix naming of constants<commit_after>package log

import (
	"reflect"
)

const TIME_FORMAT = "2006-01-02T15:04:05.000000"
const LEVEL_DEBUG = "DEBUG"
const LEVEL_INFO  = "INFO"

type Config struct {
	Level string
	Formatter func(level string, message string, tags map[string]string) string
	Output func(formattedMessage string)
	Program string
	DateFormat string
	Tags map[string]string
}

type Log struct {
	config *Config
}
type LogFormattingFailed string
func (err LogFormattingFailed) String() string {
	return "LogFormattingFailed"
}

func (log Log) Info(message string, tags interface{}) {
	log.config.Output(log.config.Formatter(LEVEL_INFO, message, mergeTags(log.config.Tags, tags)))
}

func (log Log) Debug(message string, tags interface{}) {
	if log.config.Level == LEVEL_DEBUG {
		log.config.Output(log.config.Formatter(LEVEL_DEBUG, message, mergeTags(log.config.Tags, tags)))
	}
}

func (log Log) ChildLogger(function string, additionalTags map[string]string) *Log {
	childConfig := new(Config)
	childConfig.Level = log.config.Level
	childConfig.Formatter = log.config.Formatter
	childConfig.Output = log.config.Output
	childConfig.DateFormat = log.config.DateFormat
	childConfig.Program = log.config.Program
	childConfig.Tags = log.config.Tags
	childConfig.Tags["function"] = function
	for name, value := range additionalTags {
		childConfig.Tags[name] = value
	}
	return NewLogger(childConfig)

}

func NewLogger(config *Config) *Log {
	config.Tags["program"] = config.Program
	config.Tags["function"] = "main"
	logger := new(Log)
	logger.config = config
	return logger
}

func mergeTags(tags map[string]string, additionalTags interface{}) map[string]string {
	outputTags := map[string]string{}
	for name, value := range tags {
		outputTags[name] = value
	}

	reflectedContext := reflect.TypeOf(&additionalTags).Elem()
	reflectedValue := reflect.ValueOf(&additionalTags).Elem()
	if reflectedValue.Kind() == reflect.Map {
		for _, name := range reflectedValue.MapKeys() {
			outputTags[name] = reflectedValue.MapIndex(name).String()
		}
	} else if reflectedValue.Kind() == reflect.Struct {
		for i := 0; i < reflectedContext.NumField(); i++ {
			currentField := reflectedContext.Field(i)
			outputTags[currentField.Name] = reflectedValue.FieldByName(currentField.Name).String()
		}
	}

	return outputTags
}

{'fraction_non_alphanumeric': 0.12204555221314999, 'fraction_numerical': 0.009024495058014611, 'mean_word_length': 5.411845730027548, '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': '1898062', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1443, 'n_words': 335}