trace_logger

package
v0.14.7-rc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

trace_logger is a tool to record trace log.

Index

Constants

View Source
const (
	DefaultDir             string = "./"
	DefaultFilePrefix      string = "trace_logger"
	DefaultLevel           string = "INFO"
	DefaultMaxKeepDays     int    = 7
	DefaultMaxFileNum      int    = 3
	DefaultMaxFileSizeInMB int    = 10
	DefaultTimeout         string = "2h"
	DefaultMaxCacheSize    int    = 10000
	DefaultSyncInterval    string = "10m"
	DefaultDeleteInterval  string = "1m"
	LogrusField                   = "trace_logger"
)

default configs

View Source
const (
	NoKeyError         = "no key has been set"
	NoTraceError       = "no trace has been set"
	LogLevelStringSize = 4
	CacheLoadFactor    = 0.75
	LogStringFormat    = "2006-01-02 15:04:05.06"
)

define errors

Variables

This section is empty.

Functions

func AutoDelete

func AutoDelete(duration time.Duration, method ...DeleteMethod) error

func AutoSync

func AutoSync(duration time.Duration) error

func CancelAutoDelete

func CancelAutoDelete() error

func CancelAutoSync

func CancelAutoSync() error

func ClearAll

func ClearAll() error

func Init

func Init(config TraceLoggerConfig) error

func InitTraceLoggerManager

func InitTraceLoggerManager(config TraceLoggerConfig) error

func LoadAll

func LoadAll(path string, prefix ...string) error

func ParseTimeUnit

func ParseTimeUnit(timeStr string) (time.Duration, error)

func ParseTimeUnitWithDefault

func ParseTimeUnitWithDefault(timeStr string, defaultTime time.Duration) time.Duration

func Start

func Start(config TraceLoggerConfig)

func SyncAll

func SyncAll() error

func UpdateKey

func UpdateKey(oldKey, newKey string) error

UpdateKey this function will update the key of the trace fileLogger.

Types

type DefaultTraceLoggerManager

type DefaultTraceLoggerManager struct {
	// contains filtered or unexported fields
}

func NewDefaultTraceLoggerManager

func NewDefaultTraceLoggerManager(cacheSize int, timeout time.Duration, debug bool) *DefaultTraceLoggerManager

func (*DefaultTraceLoggerManager) AutoDelete

func (d *DefaultTraceLoggerManager) AutoDelete(duration time.Duration, methods ...DeleteMethod) error

func (*DefaultTraceLoggerManager) AutoSync

func (d *DefaultTraceLoggerManager) AutoSync(duration time.Duration) error

func (*DefaultTraceLoggerManager) CancelAutoDelete

func (d *DefaultTraceLoggerManager) CancelAutoDelete() error

func (*DefaultTraceLoggerManager) CancelAutoSync

func (d *DefaultTraceLoggerManager) CancelAutoSync() error

func (*DefaultTraceLoggerManager) ClearAll

func (d *DefaultTraceLoggerManager) ClearAll() error

ClearAll will clear all the trace from the cache please sync the cache before calling this function

func (*DefaultTraceLoggerManager) DeleteUnusedCache

func (d *DefaultTraceLoggerManager) DeleteUnusedCache(timeout time.Duration, methods ...DeleteMethod) error

func (*DefaultTraceLoggerManager) GetAllTraceFromCache

func (d *DefaultTraceLoggerManager) GetAllTraceFromCache() []Trace

func (*DefaultTraceLoggerManager) GetTraceFromCache

func (d *DefaultTraceLoggerManager) GetTraceFromCache(key string) (Trace, bool)

func (*DefaultTraceLoggerManager) Key

func (*DefaultTraceLoggerManager) KeyWithUpdate

func (d *DefaultTraceLoggerManager) KeyWithUpdate(key string) TraceLogger

func (*DefaultTraceLoggerManager) LoadAll

func (d *DefaultTraceLoggerManager) LoadAll(path string, prefixes ...string) (err error)

LoadAll will load all the trace from the file, and replace local cache

func (*DefaultTraceLoggerManager) NewTraceLogger

func (d *DefaultTraceLoggerManager) NewTraceLogger() TraceLogger

func (*DefaultTraceLoggerManager) SetTraceToCache

func (d *DefaultTraceLoggerManager) SetTraceToCache(key string, trace Trace) (err error)

func (*DefaultTraceLoggerManager) StoreTraceToFile

