log

package
v0.0.0-...-1a2806f Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FATAL = iota
	ERROR
	TRACE
	WARNING
	INFO
	DEBUG
)

Log Levels Define 0 Fatal: system is unusable 1 Error: error conditions 2 Trace: trace conditions 3 Warning: warning conditions 4 Info: informational messages 5 Debug: debug-level messages

View Source
const BATCH_LOG_SIZE = 1

Variables

View Source
var LogLevelPrefix = [DEBUG + 1]string{" [FATAL] ", " [ERROR] ", " [TRACE] ", " [WARNING] ", " [INFO] ", " [DEBUG] "}

LogLevelPrefix level format to string

Functions

func Debug

func Debug(v ...interface{}) error

Debug debug-level messages

func Error

func Error(v ...interface{}) error

Error error conditions

func Fatal

func Fatal(v ...interface{}) error

Fatal system is unusable

func FormatLog

func FormatLog(level int32, v ...interface{}) string

FormatLog format log and return string if len(v) > 1 ,format = v[0]

func GetStack

func GetStack(all bool) string

GetStack get current stack

func Info

func Info(v ...interface{}) error

Info informational messages

func InitLogger

func InitLogger(conf *Config)

SetGLogger set logger module instance

func PrintStack

func PrintStack(all bool)

PrintStack print current stack

func Run

func Run(s *LogModule)

func Trace

func Trace(v ...interface{}) error

Trace error conditions

func TraceCost

func TraceCost(msg string) func()

TraceCost calc the api cost time usage: defer TraceCose("func")()

func Warning

func Warning(v ...interface{}) error

Warning warning conditions

func WriteLog

func WriteLog(level int32, v ...interface{}) error

WriteLog send log to queue

Types

type Config

type Config struct {
	Filename     string `mapstructure:"filename"`
	Level        int32  `mapstructure:"level"`
	MaxSize      int32  `mapstructure:"max_size"`
	MaxLines     int32  `mapstructure:"max_lines"`
	HourEnabled  bool   `mapstructure:"hour_enabled"`
	DailyEnabled bool   `mapstructure:"daily_enabled"`
	QueueSize    int    `mapstructure:"queue_size"`
}

Config log attributes in config file

func (*Config) GetQueueSize

func (c *Config) GetQueueSize() int

GetQueueSize get module queue size

type ConsoleLog

type ConsoleLog struct {
	Conf *Config
	// contains filtered or unexported fields
}

ConsoleLog writes messages to terminal.

func (*ConsoleLog) CanLog

func (c *ConsoleLog) CanLog(msgSize int32, count int32) bool

CanLog check log status

func (*ConsoleLog) Close

func (c *ConsoleLog) Close() error

Close and flush

func (*ConsoleLog) Debug

func (c *ConsoleLog) Debug(v ...interface{}) error

Debug debug-level messages

func (*ConsoleLog) Error

func (c *ConsoleLog) Error(v ...interface{}) error

Error error conditions

func (*ConsoleLog) Fatal

func (c *ConsoleLog) Fatal(v ...interface{}) error

Fatal system is unusable

func (*ConsoleLog) GetLevel

func (c *ConsoleLog) GetLevel() int32

GetLevel atomic get level

func (*ConsoleLog) Info

func (c *ConsoleLog) Info(v ...interface{}) error

Info informational messages

func (*ConsoleLog) InitLogger

func (c *ConsoleLog) InitLogger(conf *Config) error

InitLogger init logger

func (*ConsoleLog) ParseConfig

func (c *ConsoleLog) ParseConfig(conf *Config) error

ParseConfig read config from map[string]interface{} config key map level default 7

func (*ConsoleLog) PushLog

func (c *ConsoleLog) PushLog(level int32, v ...interface{}) error

PushLog push log to queue

func (*ConsoleLog) SetLevel

func (c *ConsoleLog) SetLevel(level int32) error

SetLevel atomic set level

func (*ConsoleLog) Warning

func (c *ConsoleLog) Warning(v ...interface{}) error

Warning warning conditions

func (*ConsoleLog) WriteLog

func (c *ConsoleLog) WriteLog(msg []byte, count int32) error

WriteLog write log to file, return immediately if not meet the conditions

type FileLog

type FileLog struct {
	Conf *Config
	// contains filtered or unexported fields
}

FileLog implements Log interface, not goruntine safety write log to file,if file reached limit,rename file match format filename support filesize limit / time frequency / lines limit filename read from config like server.log and the real filename like server_20190101-01.log or server_20190101.log hourEnabled if enabled , filename like server_20190101-01.log dailyEnabled if enabled, filename like server_20190101.log else filename like server.log filename format = filename_ymd-h_num.suffix

func (*FileLog) CanLog

