log

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: GPL-3.0 Imports: 10 Imported by: 3

README

Logger

Helps your implementation to track your code activity

Why?

  • four levels debug, information, warning and error
  • tags that help messages be organized into logical groups
  • colored human readable output text
  • structural json output for machines, ie on production environment
  • traceable will add info about file name, location, function and line number of called log
  • implements io.Writer in order to implement your own logger or just hide dependencies for external logging
  • uses Printf(format string, args ...any) function idiom, well know in go ecosystem

How to use it?

just download it to your source code

go get github.com/sokool/log

and then run it with that sample snippet

import "github.com/sokool/log"

lgr := log.
    New(os.Stdout). // output is written to standard output
    Tag("foo")      // all messages are marked with tag name
	
lgr.Printf("hello %s", "world")
lgr.Printf("second:dbg: with tag name and as debug")
lgr.Printf("third:err: with tage name and explicit error level %v", log.Data{"message": "it's bad", "code": 859})
lgr.Printf("fourth:wrn: just warning level level")

lgr = lgr.
    Tag("bar").                                                              // all messages are marked with bar tag name
    Options(log.Levels | log.Tags | log.Time | log.Properties | log.Colors). // colored text with tags and levels
    Verbosity(log.WARNING)                                                   // do not show DBG and INF logs

lgr.Infof("ignored")
lgr.Debugf("ignored")
lgr.Errorf("with list of %v attributes", map[string]any{"code": 404, "message": "not found"})
lgr.Write([]byte("wrn: by io.Writer method"))

lgr = lgr.
    Tag("ror:nik").
    Options(log.JSON). //
    Verbosity(log.DEBUG)

lgr.Printf("with %v messages %v", log.Data{"wat": "hi"}, log.Data{"qot": "there"})

and here is an output with Options such as Date, Time, Tags, Levels, Trace and JSON

kaD03.png

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = New(os.Stdout, All)

Functions

func Debugf

func Debugf(format string, args ...any)

func Errorf

func Errorf(format string, args ...any)

func Printf

func Printf(format string, args ...any)

func Warnf added in v1.0.6

func Warnf(format string, args ...any)

Types

type Data added in v1.0.7

type Data map[string]any

type Handler

type Handler func(Message)

func HTTP

func HTTP(url string, frequency time.Duration) Handler

type Level added in v1.0.6

type Level int
const (
	DEBUG   Level = 4
	INFO    Level = 3
	WARNING Level = 2
	ERROR   Level = 1
)

func (Level) GoString added in v1.0.6

func (l Level) GoString() string

func (Level) Render added in v1.0.6

func (l Level) Render(short, color bool) string

func (Level) String added in v1.0.6

func (l Level) String() string

type Logger

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

Logger support three types(levels) of logging

INF - default, when you want emphasize that something important (not negative) has happened. It should be used for informing about rare situation in your code, such as database connection has been established.

DBG - it meant to be verbose, like every few lines of code, when you decide that part of your code implementation did something important for internal state of your library/code.

ERR - when your code is a place where error is received but there is no good way of handling that situation you might log it todo

  • default attributes data added to each Message
  • log.Format option

func New

func New(w io.Writer, o ...Option) *Logger

New instance of logger

io.Writer w will receive all messages that are generated internally from strings and arguments that are passed to Logger.Printf.

Option o represents what is going to be included in strings that are passed into io.Writer

func (*Logger) Debugf added in v1.0.4

func (l *Logger) Debugf(text string, args ...any)

func (*Logger) Errorf added in v1.0.4

func (l *Logger) Errorf(text string, args ...any)

func (*Logger) Handlers

func (l *Logger) Handlers(h ...Handler) *Logger

Handlers creates new instance of Logger and all Message's are passed into Handler just after it is writer to io.Writer

func (*Logger) Infof added in v1.0.4

func (l *Logger) Infof(text string, args ...any)

func (*Logger) Options

func (l *Logger) Options(o Option) *Logger

Options create new Logger instance

func (*Logger) Printf

func (l *Logger) Printf(text string, args ...any)

Printf when text is json format and has no arguments a then it will be transformed

func (*Logger) Tag

func (l *Logger) Tag(name string) *Logger

Tags creates new instance of Logger with predefined tag name

func (*Logger) Trace added in v1.0.5

func (l *Logger) Trace(depth int) *Logger

Trace create new Logger instance

func (*Logger) Verbosity added in v1.0.7

func (l *Logger) Verbosity(m Level) *Logger

Verbosity determines what Level of logging should be delivered to io.Writer

func (*Logger) Warnf added in v1.0.6

func (l *Logger) Warnf(text string, args ...any)

func (*Logger) Write

func (l *Logger) Write(p []byte) (n int, err error)

Write tbd

type Message

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

func NewMessage

func NewMessage(text string, deep int, args ...any) Message

func (Message) CreatedAt

func (m Message) CreatedAt() time.Time

func (Message) Fields added in v1.0.4

func (m Message) Fields() Data

func (Message) Location

func (m Message) Location(colors bool) string

func (Message) MarshalJSON

func (m Message) MarshalJSON() ([]byte, error)

func (Message) MarshalText added in v1.0.4

func (m Message) MarshalText() ([]byte, error)

func (Message) Render

func (m Message) Render(o Option) ([]byte, error)

func (Message) String

func (m Message) String() string

func (Message) Tag

func (m Message) Tag(colors bool) string

func (Message) Text

func (m Message) Text(colors, properties bool) string

func (Message) Type

func (m Message) Type(colors bool) string

type Option

type Option int64

Option ...

const (
	// Date render Message with date in 2006/01/02 format
	Date Option = 1 << iota

	// Time render Message with time in 15:04:05.000000 format
	Time

	// Levels render Message with one of INF, DBG, ERR strings
	Levels

	// Tags render Message with tag name
	Tags

	// Trace render Message with filename and line number
	Trace

	// Properties renders %v data in a Message output string
	Properties

	// Colors render Message with colors
	Colors

	// JSON makes output with json format instead text
	JSON

	All = Date | Time | Levels | Tags | Trace | Properties | Colors
)

Jump to

Keyboard shortcuts

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