Documentation
¶
Index ¶
- type ColorLogger
- func (l *ColorLogger) Debug(v ...interface{})
- func (l *ColorLogger) Debugf(format string, v ...interface{})
- func (l *ColorLogger) Error(v ...interface{})
- func (l *ColorLogger) Errorf(format string, v ...interface{})
- func (l *ColorLogger) Printf(format string, v ...interface{})
- func (l *ColorLogger) Println(v ...interface{})
- type LogInfo
- type Logger
- type OutputType
- type PlainLogger
- func (l *PlainLogger) Debug(v ...interface{})
- func (l *PlainLogger) Debugf(format string, v ...interface{})
- func (l *PlainLogger) Error(v ...interface{})
- func (l *PlainLogger) Errorf(format string, v ...interface{})
- func (l *PlainLogger) Printf(format string, v ...interface{})
- func (l *PlainLogger) Println(v ...interface{})
- type PrettyHandler
- type PrettyHandlerOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ColorLogger ¶ added in v2.1.8
ColorLogger is a logger that outputs messages in a specified color. It enhances readability by color-coding log messages based on their severity or purpose.
**Attributes:**
Info: LogInfo object containing information about the log file. ColorAttribute: A color attribute for output styling. Logger: The slog Logger instance used for logging operations.
func (*ColorLogger) Debug ¶ added in v2.1.8
func (l *ColorLogger) Debug(v ...interface{})
Debug for ColorLogger logs the provided arguments as a debug line in the specified color. The arguments are handled in the manner of fmt.Println.
func (*ColorLogger) Debugf ¶ added in v2.1.8
func (l *ColorLogger) Debugf(format string, v ...interface{})
Debugf for ColorLogger logs the provided formatted string as a debug line in the specified color. The format and arguments are handled in the manner of fmt.Printf.
func (*ColorLogger) Error ¶ added in v2.1.8
func (l *ColorLogger) Error(v ...interface{})
Error for ColorLogger logs the provided arguments as an error line in the specified color. The arguments are handled in the manner of fmt.Println.
func (*ColorLogger) Errorf ¶ added in v2.1.8
func (l *ColorLogger) Errorf(format string, v ...interface{})
Errorf for ColorLogger logs the provided formatted string as an error line in the specified color. The format and arguments are handled in the manner of fmt.Printf.
func (*ColorLogger) Printf ¶ added in v2.1.8
func (l *ColorLogger) Printf(format string, v ...interface{})
Printf for ColorLogger logs the provided formatted string in the specified color. The format and arguments are handled in the manner of fmt.Printf.
func (*ColorLogger) Println ¶ added in v2.1.8
func (l *ColorLogger) Println(v ...interface{})
Println for ColorLogger logs the provided arguments as a line in the specified color. The arguments are handled in the manner of fmt.Println.
type LogInfo ¶
LogInfo represents parameters used to manage logging throughout a program.
**Attributes:**
Dir: A string representing the directory where the log file is located. File: An afero.File object representing the log file. FileName: A string representing the name of the log file. Path: A string representing the full path to the log file.
func CreateLogFile ¶
CreateLogFile creates a log file in a 'logs' subdirectory of the specified directory. The log file's name is the provided log name with the extension '.log'.
**Parameters:**
fs: An afero.Fs instance to mock filesystem for testing. logDir: A string for the directory where 'logs' subdirectory and log file should be created. logName: A string for the name of the log file to be created.
**Returns:**
LogInfo: A LogInfo struct with information about the log file, including its directory, file pointer, file name, and path. error: An error, if an issue occurs while creating the directory or the log file.
Example ¶
package main
import (
"fmt"
"path/filepath"
"github.com/l50/goutils/v2/logging"
"github.com/spf13/afero"
)
func main() {
fs := afero.NewOsFs()
logDir := filepath.Join("/tmp", "logs")
logName := "test.log"
logPath := filepath.Join(logDir, logName)
logInfo, err := logging.CreateLogFile(fs, logPath)
if err != nil {
fmt.Printf("failed to create log file: %v", err)
return
}
fmt.Printf("log file created at: %s", logInfo.Path)
// Clean up
if err := fs.Remove(logInfo.Path); err != nil {
fmt.Printf("failed to clean up: %v", err)
}
// Unpredictable output due to timestamps and structured logging
}
Output:
type Logger ¶ added in v2.0.6
type Logger interface {
Println(v ...interface{})
Printf(format string, v ...interface{})
Error(v ...interface{})
Errorf(format string, v ...interface{})
Debug(v ...interface{})
Debugf(format string, v ...interface{})
}
Logger is an interface that defines methods for a generic logging system. It supports basic logging operations like printing, formatted printing, error logging, and debug logging.
**Methods:**
Println: Outputs a line with the given arguments. Printf: Outputs a formatted string. Error: Logs an error message. Errorf: Logs a formatted error message. Debug: Logs a debug message. Debugf: Logs a formatted debug message.
var GlobalLogger Logger
GlobalLogger is a global variable that holds the instance of the logger.
func ConfigureLogger ¶ added in v2.0.6
func ConfigureLogger(fs afero.Fs, level slog.Level, path string, outputType OutputType) (Logger, error)
ConfigureLogger sets up a logger based on the provided logging level, file path, and output type. It supports both colorized and plain text logging output, selectable via the OutputType parameter. The logger writes log entries to both a file and standard output.
**Parameters:**
level: Logging level as a slog.Level. path: Path to the log file. outputType: Type of log output, either ColorOutput or PlainOutput.
**Returns:**
Logger: Configured Logger object based on provided parameters. error: An error, if an issue occurs while setting up the logger.
Example ¶
package main
import (
"fmt"
"log/slog"
"github.com/l50/goutils/v2/logging"
"github.com/spf13/afero"
)
func plainLoggerExample() {
fs := afero.NewOsFs()
logger, err := logging.ConfigureLogger(fs, slog.LevelDebug, "/tmp/test.log", logging.PlainOutput)
if err != nil {
fmt.Printf("failed to configure logger: %v", err)
return
}
logger.Println("This is a log message")
logger.Error("This is an error log message")
logger.Errorf("This is a formatted error log message: %s", "Error details")
fmt.Println("Logger configured successfully.")
}
func colorLoggerExample() {
fs := afero.NewOsFs()
logger, err := logging.ConfigureLogger(fs, slog.LevelDebug, "/tmp/test.log", logging.ColorOutput)
if err != nil {
fmt.Printf("failed to configure logger: %v", err)
return
}
logger.Println("This is a log message")
logger.Error("This is an error log message")
logger.Errorf("This is a formatted error log message: %s", "Error details")
fmt.Println("Logger configured successfully.")
}
func main() {
plainLoggerExample()
colorLoggerExample()
// Unpredictable output due to timestamps and structured logging
}
Output:
func InitLogging ¶ added in v2.2.0
func InitLogging(fs afero.Fs, logPath string, level slog.Level, outputType OutputType) (Logger, error)
InitLogging sets up logging with a single function call. It creates a log file and configures the logger based on the specified parameters.
**Parameters:**
fs: An afero.Fs instance for filesystem operations, allows mocking in tests. logDir: The directory where the log file should be created. logName: The name of the log file. level: The logging level. outputType: The output type of the logger (PlainOutput or ColorOutput).
**Returns:**
Logger: A configured Logger object. error: An error if any issue occurs during initialization.
Example ¶
package main
import (
"fmt"
"log/slog"
"path/filepath"
"github.com/l50/goutils/v2/logging"
"github.com/spf13/afero"
)
func main() {
fs := afero.NewOsFs()
logDir := filepath.Join("/tmp", "logs")
logName := "test.log"
logPath := filepath.Join(logDir, logName)
logger, err := logging.InitLogging(fs, logPath, slog.LevelDebug, logging.PlainOutput)
if err != nil {
fmt.Printf("failed to initialize logging: %v", err)
return
}
logger.Println("This is a log message")
logger.Error("This is an error log message")
logger.Errorf("This is a formatted error log message: %s", "Error details")
// Since we can't predict the log message, print a static message instead.
fmt.Println("Logger configured successfully.")
// Clean up
if err := fs.Remove(logPath); err != nil {
fmt.Printf("failed to clean up: %v", err)
}
// Unpredictable output due to timestamps and structured logging
}
Output:
type OutputType ¶ added in v2.1.8
type OutputType int
OutputType is an enumeration type that specifies the output format of the logger. It can be either plain text or colorized text.
const ( // PlainOutput indicates that the logger will produce plain text // output without any colorization. This is suitable for log // files or environments where ANSI color codes are not supported. PlainOutput OutputType = iota // ColorOutput indicates that the logger will produce colorized // text output. This is useful for console output where color // coding can enhance readability. ColorOutput )
type PlainLogger ¶ added in v2.0.6
PlainLogger is a logger implementation using the slog library. It provides structured logging capabilities.
**Attributes:**
Info: LogInfo object containing information about the log file. Logger: The slog Logger instance used for logging operations.
func (*PlainLogger) Debug ¶ added in v2.1.1
func (l *PlainLogger) Debug(v ...interface{})
Debug for PlainLogger logs the provided arguments as a debug line using slog library. The arguments are converted to a string using fmt.Sprint.
func (*PlainLogger) Debugf ¶ added in v2.1.1
func (l *PlainLogger) Debugf(format string, v ...interface{})
Debugf for PlainLogger logs the provided formatted string as a debug line using slog library. The format and arguments are handled in the manner of fmt.Printf.
func (*PlainLogger) Error ¶ added in v2.0.7
func (l *PlainLogger) Error(v ...interface{})
Error for PlainLogger logs the provided arguments as an error line using slog library. The arguments are converted to a string using fmt.Sprint.
func (*PlainLogger) Errorf ¶ added in v2.0.7
func (l *PlainLogger) Errorf(format string, v ...interface{})
Errorf for PlainLogger logs the provided formatted string as an error line using slog library. The format and arguments are handled in the manner of fmt.Printf.
func (*PlainLogger) Printf ¶ added in v2.0.6
func (l *PlainLogger) Printf(format string, v ...interface{})
Printf for PlainLogger logs the provided formatted string using slog library. The format and arguments are handled in the manner of fmt.Printf.
func (*PlainLogger) Println ¶ added in v2.0.6
func (l *PlainLogger) Println(v ...interface{})
Println for PlainLogger logs the provided arguments as a line using slog library. The arguments are converted to a string using fmt.Sprint.
type PrettyHandler ¶ added in v2.1.8
PrettyHandler is a custom log handler that provides colorized logging output. It wraps around slog.Handler and adds color to log messages based on their level.
**Attributes:**
Handler: The underlying slog.Handler used for logging. l: Standard logger used for outputting log messages.
func NewPrettyHandler ¶ added in v2.1.8
func NewPrettyHandler(out io.Writer, opts PrettyHandlerOptions) *PrettyHandler
NewPrettyHandler creates a new PrettyHandler with specified output writer and options. It configures a PrettyHandler for colorized logging output.
**Parameters:**
out: Output writer where log messages will be written. opts: PrettyHandlerOptions for configuring the handler.
**Returns:**
*PrettyHandler: A new instance of PrettyHandler.
func (*PrettyHandler) Handle ¶ added in v2.1.8
Handle formats and outputs a log message for PrettyHandler. It colorizes the log level, message, and adds structured fields to the log output.
**Parameters:**
ctx: Context for the log record. r: The log record containing log data.
**Returns:**
error: An error if any issue occurs during log handling.
type PrettyHandlerOptions ¶ added in v2.1.8
type PrettyHandlerOptions struct {
SlogOpts slog.HandlerOptions
}
PrettyHandlerOptions represents options used for configuring the PrettyHandler.
**Attributes:**
SlogOpts: Options for the underlying slog.Handler.