func (d *DefaultTraceLoggerManager) StoreTraceToFile(trace Trace)

func (*DefaultTraceLoggerManager) String

func (d *DefaultTraceLoggerManager) String() string

func (*DefaultTraceLoggerManager) SyncAll

func (d *DefaultTraceLoggerManager) SyncAll() error

func (*DefaultTraceLoggerManager) UpdateKey

func (d *DefaultTraceLoggerManager) UpdateKey(key string, newKey string) error

type DeleteMethod

type DeleteMethod func(key string) bool

DeleteMethod return true to delete

var DefaultDeleteMethod DeleteMethod = func(key string) bool {
	return true
}

TODO: add custom delete method

var DeleteFunc DeleteMethod = func(key string) bool {

	run, err := models.GetRunByID(log.NewEntry(log.StandardLogger()), key)
	if err != nil {

		return true
	}

	if common.IsRunFinalStatus(run.Status) {
		return true
	}
	return false
}

DeleteFunc delete function for trace log

type Trace

type Trace struct {
	Logs       []traceLog
	UpdateTime time.Time
	// contains filtered or unexported fields
}

func GetJobTracesByRunID

func GetJobTracesByRunID(runID string) ([]Trace, bool)

GetJobTracesByRunID for run and job relation mapping

func GetTraceFromCache

func GetTraceFromCache(key string) (Trace, bool)

func GetTracesByRunIDAndJobID

func GetTracesByRunIDAndJobID(runID, jobID string) ([]Trace, bool)

GetTracesByRunIDAndJobID return the traces by run or job id, if not found, return nil, false it will return related run and job log for runID, and related job log for jobID, it will omit empty IDs

func (Trace) String

func (t Trace) String() string

type TraceLogger

type TraceLogger interface {
	// fileLogger interface
	Infof(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	// trace interface
	SetKey(key string)
	GetKey() string
	GetTrace() Trace
	UpdateTraceWithKey(key string) error
	UpdateTrace() error
}

func Key

func Key(key string) TraceLogger

Key this function will create a trace fileLogger for every unique key, the logs will be saved to a same slice when use same trace fileLogger. A new key will be treated as temp key, logs will not be saved until the key is updated by UpdateKey.

func KeyWithUpdate

func KeyWithUpdate(key string) TraceLogger

KeyWithUpdate same behavior as Key method, but the key will be updated to newKey after the fileLogger is created. i.e. KeyWithUpdate behave same as Key(key1) + UpdateKey(key1, key1)

type TraceLoggerConfig

type TraceLoggerConfig struct {
	Dir             string `yaml:"dir"`             // Dir log file dir
	FilePrefix      string `yaml:"filePrefix"`      // FilePrefix log file prefix
	Level           string `yaml:"level"`           // Level log level
	MaxKeepDays     int    `yaml:"maxKeepDays"`     // MaxKeepDays max keep days for log rotation
	MaxFileNum      int    `yaml:"maxFileNum"`      // MaxFileNum max file num for log rotation
	MaxFileSizeInMB int    `yaml:"maxFileSizeInMB"` // MaxFileSizeInMB max file size in MB for log rotation
	IsCompress      bool   `yaml:"isCompress"`      // IsCompress is compress log file
	Timeout         string `yaml:"timeout"`         // Timeout for local cache
	MaxCacheSize    int    `yaml:"maxCacheSize"`    // MaxCacheSize max local cache size, evict when cache size exceed this value
	SyncInterval    string `yaml:"syncInterval"`    // SyncInterval auto syncs interval
	DeleteInterval  string `yaml:"deleteInterval"`  // DeleteInterval auto delete interval
	Debug           bool   `yaml:"debug"`           // Debug is debug mode, print log to stdout if set true
}

type TraceLoggerManager

type TraceLoggerManager interface {
	NewTraceLogger() TraceLogger

	GetTraceFromCache(key string) (Trace, bool)
	GetAllTraceFromCache() []Trace
	SetTraceToCache(key string, trace Trace) error
	UpdateKey(key string, newKey string) error
	Key(key string) TraceLogger
	KeyWithUpdate(key string) TraceLogger

	SyncAll() error
	LoadAll(path string, prefix ...string) error
	ClearAll() error
	DeleteUnusedCache(timeout time.Duration, method ...DeleteMethod) error

	AutoDelete(duration time.Duration, method ...DeleteMethod) error
	CancelAutoDelete() error

	AutoSync(duration time.Duration) error
	CancelAutoSync() error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL