logger

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: Apache-2.0 Imports: 3 Imported by: 1

README

Go Logger Package

GitHub Tag License buyMeACoffee GitHub issues GitHub stars

A lightweight and customizable logging package for Go. This package provides support for different log levels, output streams, formatted logs, and error handling, making it easy to integrate logging into your application.

Features

  • Support for log levels: DEBUG, INFO, WARNING, ERROR, FATAL, OFF
  • Customizable log message and error prefixes
  • Separate output writers for standard logs and errors
  • Easy-to-use functions for logging formatted messages
  • Error checking with built-in logging for ERROR and FATAL levels
  • Fatal logs that exit the program

Installation

go get github.com/cyrus2281/go-logger

Usage

Using the Default Logger

The default logger writes to os.Stdout and os.Stderr and has the log level set to INFO. All of which can be customized using the SetOutputWriters, SetLogLevel, and SetPrefixFormatters methods.

package main

import (
	"os"
	"github.com/cyrus2281/go-logger/logger"
)

func main() {
	// Logging examples
	logger.Infoln("This is an info message")
	logger.Warningln("This is a warning message")
	logger.Errorln("This is an error message")
}
Basic Logger Setup

You can create a new logger using the NewLogger function by specifying the log level and output writers:

package main

import (
	"os"
	"github.com/cyrus2281/go-logger/logger"
)

func main() {
	// Create a logger with INFO level
	log := logger.NewLogger(logger.INFO, os.Stdout, os.Stderr)

	// Logging examples
	log.Infoln("This is an info message")
	log.Warningln("This is a warning message")
	log.Errorln("This is an error message")
}
Customizing Log Prefixes

You can set prefixes for standard log messages and error messages:

func formatter(level int) string {
    currentDate := time.Now().Format("2006-01-02")
	return fmt.Sprintf("[%s] [%d] ", currentDate, level)
}

log.SetPrefixFormatter(formatter)
log.SetErrorPrefixFormatter(formatter)

log.Infoln("Application started")
log.Errorln("Failed to connect to database")
Log Levels

The package supports the following log levels, allowing you to filter messages based on importance:

  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • FATAL
  • OFF
log.SetLogLevel(logger.WARNING)

log.Debugln("This will not be logged")
log.Infoln("This will not be logged")
log.Warningln("This will be logged")
log.Errorln("This will be logged")
Formatted Logs

You can log formatted messages using the DebugF, InfoF, WarningF, ErrorF, and FatalF methods.

log.InfoF("User %s logged in at %s", "john_doe", "10:00 AM")
log.ErrorF("Failed to load file: %s", "config.yaml")
Error Checking

The logger provides utility methods for checking errors and logging them automatically:

err := errors.New("connection failed")
log.CheckError(err)  // Logs if `err` is not nil

fatalErr := errors.New("fatal error")
log.CheckFatal(fatalErr)  // Logs and exits the program if `fatalErr` is not nil

Log Level Behavior

The log level controls which messages will be printed based on their importance:

Log Level Logs Debug Logs Info Logs Warning Logs Error Logs Fatal
DEBUG
INFO
WARNING
ERROR
FATAL
OFF

Advanced Usage

Custom Output Writers

You can direct standard and error logs to different output writers (e.g., files, network streams, etc.).

var outBuf, errBuf bytes.Buffer

log.SetOutputWriters(&outBuf, &errBuf)

log.Infoln("This will be written to outBuf")
log.Errorln("This will be written to errBuf")

Contributing

Contributions are welcome! Feel free to submit issues or pull requests to help improve this package.

Running Tests

To run the tests for this package, use:

go test ./...
License

This project is licensed under the Apache v2.0 License - see the LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	DEBUG = iota
	INFO
	WARNING
	ERROR
	FATAL
	OFF
)

Variables

This section is empty.

Functions

func CheckError

func CheckError(err error)

CheckError checks error and logs with ERROR level to error output if not nil

func CheckErrorF

func CheckErrorF(err error, format string, message ...any)

CheckErrorF checks error and logs formatted message with ERROR level to error output if not nil

func CheckErrorln

func CheckErrorln(err error)

CheckErrorln checks error and logs with ERROR level to error output if not nil and a newline

func CheckFatal

func CheckFatal(err error)

CheckFatal checks error and logs with FATAL level to error output and exits with 1 if not nil

func CheckFatalF

func CheckFatalF(err error, format string, message ...any)

CheckFatalF checks error and logs formatted message with FATAL level to error output and exits with 1 if not nil

func CheckFatalln

func CheckFatalln(err error)

CheckFatalln checks error and logs with FATAL level to error output and exits with 1 if not nil and a newline

func Debug

func Debug(message ...any)

Debug logs with DEBUG level

func DebugF

func DebugF(format string, message ...any)

DebugF logs formatted message with DEBUG level

func Debugln

func Debugln(message ...any)

Debugln logs with DEBUG level and a newline

func Error

func Error(message ...any)

Error logs with ERROR level to error output

func ErrorF

func ErrorF(format string, message ...any)

ErrorF logs formatted message with ERROR level to error output

func Errorln

func Errorln(message ...any)

Errorln logs with ERROR level to error output and a newline

func Fatal

func Fatal(message ...any)

Fatal logs with FATAL level to error output and exits with 1

func FatalF

func FatalF(format string, message ...any)

FatalF logs formatted message with FATAL level to error output and exits with 1

func Fatalln

func Fatalln(message ...any)

Fatalln logs with FATAL level to error output and exits with 1 and a newline

func GetLogLevel

func GetLogLevel() int

GetLogLevel gets the log level

func Info

func Info(message ...any)

Info logs with INFO level

func InfoF

func InfoF(format string, message ...any)

InfoF logs formatted message with INFO level

func Infoln

func Infoln(message ...any)

Infoln logs with INFO level and a newline

func NewDefaultLogger

func NewDefaultLogger() logger

NewDefaultLogger creates a new logger instance with default log level, standard output and error output

func NewLogger

func NewLogger(level int, outputWriter io.Writer, errOutputWriter io.Writer) logger

NewLogger creates a new logger instance with the specified log level, output writer and error output writer

func SetErrorOutputWriter

func SetErrorOutputWriter(writer io.Writer)

SetErrorOutputWriter sets the output writer for error output

func SetErrorPrefixFormatter added in v1.1.0

func SetErrorPrefixFormatter(errorPrefixFormatter func(level int) string)

SetErrorPrefixFormatter sets the prefix for the error log message

func SetLogLevel

func SetLogLevel(level int)

SetLogLevel sets the log level

func SetOutputWriter

func SetOutputWriter(writer io.Writer)

SetOutputWriter sets the output writer for standard output

func SetOutputWriters

func SetOutputWriters(writer io.Writer, errWriter io.Writer)

SetOutputWriters sets the output writers for standard output and error output

func SetPrefixFormatter added in v1.1.0

func SetPrefixFormatter(prefixFormatter func(level int) string)

SetPrefixFormatter sets the prefix formatter for the log message

func SetPrefixFormatters added in v1.1.0

func SetPrefixFormatters(prefixFormatter func(level int) string, errorPrefixFormatter func(level int) string)

SetPrefixFormatters sets the prefix formatters for the log message and the error log message

func Warning

func Warning(message ...any)

Warning logs with WARNING level

func WarningF

func WarningF(format string, message ...any)

WarningF logs formatted message with WARNING level

func Warningln

func Warningln(message ...any)

Warningln logs with WARNING level and a newline

Types

This section is empty.

Jump to

Keyboard shortcuts

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