logrusutil

package
v0.0.0-...-c6ea6ab Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: LGPL-3.0 Imports: 8 Imported by: 4

Documentation

Index

Examples

Constants

View Source
const (
	Llongfile = 1 << iota
	Lshortfile
	Ltime
	Llevel
	LstdFlags = Ltime | Lshortfile | Llevel
)

flags

Variables

This section is empty.

Functions

func NewConsoleLogger

func NewConsoleLogger() *logrus.Logger

NewConsoleLogger create new Logger with ConsoleLogFormatter

func StackLogLevel

func StackLogLevel(logger *logrus.Logger, level logrus.Level) (recover func())

StackLogLevel temporary change log level and return recover function

Example
package main

import (
	"fmt"
	"os"

	"github.com/sirupsen/logrus"
	"github.com/tsaikd/KDGoLib/logrusutil"
)

func main() {
	// logger := logrus.New()
	logger := &logrus.Logger{
		Out: os.Stdout,
		Formatter: &logrusutil.ConsoleLogFormatter{
			Flag: logrusutil.Llevel,
		},
		Hooks: make(logrus.LevelHooks),
		Level: logrus.InfoLevel,
	}
	defer func() {
		fmt.Println("after logger level", logger.Level)
	}()

	logger.Debugln("debug message")
	logger.Infoln("info message")

	defer logrusutil.StackLogLevel(logger, logrus.DebugLevel)()
	fmt.Println("stack logger level", logger.Level)

	logger.Debugln("debug message")
	logger.Infoln("info message")
}
Output:

[info] info message
stack logger level debug
[debug] debug message
[info] info message
after logger level info

Types

type ConsoleLogFormatter

type ConsoleLogFormatter struct {
	TimestampFormat      string
	Flag                 int
	CallerOffset         int
	RuntimeCallerFilters []runtimecaller.Filter
}

ConsoleLogFormatter suitable formatter for console

func (*ConsoleLogFormatter) Format

func (t *ConsoleLogFormatter) Format(entry *logrus.Entry) (data []byte, err error)

Format output logrus entry

Jump to

Keyboard shortcuts

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