Go API Documentation

github.com/TykTechnologies/tyk/log

No package summary is available.

Package

Files: 3. Third party imports: 2. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Vars

var (
	log		= logrus.New()
	rawLog		= logrus.New()
	translations	= make(map[string]string)
)

formatterIndex serves as a list of formatters supported. it can be extended from test scope for benchmark purposes.

var (
	formatterIndex = map[string]func() logrus.Formatter{
		"default": func() logrus.Formatter {
			return &logrus.TextFormatter{
				TimestampFormat:	"Jan 02 15:04:05",
				FullTimestamp:		true,
				DisableColors:		true,
			}
		},
		"json": func() logrus.Formatter {
			return &JSONFormatter{
				TimestampFormat: time.RFC3339,
			}
		},
	}
)
var logLevels = map[string]logrus.Level{
	"error":	logrus.ErrorLevel,
	"warn":		logrus.WarnLevel,
	"debug":	logrus.DebugLevel,
	"info":		logrus.InfoLevel,
}

Types

JSONFormatter

JSONFormatter formats logs into parsable json.

Field name Field type Comment
TimestampFormat

string

TimestampFormat sets the format used for marshaling timestamps. The format to use is the same than for time.Format or time.Parse from the standard library. The standard Library already provides a set of predefined format.

DisableTimestamp

bool

DisableTimestamp allows disabling automatic timestamps in output.

DataKey

string

DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.

type JSONFormatter struct {
	// TimestampFormat sets the format used for marshaling timestamps.
	// The format to use is the same than for time.Format or time.Parse from the standard
	// library.
	// The standard Library already provides a set of predefined format.
	TimestampFormat	string

	// DisableTimestamp allows disabling automatic timestamps in output.
	DisableTimestamp	bool

	// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
	DataKey	string
}

RawFormatter

RawFormatter returns the logrus entry message as bytes.

type RawFormatter struct{}

TranslationFormatter

TranslationFormatter handles message reformatting with translations.

Field name Field type Comment

logrus.Formatter

No comment on field.
type TranslationFormatter struct {
	logrus.Formatter
}

Functions

func Get

Get returns the default configured logger.

func Get() *logrus.Logger {
	return log
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func GetRaw

GetRaw is used internally. Should likely be removed first, do not rely on it.

func GetRaw() *logrus.Logger {
	return rawLog
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func LoadTranslations

LoadTranslations takes a map[string]interface and flattens it to map[string]string. Because translations have been loaded - we internally override log the formatter. Nested entries are accessible using dot notation.

Example: {"foo": {"bar": "baz"}} Flattened: foo.bar: baz

func LoadTranslations(thing map[string]interface{}) {
	// This wraps the existing formatter if translations are loaded.
	log.Formatter = &TranslationFormatter{log.Formatter}
	translations, _ = maps.Flatten(thing)
}

Cognitive complexity: 2, Cyclomatic complexity: 1

Uses: maps.Flatten.

func NewFormatter

func NewFormatter(format string) logrus.Formatter {
	ctor, ok := formatterIndex[format]
	if !ok {
		ctor, _ = formatterIndex["default"]
	}
	return ctor()
}

Cognitive complexity: 2, Cyclomatic complexity: 2

func (*JSONFormatter) Format

Format renders a single log entry

func (f *JSONFormatter) Format(entry *logrus.Entry) ([]byte, error) {
	data := make(logrus.Fields, len(entry.Data)+4)
	for k, v := range entry.Data {
		switch v := v.(type) {
		case error:
			// Otherwise errors are ignored by `encoding/json`
			// https://github.com/sirupsen/logrus/issues/137
			data[k] = v.Error()
		default:
			data[k] = v
		}
	}

	if f.DataKey != "" {
		newData := make(logrus.Fields, 4)
		newData[f.DataKey] = data
		data = newData
	}

	if len(entry.Data) > 0 {
		if err, ok := entry.Data[logrus.ErrorKey]; ok {
			data[logrus.FieldKeyLogrusError] = err
		}
	}
	if !f.DisableTimestamp {
		data[logrus.FieldKeyTime] = entry.Time.Format(f.TimestampFormat)
	}
	data[logrus.FieldKeyMsg] = entry.Message
	data[logrus.FieldKeyLevel] = entry.Level.String()

	var w bytes.Buffer
	enc := json.NewEncoder(&w)
	err := enc.Encode(data)
	return w.Bytes(), err
}

Cognitive complexity: 14, Cyclomatic complexity: 8

Uses: bytes.Buffer, json.NewEncoder, logrus.ErrorKey, logrus.FieldKeyLevel, logrus.FieldKeyLogrusError, logrus.FieldKeyMsg, logrus.FieldKeyTime, logrus.Fields.

func (*RawFormatter) Format

Format returns the entry.Message as a []byte.

func (f *RawFormatter) Format(entry *logrus.Entry) ([]byte, error) {
	return []byte(entry.Message), nil
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*TranslationFormatter) Format

Format will translate the log message based on the message code. This is a HTTP response code if provided. The message is usually just "Finished" for those cases, this would likely produce a better log message.

func (t *TranslationFormatter) Format(entry *logrus.Entry) ([]byte, error) {
	if code, ok := entry.Data["code"]; ok {
		if translation, ok := translations[code.(string)]; ok {
			entry.Message = translation
		}
	}
	return t.Formatter.Format(entry)
}

Cognitive complexity: 4, Cyclomatic complexity: 3

Private functions

func getenv

getenv (names ...string) string
References: os.Getenv, strings.ToLower.

func init

init ()

func setupGlobals

setupGlobals ()


Tests

Files: 1. Third party imports: 2. Imports from organisation: 0. Tests: 1. Benchmarks: 1.

Types

testFormatter

This type doesn't have documentation.

type testFormatter struct{}

Test functions

TestNewFormatter

References: assert.NotNil, assert.True, logrus.JSONFormatter, logrus.TextFormatter.

Benchmark functions

BenchmarkFormatter

References: testing.B.