log

package
v1.17.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: MIT, MIT Imports: 6 Imported by: 20

README

Log

Write logs with color-coded output for different log levels.

Usage

Quick usage

import "github.com/gflydev/core/log"

log.Errorf("Invalid field name")
Color Coding

The logger uses color-coded output for different log levels to improve readability:

  • TRACE: Gray
  • DEBUG: Blue
  • INFO: Green
  • WARN: Yellow
  • ERROR: Red
  • FATAL: Purple
  • PANIC: Cyan

Colors are automatically applied when logging to a terminal.

Log Format

The logger outputs log messages with the following format:

LEVEL message

For example:

ERROR Invalid field name

The log level is color-coded as described above.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(v ...any)

Debug logs a debug message using the default logger's Debug method.

Parameters:

  • v (...any): Variadic arguments that are passed to the logger.

Returns:

  • None.

func Debugf

func Debugf(format string, v ...any)

Debugf logs a formatted debug message using the default logger's Debugf method.

Parameters:

  • format (string): The format string.
  • v (...any): Values to format.

Returns:

  • None.

func Debugw

func Debugw(msg string, keysAndValues ...any)

Debugw logs a debug message with context key-value pairs using the default logger's Debugw method.

Parameters:

  • msg (string): The message to log.
  • keysAndValues (...any): Key-value pairs providing additional context.

Returns:

  • None.

func Error

func Error(v ...any)

Error logs an error message using the default logger's Error method.

Parameters:

  • v (...any): Variadic arguments that are passed to the logger.

Returns:

  • None.

func Errorf

func Errorf(format string, v ...any)

Errorf logs a formatted error message using the default logger's Errorf method.

Parameters:

  • format (string): The format string.
  • v (...any): Values to format.

Returns:

  • None.

func Errorw

func Errorw(msg string, keysAndValues ...any)

Errorw logs an error message with context key-value pairs using the default logger's Errorw method.

Parameters:

  • msg (string): The message to log.
  • keysAndValues (...any): Key-value pairs providing additional context.

Returns:

  • None.

func Fatal

func Fatal(v ...any)

Fatal calls the default logger's Fatal method and then exits the program.

Parameters:

  • v (...any): Variadic arguments that are passed to the logger.

Returns:

  • None (the program terminates with os.Exit(1)).

func Fatalf

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

Fatalf logs a formatted fatal message and then exits the program.

Parameters:

  • format (string): The format string.
  • v (...any): Values to format.

Returns:

  • None (the program terminates with os.Exit(1)).

func Fatalw

func Fatalw(msg string, keysAndValues ...any)

Fatalw logs a fatal message with context key-value pairs and then exits the program.

Parameters:

  • msg (string): The message to log.
  • keysAndValues (...any): Key-value pairs providing additional context.

Returns:

  • None (the program terminates with os.Exit(1)).

func Info

func Info(v ...any)

Info logs an informational message using the default logger's Info method.

Parameters:

  • v (...any): Variadic arguments that are passed to the logger.

Returns:

  • None.

func Infof

func Infof(format string, v ...any)

Infof logs a formatted informational message using the default logger's Infof method.

Parameters:

  • format (string): The format string.
  • v (...any): Values to format.

Returns:

  • None.

func Infow

func Infow(msg string, keysAndValues ...any)

Infow logs an informational message with context key-value pairs using the default logger's Infow method.

Parameters:

  • msg (string): The message to log.
  • keysAndValues (...any): Key-value pairs providing additional context.

Returns:

  • None.

func Panic

func Panic(v ...any)

Panic logs a message at the Panic level using the default logger's Panic method. Note: Despite the name, this method does not actually panic.

Parameters:

  • v (...any): Variadic arguments that are passed to the logger.

Returns:

  • None.

func Panicf

func Panicf(format string, v ...any)

Panicf logs a formatted message at the Panic level using the default logger's Panicf method. Note: Despite the name, this method does not actually panic.

Parameters:

  • format (string): The format string.
  • v (...any): Values to format.

Returns:

  • None.

func Panicw

func Panicw(msg string, keysAndValues ...any)

Panicw logs a message at the Panic level with context key-value pairs using the default logger's Panicw method. Note: Despite the name, this method does not actually panic.

