logger

package module
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: GPL-3.0 Imports: 9 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Logger

Logger adds the option to write colored output

Documentation

Index

Constants

View Source
const (
	DEFAULT_COLOR        = "\x1b[0m"
	BLACK_COLOR          = "\x1b[30m"
	DARK_RED_COLOR       = "\x1b[31m"
	DARK_GREEN_COLOR     = "\x1b[32m"
	DARK_YELLOW_COLOR    = "\x1b[33m"
	DARK_BLUE_COLOR      = "\x1b[34m"
	DARK_MAGENTA_COLOR   = "\x1b[35m"
	DARK_CYAN_COLOR      = "\x1b[36m"
	DARK_WHITE_COLOR     = "\x1b[37m"
	BRIGHT_BLACK_COLOR   = "\x1b[90m"
	BRIGHT_RED_COLOR     = "\x1b[31m"
	BRIGHT_GREEN_COLOR   = "\x1b[32m"
	BRIGHT_YELLOW_COLOR  = "\x1b[33m"
	BRIGHT_BLUE_COLOR    = "\x1b[34m"
	BRIGHT_MAGENTA_COLOR = "\x1b[35m"
	BRIGHT_CYAN_COLOR    = "\x1b[36m"
	WHITE_COLOR          = "\x1b[37m"
)

Variables

View Source
var (
	TimeFormat = "2006-01-02 15:04:05.00" // TimeFormat defines which timestamp to use with the logs. It can be modified.
)

Functions

func Debug

func Debug(a ...any)

func Fatal added in v1.1.8

func Fatal(a ...any)

func Fatalf added in v1.1.8

func Fatalf(format string, a ...any)

func IndentString

func IndentString(s string, n int) string

IndentString takes a string and indents every line with the provided number of single spaces

func LogsToJSON added in v1.1.5

func LogsToJSON(logs []Log) []byte

func LogsToJSONIndented added in v1.1.5

func LogsToJSONIndented(logs []Log, spaces int) []byte

func PanicToErr

func PanicToErr(f func() error) error

CapturePanic is the same as PanicToErr but returns a plain error

func Print

func Print(level LogLevel, a ...any)

Print creates a Log with the given severity and message; any data after message will be used to populate the extra field of the Log automatically using the built-in function fmt.Sprint(extra...)

func Printf

func Printf(level LogLevel, format string, a ...any)

Printf creates a Log with the given severity; the rest of the arguments is used as the built-in function fmt.Sprintf(format, a...), however if the resulting string contains a line feed, everything after that will be used to populate the extra field of the Log

func RemoveTerminalColors

func RemoveTerminalColors(s string) string

func Stack

func Stack() string

Stack returns the execution stack. This must be called after a panic (during a recovery) because it strips the first lines where are reported also the recovery functions, returning only the panic-reletate stuff. If this is not desired just use the standard debug.Stack

func ToTerminal

func ToTerminal(out io.Writer) bool

Types

type Log

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

Log is the structure that can be will store any log reported with Logger. It keeps the error severity level (see the constants) the date it was created and the message associated with it (probably an error). It also has the optional field "extra" that can be used to store additional information

func (Log) Colored

func (l Log) Colored() string

func (Log) Date

func (l Log) Date() time.Time

func (Log) Extra

func (l Log) Extra() string

func (Log) Full

func (l Log) Full() string

func (Log) FullColored added in v1.2.4

func (l Log) FullColored() string

func (Log) ID

func (l Log) ID() string

func (Log) JSON

func (l Log) JSON() []byte

JSON returns the Log l in a json-encoded string in form of a slice of bytes

func (Log) Level

func (l Log) Level() LogLevel

func (Log) LevelMatchAny added in v1.1.4

func (l Log) LevelMatchAny(levels ...LogLevel) bool

func (Log) MarshalJSON added in v1.2.2

func (l Log) MarshalJSON() ([]byte, error)

func (Log) Match added in v1.1.4

func (l Log) Match(tags ...string) bool

func (Log) MatchAny added in v1.1.4

func (l Log) MatchAny(tags ...string) bool

func (Log) Message

func (l Log) Message() string

func (Log) RawExtra added in v1.2.2

func (l Log) RawExtra() string

func (Log) RawMessage added in v1.2.2

func (l Log) RawMessage() string

func (Log) String

func (l Log) String() string

func (Log) Tags added in v1.0.0

