logger

package module
v0.0.0-...-77ecf91 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 10 Imported by: 24

README

logger GO library

Install
import:
- package: github.com/best-expendables/logger
  version: x.x.x
Rules:

Message is truncated at 128 characters

Level table

Priority #1 #2 #3 #4 #5 #6 #7 #8
Level emerg alert crit err warning notice info debug

There are 8 Levels to provide to LoggerProvider

EmergencyLevel
AlertLevel
CriticalLevel
ErrorLevel
WarningLevel
NoticeLevel
InfoLevel
DebugLevel
Examples:
With content field
    
	package main
    
    import (
        "github.com/best-expendables/logger"
    	"context"
    )
    
    func main() {
        // Create loggerFactory with level Error above
        loggerFactory := logger.NewLoggerFactory(logger.ErrorLevel)

        // fake context,
        // in real cases, it should be current app's context
        ctx := context.TODO()
    
        // withField will be filled into "content" field
        loggerFactory.Logger(ctx).WithField("test key", "test").Alert("Test")
    
        // simple log, no content field
        loggerFactory.Logger(ctx).Debug("Testing")
        loggerFactory.Logger(ctx).Critical("Testing")
        loggerFactory.Logger(ctx).Error("Testing")
        loggerFactory.Logger(ctx).Warning("Testing")
        loggerFactory.Logger(ctx).Notice("Testing")
    }

Without content field
     package main
     
     import (
     	"github.com/best-expendables/logger"
     	"context"
     )
     
     func main() {
        // Create loggerFactory with level Error above
        loggerFactory := logger.NewLoggerFactory(logger.ErrorLevel)

        
        // fake context,
        // in real cases, it should be current app's context
        ctx := context.TODO()
    
        // simple log, no content field
        loggerFactory.Logger(ctx).Debug("Testing")
        loggerFactory.Logger(ctx).Critical("Testing")
        loggerFactory.Logger(ctx).Error("Testing")
        loggerFactory.Logger(ctx).Warning("Testing")
        loggerFactory.Logger(ctx).Notice("Testing")
     }

simple log without any fields
    
	package main
    
    import (
    	"github.com/best-expendables/logger"
    )
    
    func main() {
    	logger.Info("Test log")
    }

Documentation

Index

Constants

View Source
const (
	// FormatVersion log format version
	FormatVersion uint8 = 2
	// MessageLength max length of message
	MessageLength = 128
)
View Source
const (
	FieldVersion   = "@version"
	FieldTimestamp = "@timestamp"
	FieldLevel     = "level"
	FieldContent   = "content"
	FieldMessage   = "message"
	FieldUserId    = "userId"
	FieldTraceId   = "traceId"
	FieldRequestId = "context-id"
)

Variables

This section is empty.

Functions

func Alert

func Alert(args ...interface{})

func Alertf

func Alertf(format string, args ...interface{})

func ContextWithEntry

func ContextWithEntry(e Entry, ctx context.Context) context.Context

ContextWithEntry set entry to context

func Critical

func Critical(args ...interface{})

func Criticalf

func Criticalf(format string, args ...interface{})

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(format string, args ...interface{})

func Emergency

func Emergency(args ...interface{})

func Emergencyf

func Emergencyf(format string, args ...interface{})

func Error

func Error(args ...interface{})

func Errorf

func Errorf(format string, args ...interface{})

func Info

func Info(args ...interface{})

func Infof

func Infof(format string, args ...interface{})

func NewLoggerFactory

func NewLoggerFactory(level Level, opts ...opt) *factory

NewLoggerProvider returns logger provider

func Notice

func Notice(args ...interface{})

func Noticef

func Noticef(format string, args ...interface{})

func SetDefaultEntry

func SetDefaultEntry(entry Entry)

SetDefaultEntry

func SetOut

func SetOut(out io.Writer) opt

SetOut output log writer

func Warning

func Warning(args ...interface{})

func Warningf

func Warningf(format string, args ...interface{})

Types

type Entry

type Entry interface {
	Info(args ...interface{})
	Debug(args ...interface{})
	Notice(args ...interface{})
	Warning(args ...interface{})
	Alert(args ...interface{})
	Error(args ...interface{})
	Emergency(args ...interface{})
	Critical(args ...interface{})

	Infof(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Noticef(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Alertf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Emergencyf(format string, args ...interface{})
	Criticalf(format string, args ...interface{})

	WithField(key string, value interface{}) Entry
	WithFields(fields Fields) Entry
	GetFields() Fields
}

func EntryFromContext

func EntryFromContext(ctx context.Context) Entry

EntryFromContext get entry from context

func EntryFromContextOrDefault

func EntryFromContextOrDefault(ctx context.Context) Entry

EntryFromContextOrDefault returns the logger from context if not nil, otherwise, returns default entry

func WithFields

func WithFields(fields Fields) Entry

WithFields returns copy of default entry with fields

type Factory

type Factory interface {
	Logger(ctx context.Context) Entry
}

type Fields

type Fields map[string]interface{}

type JsonFormatter

type JsonFormatter struct{}

func (JsonFormatter) Format

func (JsonFormatter) Format(entry *logrus.Entry) ([]byte, error)

type Level

type Level uint32
const (
	EmergencyLevel Level = iota
	AlertLevel
	CriticalLevel
	ErrorLevel
	WarningLevel
	NoticeLevel
	InfoLevel
	DebugLevel
)

func (Level) String

func (lvl Level) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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