logging

package
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2023 License: MIT Imports: 5 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

CreateLogFile
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.


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/goutils/v2/l50/logging

Usage

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

import "github.com/goutils/v2/l50/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 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/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
	}

	fmt.Printf("log file created at: %s", logInfo.Path)

	// Clean up
	err = fs.Remove(logInfo.Path)
	if err != nil {
		fmt.Printf("failed to clean up: %v", err)
	}

}
Output:
log file created at: /tmp/logs/test.log

Jump to

Keyboard shortcuts

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