func (l Log) Tags() []string

func (*Log) UnmarshalJSON added in v1.2.2

func (l *Log) UnmarshalJSON(data []byte) error

type LogLevel

type LogLevel int

LogLevel defines the severity of a Log. See the constants

const (
	LOG_LEVEL_BLANK LogLevel = iota
	LOG_LEVEL_INFO
	LOG_LEVEL_DEBUG
	LOG_LEVEL_WARNING
	LOG_LEVEL_ERROR
	LOG_LEVEL_FATAL
)

func (LogLevel) MarshalJSON added in v1.0.0

func (level LogLevel) MarshalJSON() ([]byte, error)

func (LogLevel) String

func (level LogLevel) String() string

func (*LogLevel) UnmarshalJSON added in v1.0.0

func (level *LogLevel) UnmarshalJSON(b []byte) error

type Logger

type Logger struct {
	Out io.Writer
	// contains filtered or unexported fields
}

Logger is used by the Router and can be used by the user to create logs that are both written to the chosen io.Writer (if any) and saved locally in memory, so that they can be retreived programmatically and used (for example to make a view in a website)

var DefaultLogger *Logger

func NewLogger

func NewLogger(out io.Writer) *Logger

func (*Logger) AddLog added in v1.2.0

func (l *Logger) AddLog(level LogLevel, message string, extra string, writeOutput bool)

AddLog appends a log without behing printed out on the Logger output or by any parent in cascade

func (*Logger) AddTags added in v1.2.0

func (l *Logger) AddTags(tags ...string)

func (*Logger) Clone added in v1.0.0

func (l *Logger) Clone(out io.Writer, tags ...string) *Logger

func (*Logger) Debug added in v1.0.0

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

func (*Logger) DisableExtras added in v1.1.2

func (l *Logger) DisableExtras()

func (*Logger) DisableMultiLogger added in v1.2.0

func (l *Logger) DisableMultiLogger()

func (*Logger) EnableExtras added in v1.1.2

func (l *Logger) EnableExtras()

func (*Logger) EnableMultiLogger added in v1.2.0

func (l *Logger) EnableMultiLogger()

func (*Logger) JSON

func (l *Logger) JSON() []byte

JSON returns the list of logs stored in JSON format (see Log.JSON() method)

func (*Logger) JSONIndented added in v1.0.0

func (l *Logger) JSONIndented(spaces int) []byte

JSON returns the list of logs stored in JSON format (see Log.JSON() method)

func (*Logger) Logs

func (l *Logger) Logs() []Log

Logs returns the list of logs stored

func (*Logger) LogsLevelMatchAny added in v1.1.4

func (l *Logger) LogsLevelMatchAny(levels ...LogLevel) []Log

func (*Logger) LogsMatch added in v1.1.4

func (l *Logger) LogsMatch(tags ...string) []Log

func (*Logger) LogsMatchAny added in v1.1.4

func (l *Logger) LogsMatchAny(tags ...string) []Log

func (*Logger) Print

func (l *Logger) Print(level LogLevel, a ...any)

func (*Logger) Printf

func (l *Logger) Printf(level LogLevel, format string, a ...any)

func (*Logger) SetParent added in v1.2.1

func (l *Logger) SetParent(parent *Logger)

func (*Logger) Write

func (l *Logger) Write(p []byte) (n int, err error)

type PanicError

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

PanicError is the data used to capture any function returning an error and any generated panic. If Err is set, means that the function returned an error without panicking, instead if PanicErr is set, this means that the function returned prematurely due to a panic: the panic error is set and the stack trace is provided in the relative field

func CapturePanic added in v1.1.11

func CapturePanic(f func() error) (panicErr *PanicError)

PanicToErr runs any function that returns an error in a panic-controlled environment and converts any panic (if any) into an error. Expecially, the error generated by a panic can be unwrapped to get rid of the stack trace ( or manipulated to separate the error from the stack trace)

func (PanicError) Err

func (p PanicError) Err() error

func (PanicError) Error

func (err PanicError) Error() string

func (PanicError) PanicErr

func (p PanicError) PanicErr() error

func (PanicError) Stack

func (p PanicError) Stack() string

func (PanicError) String added in v1.1.6

func (err PanicError) String() string

func (PanicError) Unwrap

func (err PanicError) Unwrap() error

Jump to

Keyboard shortcuts

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