Log manager with customizable type, tag and time format.
Logger has two requirements you must meet before start using logger.
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
Messages generated and printed to output using log message driver.
// 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)
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("")
Set log message type.
// Signature:
Type(t string) Log
// Example:
myLog.Type("Error")
Set log message tags.
// Signature:
Tags(tags ...string) Log
// Example:
myLog.Tags("Server", "Exception", "SQL")
Format and write message to writers. this function use standard go fmt.Sprintf
signature.
// Signature:
Print(format string, params ...any) error
Logger is main driver for managing logs.
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)
Logger interface contains following methods:
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("")
Generate new log message with "Error"
type.
// Signature:
Error() Log
// Example:
err := lgr.Error().Tags("A", "B").Print("")
Generate new log message with "Warning"
type.
// Signature:
Warning() Log
// Example:
err := lgr.Warning().Tags("A", "B").Print("")
Print new divider message with title and length.
// Signature:
Divider(divider string, count uint8, title string) error
// Example:
err := lgr.Divider("=", 50, "SQL LOG")
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})
Write pretty json formatted data to output.
// Signature:
JSON(data any) error
// Example:
err := lgr.JSON(user)
Add new writer to logger.
// Signature:
AddWriter(name string, writer io.Writer)
// Example:
lgr.AddWriter("console", os.Stdout)
Remove writer from logger.
// Signature:
RemoveWriter(name string)
// Example:
lgr.RemoveWriter("console")
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.
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