monolog

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: MIT Imports: 5 Imported by: 0

README

monolog

Go Version GoDoc codecov Go Report Card tests MIT license

Installation

go get github.com/go-packagist/monolog

Usage

Simple usage:

package main

import (
	"github.com/go-packagist/logger"
	"github.com/go-packagist/monolog"
	"github.com/go-packagist/monolog/handler/file"
)

func main() {
	m := monolog.NewLogger("test",
		monolog.WithHandler(
			file.NewHandler(
				"./test-file-handler.log",
				file.WithLevel(logger.Debug),
			),
		),
	)

	m.Emergency("test emergency")
	m.Alert("test alert")
	m.Critical("test critical")
	m.Error("test error")
	m.Warning("test warning")
	m.Notice("test notice")
	m.Info("test info")
	m.Debug("test debug")

	// file content:
	// [2023-04-02 01:14:01] test.EMERGENCY: test emergency
	// [2023-04-02 01:14:01] test.ALERT: test alert
	// [2023-04-02 01:14:01] test.CRITICAL: test critical
	// [2023-04-02 01:14:01] test.ERROR: test error
	// [2023-04-02 01:14:01] test.WARNING: test warning
	// [2023-04-02 01:14:01] test.NOTICE: test notice
	// [2023-04-02 01:14:01] test.INFO: test info
	// [2023-04-02 01:14:01] test.DEBUG: test debug
}

Registry usage:

package registry

import (
	"github.com/go-packagist/logger"
	"github.com/go-packagist/monolog"
	"github.com/go-packagist/monolog/handler/file"
)

func main() {
	monolog.RegisterLoggers(map[string]*monolog.Logger{
		"default": monolog.NewLogger("test",
			monolog.WithHandler(
				file.NewHandler(
					"./test-file-handler.log",
					file.WithLevel(logger.Debug),
				),
			),
		),
		"test2": monolog.NewLogger("test"),
	})

	monolog.GetLogger("default").Emergency("test emergency") // default logger
	monolog.GetLogger("test2").Critical("test critical")     // test2 logger
	monolog.GetLogger().Alert("test alert")                  // default logger
	monolog.Alert("test alert")                              // default logger
	monolog.Alertf("test alert %s", "test")                  // default logger
}

License

The MIT License (MIT). Please see License File for more information.

Thanks

Monolog: I have referenced some architecture designs and made significant adjustments to implement a version in Go programming language.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alert added in v0.4.0

func Alert(message string)

func Alertf added in v0.4.0

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

func Close added in v0.6.0

func Close() error

func Critical added in v0.4.0

func Critical(message string)

func Criticalf added in v0.4.0

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

func Debug added in v0.4.0

func Debug(message string)

func Debugf added in v0.4.0

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

func Emergency added in v0.4.0

func Emergency(message string)

func Emergencyf added in v0.4.0

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

func Error added in v0.4.0

func Error(message string)

func Errorf added in v0.4.0

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

func GetLoggers added in v0.4.0

func GetLoggers() map[string]*Logger

func Info added in v0.4.0

func Info(message string)

func Infof added in v0.4.0

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

func Notice added in v0.4.0

func Notice(message string)

func Noticef added in v0.4.0

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

func RegisterLogger added in v0.4.0

func RegisterLogger(name string, logger *Logger)

func RegisterLoggers added in v0.4.0

func RegisterLoggers(loggers map[string]*Logger)

func UnregisterLogger added in v0.4.0

func UnregisterLogger(name string)

func UnregisterLoggers added in v0.4.0

func UnregisterLoggers()

func Warning added in v0.4.0

func Warning(message string)

func Warningf added in v0.4.0

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

Types

type Formatter

type Formatter interface {
	Format(record *Record) string
}

type FormatterOpt

type FormatterOpt func(Formatter)

type Formatterable

type Formatterable struct {
}

type Handler

type Handler interface {
	IsHandling(*Record) bool
	Handle(*Record) bool
	HandleBatch([]*Record) bool
	Close() error
}

Handler is the interface that all handlers must implement.

type HandlerOpt

type HandlerOpt func(Handler)

HandlerOpt is a function that can be used to configure a Handler.

type Handlerable

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

Handlerable is a struct that can be embedded in a Handler to provide

func NewHandlerable

func NewHandlerable(opts ...HandlerableOpt) *Handlerable

func (*Handlerable) Close added in v0.6.0

func (h *Handlerable) Close() error

func (*Handlerable) GetFormatter

func (h *Handlerable) GetFormatter() Formatter

func (*Handlerable) GetLevel

func (h *Handlerable) GetLevel() logger.Level

func (*Handlerable) Handle

func (h *Handlerable) Handle(*Record) bool

func (*Handlerable) HandleBatch

func (h *Handlerable) HandleBatch(records []*Record) bool

func (*Handlerable) IsHandling

func (h *Handlerable) IsHandling(record *Record) bool

func (*Handlerable) SetFormatter

func (h *Handlerable) SetFormatter(formatter Formatter)

func (*Handlerable) SetLevel

func (h *Handlerable) SetLevel(level logger.Level)

type HandlerableOpt

type HandlerableOpt func(*Handlerable)

func WithFormatter

func WithFormatter(formatter Formatter) HandlerableOpt

func WithLevel

func WithLevel(level logger.Level) HandlerableOpt

type Logger

type Logger struct {
	logger.Loggerable
	// contains filtered or unexported fields
}

func GetLogger added in v0.4.0

func GetLogger(names ...string) *Logger

func NewLogger

func NewLogger(channel string, opts ...Opt) *Logger

func (*Logger) Channel

func (l *Logger) Channel() string

func (*Logger) Close added in v0.6.0

func (l *Logger) Close() error

func (*Logger) Handlers

func (l *Logger) Handlers() []Handler

func (*Logger) Processors

func (l *Logger) Processors() []Processor

type Opt

type Opt func(*Logger)

func WithChannel

func WithChannel(channel string) Opt

func WithHandler

func WithHandler(h Handler) Opt

func WithHandlers

func WithHandlers(hs ...Handler) Opt

func WithProcessor

func WithProcessor(p Processor) Opt

func WithProcessors

func WithProcessors(ps ...Processor) Opt

func WithTimezone

func WithTimezone(tz *time.Location) Opt

type Processor

type Processor interface{}

type Record

type Record struct {
	Channel string       `json:"channel"`
	Message string       `json:"message"`
	Level   logger.Level `json:"level"`
	Time    time.Time    `json:"time"`
	Extra   interface{}  `json:"extra,omitempty"`
}

func (*Record) Marshal

func (r *Record) Marshal() ([]byte, error)

func (*Record) Unmarshal

func (r *Record) Unmarshal(data []byte) error

Directories

Path Synopsis
example
formatter
handler

Jump to

Keyboard shortcuts

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