Documentation
¶
Index ¶
- type ColoredLogger
- func (l *ColoredLogger) Debug(v ...interface{})
- func (l *ColoredLogger) Debugf(format string, v ...interface{})
- func (l *ColoredLogger) Error(v ...interface{})
- func (l *ColoredLogger) Errorf(format string, v ...interface{})
- func (l *ColoredLogger) Printf(format string, v ...interface{})
- func (l *ColoredLogger) Println(v ...interface{})
- type LogInfo
- type Logger
- 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 SlogLogger
- func (l *SlogLogger) Debug(v ...interface{})
- func (l *SlogLogger) Debugf(format string, v ...interface{})
- func (l *SlogLogger) Error(v ...interface{})
- func (l *SlogLogger) Errorf(format string, v ...interface{})
- func (l *SlogLogger) Printf(format string, v ...interface{})
- func (l *SlogLogger) Println(v ...interface{})
- type SlogPlainLogger
- func (l *SlogPlainLogger) Debug(v ...interface{})
- func (l *SlogPlainLogger) Debugf(format string, v ...interface{})
- func (l *SlogPlainLogger) Error(v ...interface{})
- func (l *SlogPlainLogger) Errorf(format string, v ...interface{})
- func (l *SlogPlainLogger) Printf(format string, v ...interface{})
- func (l *SlogPlainLogger) Println(v ...interface{})
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ColoredLogger ¶ added in v2.0.6
ColoredLogger represents a logger that outputs in cyan color.
**Attributes:**
Info: LogInfo object containing information about the log file. ColorAttribute: A color.Attribute object representing the color
func (*ColoredLogger) Debug ¶ added in v2.1.1
func (l *ColoredLogger) Debug(v ...interface{})
Debug for ColoredLogger logs the provided arguments as a debug line in the specified color. The arguments are handled in the manner of fmt.Println.
func (*ColoredLogger) Debugf ¶ added in v2.1.1
func (l *ColoredLogger) Debugf(format string, v ...interface{})
Debugf for ColoredLogger 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 (*ColoredLogger) Error ¶ added in v2.0.7
func (l *ColoredLogger) Error(v ...interface{})
Error for ColoredLogger logs the provided arguments as an error line in the specified color. The arguments are handled in the manner of fmt.Println.
func (*ColoredLogger) Errorf ¶ added in v2.0.7
func (l *ColoredLogger) Errorf(format string, v ...interface{})
Errorf for ColoredLogger 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 (*ColoredLogger) Printf ¶ added in v2.0.6
func (l *ColoredLogger) Printf(format string, v ...interface{})
Printf for ColoredLogger logs the provided formatted string in the specified color. The format and arguments are handled in the manner of fmt.Printf.
func (*ColoredLogger) Println ¶ added in v2.0.6
func (l *ColoredLogger) Println(v ...interface{})
Println for ColoredLogger 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"
"github.com/fatih/color"
"github.com/l50/goutils/v2/logging"
"github.com/spf13/afero"
)
func main() {
fs := afero.NewOsFs()
logDir := "/tmp"
logName := "test.log"
logInfo, err := logging.CreateLogFile(fs, logDir, logName)
if err != nil {
fmt.Printf("failed to create log file: %v", err)
return
}
// Specify the color attribute for the ColoredLogger
var logger = &logging.ColoredLogger{Info: logInfo, ColorAttribute: color.FgBlue}
logger.Println("This is a log message")
logger.Debug("This is a debug log message")
logger.Debugf("This is a formatted debug log message: %s", "Debug details")
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 defines the methods for a generic logging interface. It consists of Println, Printf and Error methods.
func ConfigureLogger ¶ added in v2.0.6
ConfigureLogger creates a logger based on the provided level. Depending on the level, it returns a colored or plain logger.
**Parameters:**
level: Logging level as a slog.Level. path: Path to the log file.
**Returns:**
Logger: Logger object based on provided level. 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"
)
func main() {
logger, err := logging.ConfigureLogger(slog.LevelDebug, "/tmp/test.log")
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")
// Since we can't predict the log message, print a static message instead.
fmt.Println("Logger configured successfully.")
// Unpredictable output due to timestamps and structured logging
}
Output:
type PlainLogger ¶ added in v2.0.6
type PlainLogger struct {
Info LogInfo
}
PlainLogger represents a logger that outputs in plain format.
**Attributes:**
Info: LogInfo object containing information about the log file.
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 in plain text. The arguments are handled in the manner of fmt.Println.
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 in plain text. 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 in plain text. The arguments are handled in the manner of fmt.Println.
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 in plain text. 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 in plain text. 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 in plain text. The arguments are handled in the manner of fmt.Println.
type SlogLogger ¶ added in v2.0.6
SlogLogger represents a logger using the slog library.
**Attributes:**
Logger: Logger object from slog library.
func (*SlogLogger) Debug ¶ added in v2.1.1
func (l *SlogLogger) Debug(v ...interface{})
Debug for SlogLogger logs the provided arguments as a debug line using slog library. The arguments are converted to a string using fmt.Sprint.
func (*SlogLogger) Debugf ¶ added in v2.1.1
func (l *SlogLogger) Debugf(format string, v ...interface{})
Debugf for SlogLogger 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 (*SlogLogger) Error ¶ added in v2.0.7
func (l *SlogLogger) Error(v ...interface{})
Error for SlogLogger logs the provided arguments as an error line using slog library. The arguments are converted to a string using fmt.Sprint.
func (*SlogLogger) Errorf ¶ added in v2.0.7
func (l *SlogLogger) Errorf(format string, v ...interface{})
Errorf for SlogLogger 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 (*SlogLogger) Printf ¶ added in v2.0.6
func (l *SlogLogger) Printf(format string, v ...interface{})
Printf for SlogLogger logs the provided formatted string using slog library. The format and arguments are handled in the manner of fmt.Printf.
func (*SlogLogger) Println ¶ added in v2.0.6
func (l *SlogLogger) Println(v ...interface{})
Println for SlogLogger logs the provided arguments as a line using slog library. The arguments are converted to a string using fmt.Sprint.
type SlogPlainLogger ¶ added in v2.0.6
SlogPlainLogger represents a plain logger using the slog library.
**Attributes:**
Logger: Logger object from slog library.
func (*SlogPlainLogger) Debug ¶ added in v2.1.1
func (l *SlogPlainLogger) Debug(v ...interface{})
Debug for SlogPlainLogger logs the provided arguments as a debug line using slog library. The arguments are converted to a string using fmt.Sprint.
func (*SlogPlainLogger) Debugf ¶ added in v2.1.1
func (l *SlogPlainLogger) Debugf(format string, v ...interface{})
Debugf for SlogPlainLogger 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 (*SlogPlainLogger) Error ¶ added in v2.0.7
func (l *SlogPlainLogger) Error(v ...interface{})
Error for SlogPlainLogger logs the provided arguments as an error line using slog library. The arguments are converted to a string using fmt.Sprint.
func (*SlogPlainLogger) Errorf ¶ added in v2.0.7
func (l *SlogPlainLogger) Errorf(format string, v ...interface{})
Errorf for SlogPlainLogger 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 (*SlogPlainLogger) Printf ¶ added in v2.0.6
func (l *SlogPlainLogger) Printf(format string, v ...interface{})
Printf for SlogPlainLogger logs the provided formatted string using slog library. The format and arguments are handled in the manner of fmt.Printf.
func (*SlogPlainLogger) Println ¶ added in v2.0.6
func (l *SlogPlainLogger) Println(v ...interface{})
Println for SlogPlainLogger logs the provided arguments as a line using slog library. The arguments are converted to a string using fmt.Sprint.