Parameters:

  • msg (string): The message to log.
  • keysAndValues (...any): Key-value pairs providing additional context.

Returns:

  • None.

func SetLevel

func SetLevel(lv Level)

SetLevel adjusts the logging level to filter messages below a certain severity.

Parameters:

  • lv (Level): The logging level to set.

Returns:

  • None.

func SetLogger

func SetLogger(v AllLogger)

SetLogger sets the default logger to a new instance.

Parameters:

  • v (AllLogger): The new logger to be set.

Returns:

  • None.

func SetOutput

func SetOutput(w io.Writer)

SetOutput changes the output destination of the logger.

Parameters:

  • w (io.Writer): The new output destination.

Returns:

  • None.

func Trace

func Trace(v ...any)

Trace logs a trace message using the default logger's Trace method.

Parameters:

  • v (...any): Variadic arguments that are passed to the logger.

Returns:

  • None.

func Tracef

func Tracef(format string, v ...any)

Tracef logs a formatted trace message using the default logger's Tracef method.

Parameters:

  • format (string): The format string.
  • v (...any): Values to format.

Returns:

  • None.

func Tracew

func Tracew(msg string, keysAndValues ...any)

Tracew logs a trace message with context key-value pairs using the default logger's Tracew method.

Parameters:

  • msg (string): The message to log.
  • keysAndValues (...any): Key-value pairs providing additional context.

Returns:

  • None.

func Warn

func Warn(v ...any)

Warn logs a warning message using the default logger's Warn method.

Parameters:

  • v (...any): Variadic arguments that are passed to the logger.

Returns:

  • None.

func Warnf

func Warnf(format string, v ...any)

Warnf logs a formatted warning message using the default logger's Warnf method.

Parameters:

  • format (string): The format string.
  • v (...any): Values to format.

Returns:

  • None.

func Warnw

func Warnw(msg string, keysAndValues ...any)

Warnw logs a warning message with context key-value pairs using the default logger's Warnw method.

Parameters:

  • msg (string): The message to log.
  • keysAndValues (...any): Key-value pairs providing additional context.

Returns:

  • None.

Types

type AllLogger

type AllLogger interface {
	CommonLogger
	ControlLogger
	// WithContext returns a logger that is associated with a given context.
	// Parameters:
	//   - ctx: The context to associate with the logger.
	WithContext(ctx context.Context) CommonLogger
}

AllLogger is the combination of Logger, FormatLogger, CtxLogger, and ControlLogger. Custom extensions can be made through AllLogger.

func DefaultLogger

func DefaultLogger() AllLogger

DefaultLogger returns the default logger instance. Parameters: None Returns: (AllLogger) The default logger instance.

type CommonLogger

type CommonLogger interface {
	Logger
	FormatLogger
	WithLogger
}

func WithContext

func WithContext(ctx context.Context) CommonLogger

WithContext creates a new logger with the provided context.

Parameters:

  • ctx (context.Context): The context to associate with the logger.

Returns:

  • CommonLogger: A logger instance associated with the given context.

type ControlLogger

type ControlLogger interface {
	// SetLevel sets the logging level for the logger.
	// Parameters:
	//   - level: The logging level to set.
	SetLevel(Level)
	// SetOutput sets the output destination for the logger.
	// Parameters:
	//   - writer: The io.Writer to write logs to.
	SetOutput(io.Writer)
}

ControlLogger provides methods to configure a logger.

type FormatLogger

type FormatLogger interface {
	// Tracef logs a formatted message at the Trace level.
	// Parameters:
	//   - format: The format string.
	//   - v: The variables or objects to format and log.
	Tracef(format string, v ...any)
	// Debugf logs a formatted message at the Debug level.
	// Parameters:
	//   - format: The format string.
	//   - v: The variables or objects to format and log.
	Debugf(format string, v ...any)
	// Infof logs a formatted message at the Info level.
	// Parameters:
	//   - format: The format string.
	//   - v: The variables or objects to format and log.
	Infof(format string, v ...any)
	// Warnf logs a formatted message at the Warning level.
	// Parameters:
	//   - format: The format string.
	//   - v: The variables or objects to format and log.
	Warnf(format string, v ...any)
	// Errorf logs a formatted message at the Error level.
	// Parameters:
	//   - format: The format string.
	//   - v: The variables or objects to format and log.
	Errorf(format string, v ...any)
	// Fatalf logs a formatted message at the Fatal level and exits the application.
	// Parameters:
	//   - format: The format string.
	//   - v: The variables or objects to format and log.
	Fatalf(format string, v ...any)
	// Panicf logs a formatted message at the Panic level.
	// Note: Despite the name, this method does not actually panic.
	// Parameters:
	//   - format: The format string.
	//   - v: The variables or objects to format and log.
	Panicf(format string, v ...any)
}

