ylog

package
v1.18.7 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package ylog provides a slog.Logger instance for logging. ylog also provides a default slog.Logger, the default logger is build from environment.

ylog allows to call log api directly, like:

ylog.Debug("test", "name", "yomo")
ylog.Info("test", "name", "yomo")
ylog.Warn("test", "name", "yomo")
ylog.Error("test", "name", "yomo")

Package ylog provides handler that supports splitting log stream to common log stream and error log stream.

Example
package main

import (
	"io"
	"net"

	"github.com/yomorun/yomo/core/ylog"
)

func main() {
	// text format logger
	logger := ylog.NewFromConfig(ylog.Config{
		Level:       "warn",
		Format:      "text",
		ErrorOutput: "stdout",
		DisableTime: true,
	})

	ylog.SetDefault(logger.With("hello", "yomo").WithGroup("syslog"))

	ylog.Debug("debug", "aaa", "bbb")
	ylog.Info("info", "ccc", "ddd")
	ylog.Warn("warn", "eee", "fff")
	ylog.Error("error", "err", io.EOF, "eee", "fff")

	// json format logger
	sysLogger := ylog.NewFromConfig(ylog.Config{
		Level:       "error",
		Format:      "json",
		ErrorOutput: "stdout",
		DisableTime: true,
	})

	sysLogger = sysLogger.WithGroup("syslog")

	sysLogger.Error("sys error", "err", net.ErrClosed, "ggg", "hhh")

}
Output:

level=WARN msg=warn hello=yomo syslog.eee=fff
level=ERROR msg=error hello=yomo syslog.err=EOF syslog.eee=fff
{"level":"ERROR","msg":"sys error","syslog":{"err":"use of closed network connection","ggg":"hhh"}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, keyvals ...interface{})

Debug logs a message at debug level.

func Default

func Default() *slog.Logger

Default returns a slog.Logger according to enviroment.

func Error

func Error(msg string, keyvals ...interface{})

Error logs a message at error level.

func Info

func Info(msg string, keyvals ...interface{})

Info logs a message at info level.

func NewFromConfig

func NewFromConfig(conf Config) *slog.Logger

NewFromConfig returns a slog.Logger according to conf.

func NewHandlerFromConfig

func NewHandlerFromConfig(conf Config) slog.Handler

NewHandlerFromConfig creates a slog.Handler from conf

func SetDefault

func SetDefault(logger *slog.Logger)

SetDefault set global logger.

func Warn

func Warn(msg string, keyvals ...interface{})

Warn logs a message at warn level.

Types

type Config

type Config struct {
	// Verbose indicates if logger log code line, use false for production.
	Verbose bool `env:"YOMO_LOG_VERBOSE" envDefault:"false"`

	// Level can be one of `debug`, `info`, `warn`, `error`
	Level string `env:"YOMO_LOG_LEVEL" envDefault:"info"`

	// Output is the filename of log file,
	// The default is stdout.
	Output string `env:"YOMO_LOG_OUTPUT"`

	// ErrorOutput is the filename of errlog file,
	// The default is stderr.
	ErrorOutput string `env:"YOMO_LOG_ERROR_OUTPUT"`

	// Format supports text and json,
	// The default is text.
	Format string `env:"YOMO_LOG_FORMAT" envDefault:"text"`

	// MaxSize is the maximum size in megabytes of the log file before it gets rotated.
	// It defaults to 100 megabytes.
	MaxSize int `env:"YOMO_LOG_MAX_SIZE"`

	// MaxBackups is the maximum number of old log files to retain.
	// The default is to retain all old log files (though MaxAge may still cause them to get deleted.)
	MaxBackups int `env:"YOMO_LOG_MAX_BACKUPS"`

	// MaxAge is the maximum number of days to retain old log files based on the timestamp encoded in their filename.
	// Note that a day is defined as 24 hours and may not exactly correspond to calendar days due to daylight savings, leap seconds, etc.
	// The default is not to remove old log files based on age.
	MaxAge int `env:"YOMO_LOG_MAX_AGE"`

	// LocalTime determines if the time used for formatting the timestamps in backup files is the computer's local time.
	// The default is to use UTC time.
	LocalTime bool `env:"YOMO_LOG_LOCAL_TIME"`

	// Compress determines if the rotated log files should be compressed using gzip.
	// The default is not to perform compression.
	Compress bool `env:"YOMO_LOG_COMPRESS"`

	// DisableTime disable time key, It's a pravited field, Just for testing.
	DisableTime bool
}

Config is the config of slog, the config is from environment.

Jump to

Keyboard shortcuts

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