logger

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package logger provides a logging mechanism for the application. The logging mechanism writes to a log.txt file in the root folder of the application.

Index

Constants

View Source
const (
	TraceLevel = iota
	DebugLevel
	InfoLevel
	WarningLevel
	ErrorLevel
	FatalLevel
)

Constants representing the log levels for the logger. These constants are used to set the LogLevel and LogLevelSpecific fields of the CustomLogger struct. TraceLevel represents the lowest level of logs, used for detailed information about program execution. DebugLevel is used for information that may be helpful in diagnosing problems. InfoLevel is for general operational information about the program. WarningLevel is for information about events that may indicate a problem. ErrorLevel is for reporting errors that occur during program execution. FatalLevel is for reporting severe errors that may prevent program execution.

Variables

This section is empty.

Functions

func Setup

func Setup(fileName string, logLevel int, logLevelSpecific int)

Setup initializes a new logger for the runtime of the application

This function is used to set up the logger with a specific log level and a specific log level filter.

Parameters:

fileName string - The name of the log file to write to.

logLevel int - The log level to log up to. This should be a value between 0 (TraceLevel) and 5 (FatalLevel).

logLevelSpecific int - The specific log level to log. This should be a value between 0 (TraceLevel) and 5 (FatalLevel), or -1 to log all levels up to the specified log level.

Returns: None

func SetupTests

func SetupTests()

SetupTests initializes a logger for the runtime of the tests.

This function is used to set up the logger with a specific log level and a specific log level filter. The log level is set to 0 (TraceLevel) and the log level specific is set to -1, meaning all log levels will be logged. This logger does not write to a file, but simply writes to standard output.

Parameters: None

Returns: None

Types

type CustomLogger

type CustomLogger struct {
	*log.Logger
	LogLevel         int
	LogLevelSpecific int
}

CustomLogger is a custom logger struct that wraps the standard logger. It implements 6 error levels: Trace, Debug, Info, Warning, Error, and Fatal. LogLevel determines the maximum level of messages that will be logged. LogLevelSpecific determines the specific level of messages that will be logged. If LogLevelSpecific is set to a log-level value, then only that log-level will be written to the log file. If LogLevelSpecific is set to -1, then all log-levels up to the specified LogLevel will be written to the log file.

var (
	Log *CustomLogger
)

func NewCustomLogger

func NewCustomLogger(test bool, fileName string, logLevel int, logLevelSpecific int) *CustomLogger

NewCustomLogger creates a new CustomLogger struct.

This function is used to create a new CustomLogger with a specific log level and a specific log level filter. The test parameter determines whether the logger should write to a file or to standard output. During testing, we wish for the logger to write to standard output.

Parameters:

test bool - A boolean value that specifies whether the logger is used for testing.

fileName string - The name of the log file to write to.

logLevel int - The log level to log up to. This should be a value between 0 (TraceLevel) and 5 (FatalLevel).

logLevelSpecific int - The specific log level to log. This should be a value between 0 (TraceLevel) and 5 (FatalLevel), or -1 to log all levels up to the specified log level.

Returns: a pointer to a new CustomLogger struct

func (*CustomLogger) Debug

func (l *CustomLogger) Debug(message string)

Debug writes a debug level message to the log file.

The message will only be logged if the LogLevelSpecific of the logger is set to DebugLevel, or if the LogLevelSpecific is set to -1 and the LogLevel is less than or equal to DebugLevel.

Parameters:

message string - The message to write to the log file.

Returns: None

func (*CustomLogger) Error

func (l *CustomLogger) Error(message string)

Error writes an error level message to the log file.

The message will only be logged if the LogLevelSpecific of the logger is set to ErrorLevel, or if the LogLevelSpecific is set to -1 and the LogLevel is less than or equal to ErrorLevel.

Parameters:

message string - The message to write to the log file.

Returns: None

func (*CustomLogger) ErrorWithErr

func (l *CustomLogger) ErrorWithErr(message string, err error)

ErrorWithErr writes an error level message to the log file, including the error variable.

The message and the error variable will only be logged if the LogLevelSpecific of the logger is set to ErrorLevel, or if the LogLevelSpecific is set to -1 and the LogLevel is less than or equal to ErrorLevel.

Parameters:

message string - The message to write to the log file.

err error - The error variable to write to the log file.

Returns: None

func (*CustomLogger) Fatal

func (l *CustomLogger) Fatal(message string)

Fatal writes a fatal level message to the log file, including the error variable.

The message and the error variable will only be logged if the LogLevelSpecific of the logger is set to FatalLevel, or if the LogLevelSpecific is set to -1 and the LogLevel is less than or equal to FatalLevel.

Parameters:

message string - The message to write to the log file.

Returns: None

func (*CustomLogger) FatalWithErr

func (l *CustomLogger) FatalWithErr(message string, err error)

FatalWithErr writes a fatal level message to the log file, including the error variable.

The message and the error variable will only be logged if the LogLevelSpecific of the logger is set to FatalLevel, or if the LogLevelSpecific is set to -1 and the LogLevel is less than or equal to FatalLevel.

Parameters:

message string - The message to write to the log file.

err error - The error variable to write to the log file.

Returns: None

func (*CustomLogger) Info

func (l *CustomLogger) Info(message string)

Info writes an info level message to the log file.

The message will only be logged if the LogLevelSpecific of the logger is set to InfoLevel, or if the LogLevelSpecific is set to -1 and the LogLevel is less than or equal to Info.

Parameters:

message string - The message to write to the log file.

Returns: None

func (*CustomLogger) Print

func (l *CustomLogger) Print(message string)

Print writes a general message to the log file.

This method is used to write a message to the log file. The message is not associated with any specific log level.

Parameters:

message string - The message to write to the log file.

Returns: None

func (*CustomLogger) Trace

func (l *CustomLogger) Trace(message string)

Trace writes a trace level message to the log file.

The message will only be logged if the LogLevelSpecific of the logger is set to TraceLevel, or if the LogLevelSpecific is set to -1 and the LogLevel is less than or equal to TraceLevel.

Parameters:

message string - The message to write to the log file.

Returns: None

func (*CustomLogger) Warning

func (l *CustomLogger) Warning(message string)

Warning writes a warning level message to the log file.

The message will only be logged if the LogLevelSpecific of the logger is set to WarningLevel, or if the LogLevelSpecific is set to -1 and the LogLevel is less than or equal to WarningLevel.

Parameters:

message string - The message to write to the log file.

Returns: None

Jump to

Keyboard shortcuts

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