FormatLogger is a logger interface that outputs logs with a format.

type Level

type Level int

Level defines the priority of a log message. When a logger is configured with a level, any log message with a lower log level (smaller by integer comparison) will not be output.

const (
	LevelTrace Level = iota // Trace represents very detailed debug information.
	LevelDebug              // Debug represents general debugging information.
	LevelInfo               // Info represents informational messages.
	LevelWarn               // Warn represents warning messages.
	LevelError              // Error represents error messages.
	LevelFatal              // Fatal represents critical error messages and causes the program to exit.
	LevelPanic              // Panic represents critical error messages (but does not actually cause a panic).
)

The levels of logs.

type Logger

type Logger interface {
	// Trace logs a message at the Trace level.
	// Parameters:
	//   - v: The variables or objects to log.
	Trace(v ...any)
	// Debug logs a message at the Debug level.
	// Parameters:
	//   - v: The variables or objects to log.
	Debug(v ...any)
	// Info logs a message at the Info level.
	// Parameters:
	//   - v: The variables or objects to log.
	Info(v ...any)
	// Warn logs a message at the Warning level.
	// Parameters:
	//   - v: The variables or objects to log.
	Warn(v ...any)
	// Error logs a message at the Error level.
	// Parameters:
	//   - v: The variables or objects to log.
	Error(v ...any)
	// Fatal logs a message at the Fatal level and exits the application.
	// Parameters:
	//   - v: The variables or objects to log.
	Fatal(v ...any)
	// Panic logs a message at the Panic level.
	// Note: Despite the name, this method does not actually panic.
	// Parameters:
	//   - v: The variables or objects to log.
	Panic(v ...any)
}

Logger is a logger interface that provides logging function with levels.

type WithLogger

type WithLogger interface {
	// Tracew logs a message at the Trace level with key-value pairs.
	// Parameters:
	//   - msg: The message string.
	//   - keysAndValues: The key-value pairs to log.
	Tracew(msg string, keysAndValues ...any)
	// Debugw logs a message at the Debug level with key-value pairs.
	// Parameters:
	//   - msg: The message string.
	//   - keysAndValues: The key-value pairs to log.
	Debugw(msg string, keysAndValues ...any)
	// Infow logs a message at the Info level with key-value pairs.
	// Parameters:
	//   - msg: The message string.
	//   - keysAndValues: The key-value pairs to log.
	Infow(msg string, keysAndValues ...any)
	// Warnw logs a message at the Warning level with key-value pairs.
	// Parameters:
	//   - msg: The message string.
	//   - keysAndValues: The key-value pairs to log.
	Warnw(msg string, keysAndValues ...any)
	// Errorw logs a message at the Error level with key-value pairs.
	// Parameters:
	//   - msg: The message string.
	//   - keysAndValues: The key-value pairs to log.
	Errorw(msg string, keysAndValues ...any)
	// Fatalw logs a message at the Fatal level with key-value pairs and exits the application.
	// Parameters:
	//   - msg: The message string.
	//   - keysAndValues: The key-value pairs to log.
	Fatalw(msg string, keysAndValues ...any)
	// Panicw logs a message at the Panic level with key-value pairs.
	// Note: Despite the name, this method does not actually panic.
	// Parameters:
	//   - msg: The message string.
	//   - keysAndValues: The key-value pairs to log.
	Panicw(msg string, keysAndValues ...any)
}

WithLogger is a logger interface that outputs logs with a message and key-value pairs.

Jump to

Keyboard shortcuts

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