lllog

package module
v0.0.0-...-45ddc23 Latest Latest
Warning

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

Go to latest
Published: May 3, 2019 License: MIT Imports: 7 Imported by: 0

README

lllog Build Status codecov

Simple and powerful logger for Golang.

example screen

lllog offers

  • different color based on the log level (using fatih/color library) + color customization
  • custom date and time format to timestamp your logs
  • format strings support
  • logging to file
  • json formatting
  • goroutines safety thanks to sync.Mutex
Basic logging

Creating a logger is freaking easy. Every logger requires a name to make easier to identify different loggers

logger := lllog.New("Name")

From now on the logging process is extremely intuitive

// Notice how the log ends with a \n char, since it is not appended by default
logger.Log("Normal log message\n")
logger.Warn("Different log levels are provided\n")
logger.Err("Nothing very special to say\n")
logger.Fatal("AAAAAAAAAAAAAA\n")

// lllog supports format strings
a := 2
b := 3
logger.Log("The result of a * b is %d\n", a * b)
logger.Log("This is %s!\n", "awesome")
Timestamps

time screen

lllog can add a time and date header, you just need to add a go time format

logger.SetFormat("02 January 2006 - 15:04")
Custom color scheme

Since each of us has different taste in color you can edit the color for each log level. (You clearly must choose a color.Attribute from the fatih/color library)

logger.SetErrColor(color.FgHiGreen)
Log to file

You can decide to log to a file. Unfortunately you cannot specify the file name, which is in the yyyy-mm-dd.log format, but you can specify the path

logger.LogToFile("logs")

Sometimes, if you are logging to a file, it could be useful not to log to the console too. You can enable and disable console logging with

logger.WriteToConsole(false)
Log formatter

When logging to a file you can decide to use two different formatters. The default one is the console formatter, which prints the exact copy of what you see in the console. lllog also features a json formatter which gives you an output like the following, easy and nice to be read by some other program:

{"Level":"Fatal","Msg":"This is sick!\n","Timestamp":1542384480,"Format":"02 January 2006 - 15:04","Name":"File logger"}

To change the log formatter:

logger.SetOutputFormatter(lllog.ConsoleFormatter) // default formatter
logger.SetOutputFormatter(lllog.JSONFormatter)    // json formatter

Every log function also returns a string with the log outputted by the chosen log formatter, if you need it for something

log := logger.Info("This is %s and returned\n", "evaluated")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConsoleFromatter

func ConsoleFromatter(log *LogMessage) string

func JSONFormatter

func JSONFormatter(log *LogMessage) string

Types

type LogMessage

type LogMessage struct {
	Level     string
	Msg       string
	Timestamp int64
	Format    string
	Name      string
}

type Logger

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

func New

func New(name string) *Logger

func (*Logger) Err

func (l *Logger) Err(format string, args ...interface{}) string

func (*Logger) Fatal

func (l *Logger) Fatal(format string, args ...interface{}) string

func (*Logger) Log

func (l *Logger) Log(format string, args ...interface{}) string

func (*Logger) LogToFile

func (l *Logger) LogToFile(path string)

func (*Logger) SetErrColor

func (l *Logger) SetErrColor(c color.Attribute)

func (*Logger) SetFatalColor

func (l *Logger) SetFatalColor(c color.Attribute)

func (*Logger) SetFormat

func (l *Logger) SetFormat(format string)

func (*Logger) SetLogColor

func (l *Logger) SetLogColor(c color.Attribute)

func (*Logger) SetOutputFormatter

func (l *Logger) SetOutputFormatter(f func(*LogMessage) string)

func (*Logger) SetWarnColor

func (l *Logger) SetWarnColor(c color.Attribute)

func (*Logger) Warn

func (l *Logger) Warn(format string, args ...interface{}) string

func (*Logger) WriteToConsole

func (l *Logger) WriteToConsole(enabled bool)

Jump to

Keyboard shortcuts

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