logger

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 6, 2023 License: BSD-1-Clause Imports: 12 Imported by: 2

Documentation

Index

Constants

View Source
const (
	Italics   string = "\x1b[3m"
	Underline string = "\x1b[4m"
	Blink     string = "\x1b[5m"
	Bold      string = "\x1b[1m"

	Reset string = "\033[0m"

	Red    string = "\033[31m"
	Green  string = "\033[32m"
	Yellow string = "\033[33m"
	Blue   string = "\033[34m"
	Purple string = "\033[35m"
	Cyan   string = "\033[36m"
	White  string = "\033[37m"
	Grey   string = "\033[90m"

	BrightRed    string = "\033[31;1m"
	BrightGreen  string = "\033[32;1m"
	BrightYellow string = "\033[33;1m"
	BrightBlue   string = "\033[34;1m"
	BrightPurple string = "\033[35;1m"
	BrightCyan   string = "\033[36;1m"
	BrightGrey   string = "\033[37;1m"

	DimRed    string = "\033[31;2m"
	DimGreen  string = "\033[32;2m"
	DimYellow string = "\033[33;2m"
	DimBlue   string = "\033[34;2m"
	DimPurple string = "\033[35;2m"
	DimCyan   string = "\033[36;2m"
	DimGrey   string = "\033[37;2m"
)

ANSI color codes

Variables

View Source
var (
	// LogTest
	ColorLevelTest = Purple
	// LogDebug
	ColorLevelDebug = Green
	// LogInfo
	ColorLevelInfo = Blue
	// LogWarn
	ColorLevelWarning = Yellow
	// LogErr
	ColorLevelError = Red
	// No level, default switch case opt.
	ColorNoLevel = Green
)

Preset colors for use in the logger's Colorize function

Functions

func Colorize

func Colorize(msg string, colors ...string) string

Colorize a message.

func CutFrontPath

func CutFrontPath(s string, length int) string

Cut the front of a path, and add "..." if it was cut.

func CutStart

func CutStart(s string, length int, delim string, prefixIfCut bool) string

Cut the string if it is longer than the specified length, and add "..." if it was cut.

func DeColorize

func DeColorize(str string) string

Remove all ANSI color codes from a string.

func NewLogFile

func NewLogFile(filename string) (*os.File, error)

Types

type BatchLogger

type BatchLogger struct {
	// The prefix of the logger.
	Prefix string

	// The log level of the logger.
	Loglevel Loglevel

	// Colorize is a flag which determines whether the log entry is written colorized.
	Colorize bool

	// Handler is a function which determines how the log entry is handled.
	Handler func(entries []*LogEntry, stdout io.Writer)

	// File is the file to write to.
	File io.Writer
	// contains filtered or unexported fields
}

BatchLogger is a logger which logs messages in batches.

This logger is useful when you want to log a large number of messages or need to pool log messages.

This does not guarantee order of messages!

func NewBatchLogger

func NewBatchLogger(loglevel Loglevel, flushSize int, flushInterval time.Duration, file io.Writer, prefix ...string) *BatchLogger

NewBatchLogger creates a new Logger.

func (*BatchLogger) Critical

func (l *BatchLogger) Critical(e error)

Critical logs a critical message.

func (*BatchLogger) Criticalf

func (l *BatchLogger) Criticalf(format string, args ...any)

Criticalf logs a critical message with a format.

func (*BatchLogger) Debug

func (l *BatchLogger) Debug(args ...any)

Write a debug message, loglevel debug

func (*BatchLogger) Debugf

func (l *BatchLogger) Debugf(format string, args ...any)

Write a debug message, loglevel debug

Format the message in the fmt package format.

func (*BatchLogger) Error

func (l *BatchLogger) Error(args ...any)

Write an error message, loglevel error

func (*BatchLogger) Errorf

func (l *BatchLogger) Errorf(format string, args ...any)

Write an error message, loglevel error

Format the message in the fmt package format.

func (*BatchLogger) Info

func (l *BatchLogger) Info(args ...any)

Write an info message, loglevel info

func (*BatchLogger) Infof

func (l *BatchLogger) Infof(format string, args ...any)

Write an info message, loglevel info

Format the message in the fmt package format.

func (*BatchLogger) LogLevel

func (l *BatchLogger) LogLevel() request.LogLevel

Loglevel returns the loglevel of the logger.

func (*BatchLogger) Now

func (l *BatchLogger) Now(loglevel Loglevel, format string, args ...any)

Write a message instantly with the given loglevel.

func (*BatchLogger) Test

func (l *BatchLogger) Test(args ...any)

Write a test message, loglevel test

func (*BatchLogger) Testf

func (l *BatchLogger) Testf(format string, args ...any)

Write a test message, loglevel test

Format the message in the fmt package format.

func (*BatchLogger) Warning

func (l *BatchLogger) Warning(args ...any)

Write a warning message, loglevel warning

func (*BatchLogger) Warningf

func (l *BatchLogger) Warningf(format string, args ...any)

Write a warning message, loglevel warning

Format the message in the fmt package format.

type LogEntry

type LogEntry struct {
	Time       time.Time         `json:"time"`       // The time the log entry was created.
	Level      Loglevel          `json:"level"`      // The level of the log entry.
	Message    string            `json:"message"`    // The message of the log entry.
	Stacktrace tracer.StackTrace `json:"stacktrace"` // The tracer of the log entry.
}

A entry to be logged.

This may include a list of callers (Stacktrace)

func NewLogEntry

func NewLogEntry(level Loglevel, message string, stackTraceLen, skip int) *LogEntry

Intialize a new log entry.

level: The level of the log entry.

message: The message of the log entry.

stackTraceLen: The length of the stacktrace.

skip: The number of frames to skip in the stacktrace.

func (*LogEntry) AsString

func (e *LogEntry) AsString(prefix string, colorized bool) string

Generate a string representation of the log entry.

prefix: A prefix to add to the log entry.

colorized: If the log entry should be colorized.

type Logger

type Logger struct {
	Loglevel Loglevel

	File io.Writer
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(loglevel Loglevel, w io.Writer, prefix ...string) *Logger

func (*Logger) Critical

func (l *Logger) Critical(err error)

func (*Logger) Criticalf added in v1.0.1

func (l *Logger) Criticalf(format string, args ...any)

func (*Logger) Debug

func (l *Logger) Debug(args ...any)

Write a debug message, loglevel debug

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...any)

Write a debug message, loglevel debug

func (*Logger) Error

func (l *Logger) Error(args ...any)

Write an error message, loglevel error

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...any)

Write an error message, loglevel error

func (*Logger) Info

func (l *Logger) Info(args ...any)

Write an info message, loglevel info

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...any)

Write an info message, loglevel info

func (*Logger) LogLevel added in v1.0.2

func (l *Logger) LogLevel() request.LogLevel

func (*Logger) Test

func (l *Logger) Test(args ...any)

Write a test message, loglevel test

func (*Logger) Testf

func (l *Logger) Testf(format string, args ...any)

Write a test message, loglevel test

func (*Logger) Warning

func (l *Logger) Warning(args ...any)

Write a warning message, loglevel warning

func (*Logger) Warningf

func (l *Logger) Warningf(format string, args ...any)

Write a warning message, loglevel warning

type Loglevel

type Loglevel int
const (
	CRITICAL Loglevel = iota + 1
	ERROR
	WARNING
	INFO
	DEBUG
	TEST
)

func (Loglevel) String

func (l Loglevel) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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