logging

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 9 Imported by: 1

README

goutils/v2/logging

The logging package is a collection of utility functions designed to simplify common logging tasks.

Table of contents:


Functions

ColoredLogger.Error(...interface{})
Error(...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.


ColoredLogger.Errorf(string, ...interface{})
Errorf(string, ...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.


ColoredLogger.Printf(string, ...interface{})
Printf(string, ...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.


ColoredLogger.Println(...interface{})
Println(...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.


ConfigureLogger(slog.Level, string)
ConfigureLogger(slog.Level, string) Logger, error

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.


CreateLogFile(afero.Fs, string, string)
CreateLogFile(afero.Fs, string, string) LogInfo, error

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.


PlainLogger.Error(...interface{})
Error(...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.


PlainLogger.Errorf(string, ...interface{})
Errorf(string, ...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.


PlainLogger.Printf(string, ...interface{})
Printf(string, ...interface{})

Printf for PlainLogger logs the provided formatted string in plain text. The format and arguments are handled in the manner of fmt.Printf.


PlainLogger.Println(...interface{})
Println(...interface{})

Println for PlainLogger logs the provided arguments as a line in plain text. The arguments are handled in the manner of fmt.Println.


SlogLogger.Error(...interface{})
Error(...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.


SlogLogger.Errorf(string, ...interface{})
Errorf(string, ...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.


SlogLogger.Printf(string, ...interface{})
Printf(string, ...interface{})

Printf for SlogLogger logs the provided formatted string using slog library. The format and arguments are handled in the manner of fmt.Printf.


SlogLogger.Println(...interface{})
Println(...interface{})

Println for SlogLogger logs the provided arguments as a line using slog library. The arguments are converted to a string using fmt.Sprint.


SlogPlainLogger.Error(...interface{})
Error(...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.


SlogPlainLogger.Errorf(string, ...interface{})
Errorf(string, ...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.


SlogPlainLogger.Printf(string, ...interface{})
Printf(string, ...interface{})

Printf for SlogPlainLogger logs the provided formatted string using slog library. The format and arguments are handled in the manner of fmt.Printf.


SlogPlainLogger.Println(...interface{})
Println(...interface{})

Println for SlogPlainLogger logs the provided arguments as a line using slog library. The arguments are converted to a string using fmt.Sprint.


Installation

To use the goutils/v2/logging package, you first need to install it. Follow the steps below to install via go get.

go get github.com/l50/goutils/v2/logging

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/l50/goutils/v2/logging"

Tests

To ensure the package is working correctly, run the following command to execute the tests for goutils/v2/logging:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColoredLogger added in v2.0.6

type ColoredLogger struct {
	Info           LogInfo
	ColorAttribute color.Attribute
}

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) 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

type LogInfo struct {
	Dir      string
	File     afero.File
	FileName string
	Path     string
}

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

func CreateLogFile(fs afero.Fs, logDir string, logName string) (LogInfo, error)

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")

	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
}

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{})
}

Logger defines the methods for a generic logging interface. It consists of Println, Printf and Error methods.

func ConfigureLogger added in v2.0.6

func ConfigureLogger(level slog.Level, path string) (Logger, error)

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
}

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) 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

type SlogLogger struct {
	Logger *slog.Logger
}

SlogLogger represents a logger using the slog library.

**Attributes:**

Logger: Logger object from slog library.

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

type SlogPlainLogger struct {
	Logger *slog.Logger
}

SlogPlainLogger represents a plain logger using the slog library.

**Attributes:**

Logger: Logger object from slog library.

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.

Jump to

Keyboard shortcuts

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