func (l *FileLog) CanLog(msgSize int32, count int32) bool

CanLog check log status

func (*FileLog) Close

func (l *FileLog) Close() error

Close close file logger

func (*FileLog) Debug

func (l *FileLog) Debug(v ...interface{}) error

Debug debug-level messages

func (*FileLog) Error

func (l *FileLog) Error(v ...interface{}) error

Error error conditions

func (*FileLog) Fatal

func (l *FileLog) Fatal(v ...interface{}) error

Fatal system is unusable

func (*FileLog) GetLevel

func (l *FileLog) GetLevel() int32

GetLevel get current log level

func (*FileLog) Info

func (l *FileLog) Info(v ...interface{}) error

Info informational messages

func (*FileLog) InitLogger

func (l *FileLog) InitLogger(conf *Config) error

InitLogger init logger

func (*FileLog) ParseConfig

func (l *FileLog) ParseConfig(conf *Config) error

ParseConfig read config from map[string]interface{} config key map filename default server.log level default 7 maxSize default 1024 maxLines default 100000 hourEnabled default false dailyEnabled default true

func (*FileLog) PushLog

func (l *FileLog) PushLog(level int32, v ...interface{}) error

PushLog push log to queue

func (*FileLog) SetLevel

func (l *FileLog) SetLevel(level int32) error

SetLevel update log level

func (*FileLog) Warning

func (l *FileLog) Warning(v ...interface{}) error

Warning warning conditions

func (*FileLog) WriteLog

func (l *FileLog) WriteLog(msg []byte, count int32) error

WriteLog write message to file, return immediately if not meet the conditions

type ILog

type ILog interface {
	// ParseConfig read config from conf object
	ParseConfig(conf *Config) error
	// InitLogger init logger
	InitLogger(conf *Config) error
	// SetLevel atomic set level value
	SetLevel(level int32) error
	// GetLevel atomic get level value
	GetLevel() int32

	// Fatal system is unusable
	Fatal(v ...interface{}) error
	// Error error conditions
	Error(v ...interface{}) error
	// Warning warning conditions
	Warning(v ...interface{}) error
	// Info informational messages
	Info(v ...interface{}) error
	// Debug debug-level messages
	Debug(v ...interface{}) error
	// WriteLog write log to file, return immediately if not meet the conditions
	WriteLog(msg []byte, count int32) error
	// CanLog check log status
	CanLog(msgSize int32, count int32) bool
	// Close and flush
	Close() error
}

ILog log Interface

func NewConsoleLogger

func NewConsoleLogger(conf *Config) (ILog, error)

NewConsoleLogger create FileLog instance

func NewFileLogger

func NewFileLogger(conf *Config) (ILog, error)

NewFileLogger create FileLog instance

type LogModule

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

LogModule struct implements the interface Module

var GLoggerModule *LogModule

GLoggerModule global log module

func (*LogModule) CanExit

func (s *LogModule) CanExit(doneFlag bool) (bool, bool)

CanExit if receive ctx.Done() and queue is empty ,then return true

func (*LogModule) Close

func (s *LogModule) Close()

Close call context cancel ,self and all child module will receive context.Done()

func (*LogModule) Context

func (s *LogModule) Context() context.Context

Context get module context

func (*LogModule) IdleTimes

func (s *LogModule) IdleTimes() uint32

IdleTimes get idle times

func (*LogModule) IdleTimesAdd

func (s *LogModule) IdleTimesAdd()

IdleTimesAdd add idle times

func (*LogModule) IdleTimesReset

func (s *LogModule) IdleTimesReset()

IdleTimesReset reset idle times

func (*LogModule) MQ

func (s *LogModule) MQ() queue.Queue

MQ return module queue

func (*LogModule) OnRun

func (s *LogModule) OnRun(dt time.Duration)

OnRun goruntine run and call OnRun , always use ModuleRun to call this function

func (*LogModule) PushBytes

func (s *LogModule) PushBytes(option int32, buf []byte) error

PushBytes async push string or bytes to queue, with option

func (*LogModule) Reload

func (s *LogModule) Reload(conf *Config) error

Reload reload config from map

func (*LogModule) Setup

func (s *LogModule) Setup(conf *Config) (*LogModule, error)

Setup init and setup config Log config

m := map[string]interface{}{
	"filename":    "server.log",
	"level":       4,
	"maxSize":     50,
	"maxLines":    1000,
	"hourEnabled": false,
	"dailyEnable": true,
 "queueSize":1000,
}

func (*LogModule) Stop

func (s *LogModule) Stop() error

Stop goruntine

func (*LogModule) UserData

func (s *LogModule) UserData() int32

UserData module custom option, can you store you self value

Jump to

Keyboard shortcuts

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