logrus

package
v1.0.0-dev.63 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 10 Imported by: 0

README

logrus.v1

Example

package main

import (
	"context"

	"github.com/xgodev/boost/wrapper/log"
	"github.com/xgodev/boost/wrapper/log/contrib/sirupsen/logrus.v1"
)

func main() {
	ctx := context.Background()

	//example use logrus
	log.Set(logrus.NewLogger())

	logger := log.WithField("main_field", "example")

	logger.Info("main method.")
	//output: INFO[2021/05/14 17:15:04.757] main method. main_field=example

	ctx = logger.ToContext(ctx)

	foo(ctx)

	withoutContext()
}

func foo(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("foo_field", "example")
	logger.Infof("%s method.", "foo")
	//output: INFO[2021/05/14 17:15:04.757] foo method. foo_field=example main_field=example

	ctx = logger.ToContext(ctx)
	bar(ctx)
}

func bar(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("bar_field", "example")

	logger.Infof("%s method.", "bar")
	//output: INFO[2021/05/14 17:15:04.757] bar method. bar_field=example foo_field=example main_field=example
}

func withoutContext() {
	log.Info("withoutContext method")
	//output: INFO[2021/05/14 17:15:04.757] withoutContext method
}

default options:

option value
Formatter text.New()
ConsoleEnabled true
ConsoleLevel "INFO"
FileEnabled false
FileLevel "INFO"
FilePath "/tmp"
FileName "application.log"
FileMaxSize 100
FileCompress true
FileMaxAge 28
TimeFormat "2006/01/02 15:04:05.000"
ErrorFieldName "err"

The package accepts a default constructor:

// default constructor
logger := logrus.NewLogger()

Or a constructor with Options:

logger := logrus.NewLoggerWithOptions(&logrus.Options{})

Or a constructor with multiple parameters using optional pattern:

// multiple optional parameters constructor
logger := logrus.NewLogger(
	logrus.WithFormatter(json.New())
	logrus.WithConsoleEnabled(true),
	logrus.WithFilePath("/tmp"),
	...
)

This is the list of all the configuration functions supported by package:

WithFormatter

sets output format of the logs. Using TEXT/JSON/CLOUDWATCH.

import (
	"github.com/xgodev/boost/wrapper/log/contrib/sirupsen/logrus.v1/formatter/text"
	"github.com/xgodev/boost/wrapper/log/contrib/sirupsen/logrus.v1/formatter/json"
	"github.com/xgodev/boost/wrapper/log/contrib/sirupsen/logrus.v1/formatter/cloudwatch"
	...
)

// text formatter
logger := logrus.NewLogger(logrus.WithFormatter(text.New()))

// json formatter
logger := logrus.NewLogger(logrus.WithFormatter(json.New()))

// cloudwatch formatter
logger := logrus.NewLogger(logrus.WithFormatter(cloudwatch.New()))
WithTimeFormat

sets the format used for marshaling timestamps.

// time format
logger := logrus.NewLogger(logrus.WithTimeFormat("2006/01/02 15:04:05.000"))
WithConsoleEnabled

sets whether the standard logger output will be in console. Accepts multi writing (console and file).

// console enable true
logger := logrus.NewLogger(logrus.WithConsoleEnabled(true))

// console enable false
logger := logrus.NewLogger(logrus.WithConsoleEnabled(false))
WithConsoleLevel

sets console logging level to any of these options below on the standard logger.

// log level DEBUG
logger := logrus.NewLogger(logrus.WithConsoleLevel("DEBUG"))

// log level WARN
logger := logrus.NewLogger(logrus.WithConsoleLevel("WARN"))

// log level FATAL
logger := logrus.NewLogger(logrus.WithConsoleLevel("FATAL"))

// log level ERROR
logger := logrus.NewLogger(logrus.WithConsoleLevel("ERROR"))

// log level TRACE
logger := logrus.NewLogger(logrus.WithConsoleLevel("TRACE"))

// log level INFO
logger := logrus.NewLogger(logrus.WithConsoleLevel("INFO"))
WithHook

sets a hook to be fired when logging on the logging levels.

import (
	"log/syslog"

	"github.com/xgodev/boost/wrapper/log"
	"github.com/xgodev/boost/wrapper/log/contrib/sirupsen/logrus.v1"
	syshooklg "github.com/sirupsen/logrus/hooks/syslog"
)

func main() {
	hook, _ := syshooklg.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
	logger := logrus.NewLogger(logrus.WithHook(hook))
	...
}
WithFileEnabled

sets whether the standard logger output will be in file. Accepts multi writing (file and console).

// file enable true
logger := logrus.NewLogger(logrus.WithFileEnabled(true))

// file enable false
logger := logrus.NewLogger(logrus.WithFileEnabled(false))
WithFilePath

sets the path where the file will be saved.

// file path
logger := logrus.NewLogger(logrus.WithFilePath("/tmp"))
WithFileName

sets the name of the file.

// file name
logger := logrus.NewLogger(logrus.WithFileName("application.log"))
WithFileMaxSize

sets the maximum size in megabytes of the log file. It defaults to 100 megabytes.

// file max size
logger := logrus.NewLogger(logrus.WithFileMaxSize(100))
WithFileCompress

sets whether the log files should be compressed.

// file compress true
logger := logrus.NewLogger(logrus.WithFileCompress(true))

// file compress false
logger := logrus.NewLogger(logrus.WithFileCompress(false))
WithFileMaxAge

sets the maximum number of days to retain old log files based on the timestamp encoded in their filename.

// file max age
logger := logrus.NewLogger(logrus.WithFileMaxAge(10))
WithErrorFieldName

sets the field name used on WithError

logger := logrus.NewLogger(logrus.WithErrorFieldName("error"))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(option ...Option) log.Logger

NewLogger constructs a new Logger from provided variadic Option.

func NewLoggerWithOptions

func NewLoggerWithOptions(options *Options) log.Logger

NewLoggerWithOptions constructs a new Logger from provided Options.

Types

type Option

type Option func(options *Options)

func WithConsoleEnabled

func WithConsoleEnabled(value bool) Option

func WithConsoleLevel

func WithConsoleLevel(value string) Option

func WithErrorFieldName

func WithErrorFieldName(value string) Option

func WithFileCompress

func WithFileCompress(value bool) Option

func WithFileEnabled

func WithFileEnabled(value bool) Option

func WithFileLevel

func WithFileLevel(value string) Option

func WithFileMaxAge

func WithFileMaxAge(value int) Option

func WithFileMaxSize

func WithFileMaxSize(value int) Option

func WithFileName

func WithFileName(value string) Option

func WithFilePath

func WithFilePath(value string) Option

func WithFormatter

func WithFormatter(value logrus.Formatter) Option

func WithHook

func WithHook(value logrus.Hook) Option

func WithTimeFormat

func WithTimeFormat(value string) Option

type Options

type Options struct {
	Formatter      logrus.Formatter // formatter TEXT/JSON/CLOUDWATCH
	ErrorFieldName string           // define field name for error logging
	Time           struct {
		Format string // date and time formats
	}
	Console struct {
		Enabled bool   // enable/disable console logging
		Level   string // console log level
	}
	Hooks []logrus.Hook
	File  struct {
		Enabled  bool   // enable/disable file logging
		Level    string // file log level
		Path     string // file log path
		Name     string // log filename
		MaxSize  int    // log file max size (MB)
		Compress bool   // enabled/disable file compress
		MaxAge   int    // file max age
	}
}

Directories

Path Synopsis
formatter

Jump to

Keyboard shortcuts

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