welog

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 10, 2023 License: MIT Imports: 5 Imported by: 0

README

Welog

build license Go version GoDoc

A simple and colorful logging module for Go. It allows developers to easily log messages at different log levels, including Debug, Info, Warn, and Error.

Install

To install the module, run the following command:

go get github.com/iamando/welog

Usage

To use the module in your Go program, import it using the following code:

import "github.com/iamando/welog"

Then, create a logger using the GenerateLogger function:

logger := welog.GenerateLoggerBuilder().Build()

You can customize the logger by chaining one or more options:

logger := welog.GenerateLoggerBuilder()
  .WithLogLevel(welog.Info), // By default `Info`
  .WithLogMode(welog.Colorful), // By default `Basic`
  .WithLogFile(true), // By default `false`, to give access create and write log file for errors
  .Build()

Options

WithLogLevel

Set the log level for the logger. Available levels are Debug, Info, Warn, and Error.

WithLogMode

Set the log mode for the logger. Available modes are Basic and Colorful.

WithLogFile

Set whether to write logs to a file in addition to the console. If true, a file called welog-errors.log will be created in the current directory and error logs will be written to it.

Log

Once you have a logger, you can use its methods to log messages at different levels:

logger.Debug("Debug message")
logger.Info("Info message")
logger.Warn("Warn message")
logger.Error("Error message")

By default, logs will be written to standard error in the following format:

[<timestamp>] [<level>] <message>

Where <timestamp> is the current time in the 2006-01-02 15:04:05 format, <level> is the log level, and <message> is the message to be logged.

Colorized output

If the log mode is set to Color, the log level will be colorized in the console output:

  • Debug: white
  • Info: green
  • Warn: yellow
  • Error: red

Customization

You can customize the output format of the logs by passing a formatting function to the SetFormatter method of a logger:

logger.SetFormatter(func(level LogLevel, message string) string {
  return fmt.Sprintf("%s [%s] %s", level, time.Now().Format("2006-01-02"), message)
})

Example

package main

import "github.com/iamando/welog"


func main() {
  logger := welog.GenerateLogger()

  // log some messages
  logger.Error("An error occurred")
  logger.Warn("A warning occurred")
  logger.Info("An informational message")
  logger.Debug("A debug message")
}

Testing

go test

Support

Welog is an MIT-licensed open source project. It can grow thanks to the sponsors and support.

License

Welog is MIT licensed.

Documentation

Index

Constants

View Source
const TimestampFormat string = "2006-01-02 15:04:05"

Variables

This section is empty.

Functions

This section is empty.

Types

type ColorFormatter

type ColorFormatter struct{}

type LogLevel

type LogLevel int
const (
	Error LogLevel = iota
	Warn
	Info
	Debug
)

type LogMode

type LogMode string
const (
	Basic    LogMode = "basic"
	Colorful LogMode = "colorful"
)

type LogWrite

type LogWrite bool

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func (*Logger) Debug

func (l *Logger) Debug(message string)

func (*Logger) Error

func (l *Logger) Error(message string)

func (*Logger) Info

func (l *Logger) Info(message string)

func (*Logger) Log

func (logger *Logger) Log(level LogLevel, message string)

func (*Logger) SetFormatter

func (logger *Logger) SetFormatter(formatter func(LogLevel, string) string)

func (*Logger) SetOutput

func (logger *Logger) SetOutput(writer io.Writer)

func (*Logger) Warn

func (l *Logger) Warn(message string)

type LoggerBuilder added in v1.1.0

type LoggerBuilder struct {
	// contains filtered or unexported fields
}

func GenerateLoggerBuilder added in v1.1.0

func GenerateLoggerBuilder() *LoggerBuilder

func (*LoggerBuilder) Build added in v1.1.0

func (builder *LoggerBuilder) Build() *Logger

func (*LoggerBuilder) WithLogFile added in v1.1.0

func (builder *LoggerBuilder) WithLogFile(hasLogFile LogWrite) *LoggerBuilder

func (*LoggerBuilder) WithLogLevel added in v1.1.0

func (builder *LoggerBuilder) WithLogLevel(level LogLevel) *LoggerBuilder

func (*LoggerBuilder) WithLogMode added in v1.1.0

func (builder *LoggerBuilder) WithLogMode(mode LogMode) *LoggerBuilder

Jump to

Keyboard shortcuts

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