logger

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 9 Imported by: 2

README

Logger

Log manager with customizable type, tag and time format.

Requirements

Logger has two requirements you must meet before start using logger.

Time Formatter

time formatter is a function that accept time and returns formatted date in locale(gregorian, jalaali, etc.). logger use formatter function for customizable date format.

By default logger contains two formatter function, gregorian and jalaali. you can write your own formatter function by implementing TimeFormatter type.

type TimeFormatter func(t time.Time, format string) string
Log Message

Messages generated and printed to output using log message driver.

Create New Log Message
// Signature:
// @param tf string log message time format
// @param f TimeFormatter formatter function
// @param writers ...io.Writer writers list
NewLog(tf string, f TimeFormatter, typ string, writers ...io.Writer) Log {

// Example
import "github.com/gomig/logger"
myLog := logger.NewLog("2006-01-02 15:04", logger.GregorianFormatter, myWriter)
Usage

Log message interface contains following methods:

Note: Log message methods return log instance as return value for use methods in chaining style. e.g.:

err := myLog.Type("Error").Tags("Server", "Exception").Print("")
Type

Set log message type.

// Signature:
Type(t string) Log

// Example:
myLog.Type("Error")
Tags

Set log message tags.

// Signature:
Tags(tags ...string) Log

// Example:
myLog.Tags("Server", "Exception", "SQL")
Print

Format and write message to writers. this function use standard go fmt.Sprintf signature.

// Signature:
Print(format string, params ...any) error

Logger

Logger is main driver for managing logs.

Create New Logger Driver

NOTE: Logger constructor parameters used as default formatting for log messages.

NOTE: You can pass multiple writer to logger.

// Signature:
NewLogger(tf string, f TimeFormatter, writers ...io.Writer) Logger

// Example:
import "github.com/gomig/logger"
lgr := logger.NewLogger("2006-01-02 15:04:05", logger.GregorianFormatter, os.Stdout)
Usage

Logger interface contains following methods:

Log

Generate new log message with "LOG" type.

// Signature:
Log() Log

// Example:
// You can change message type
err := lgr.Log().Type("INFO").Tags("A", "B").Print("")
Error

Generate new log message with "Error" type.

// Signature:
Error() Log

// Example:
err := lgr.Error().Tags("A", "B").Print("")
Warning

Generate new log message with "Warning" type.

// Signature:
Warning() Log

// Example:
err := lgr.Warning().Tags("A", "B").Print("")
Divider

Print new divider message with title and length.

// Signature:
Divider(divider string, count uint8, title string) error

// Example:
err := lgr.Divider("=", 50, "SQL LOG")
Raw

Write raw message to logger writers. this message follow fmt.Sprintf pattern.

// Signature:
Raw(format string, params ...any) error

// Example:
err := lgr.Raw("Total users count is: %d", 5120})
JSON

Write pretty json formatted data to output.

// Signature:
JSON(data any) error

// Example:
err := lgr.JSON(user)
AddWriter

Add new writer to logger.

// Signature:
AddWriter(name string, writer io.Writer)

// Example:
lgr.AddWriter("console", os.Stdout)
RemoveWriter

Remove writer from logger.

// Signature:
RemoveWriter(name string)

// Example:
lgr.RemoveWriter("console")

File Logger

File logger is a standard writer for generating and writing to time format based file names.

Note: We can use file logger as writer of logger driver to log files.

Generate File Logger

time format passed to constructor function determine how file managed (daily, monthly, yearly, etc.).

// Signature:
NewFileLogger(path string, prefix string, tf string, f TimeFormatter) io.Writer

// Example:
import "github.com/gomig/logger"
yW := logger.NewFileLogger("logs", "app", "2006", logger.GregorianFormatter) // yearly file logger
mW := logger.NewFileLogger("logs", "app", "2006-01", logger.GregorianFormatter) // monthly file logger
dW := logger.NewFileLogger("logs", "app", "2006-01-02", logger.GregorianFormatter) // daily file logger
hW := logger.NewFileLogger("logs", "app", "2006-01-02 15", logger.GregorianFormatter) // hourly file logger

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GregorianFormatter

func GregorianFormatter(t time.Time, format string) string

GregorianFormatter gregorian date formatter

func JalaaliFormatter

func JalaaliFormatter(t time.Time, format string) string

JalaaliFormatter jalaali (tehran) date formatter

func NewFileLogger

func NewFileLogger(path string, prefix string, tf string, f TimeFormatter) io.Writer

NewFileLogger create new file logger writer

Types

type Log

type Log interface {
	// Type Set message type
	Type(t string) Log
	// Tags add tags to message
	Tags(tags ...string) Log
	// Print print message to writer
	Print(format string, params ...any) error
}

Log log message interface

func NewLog

func NewLog(tf string, f TimeFormatter, typ string, writers ...io.Writer) Log

NewLog Create new log message instance

type Logger

type Logger interface {
	// Log generate new log message
	Log() Log
	// Error generate new error message
	Error() Log
	// Warning generate new warning message
	Warning() Log
	// Divider generate new divider message
	Divider(divider string, count uint8, title string) error
	// Raw write raw message to output
	Raw(format string, params ...any) error
	// JSON write pretty json formatted data to output
	JSON(data any) error
	// AddWriter add new writer to logger
	AddWriter(name string, writer io.Writer)
	// RemoveWriter remove writer from logger
	RemoveWriter(name string)
}

Logger is the interface for logger drivers.

func NewLogger

func NewLogger(tf string, f TimeFormatter) Logger

NewLogger create a new logger instance

type TimeFormatter

type TimeFormatter func(t time.Time, format string) string

TimeFormatter for log date

Jump to

Keyboard shortcuts

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