logos

package module
v0.0.0-...-ee5be7b Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2021 License: MIT Imports: 21 Imported by: 0

README

logos

Библиотека для логирования пакетов в стиле log4j

High Performance

A quick and simple benchmark with zap/zerolog, which runs on [github actions][benchmark]:

// go test -v -cpu=4 -run=none -bench=. -benchtime=10s -benchmem log_test.go
package main

import (
	"io/ioutil"
	"testing"

	"github.com/phuslu/log"
	"github.com/v8platform/logos"
	"github.com/rs/zerolog"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var fakeMessage = "Test logging, but use a somewhat realistic message length. "

func BenchmarkLogos(b *testing.B) {

	const newConfig = `
appenders:
  console:
    - name: CONSOLE
      target: discard
      encoder:
        console:
loggers:
  root:
    level: info
    appender_refs:
      - CONSOLE
`
	err := logos.InitWithConfigContent(newConfig)
	if err != nil {
		panic(err)
	}

	logger := logos.New("benchmark")
	for i := 0; i < b.N; i++ {
		logger.Info(fakeMessage, zap.String("foo", "bar"), zap.Int("int", 42))
	}
}

func BenchmarkZap(b *testing.B) {
	logger := zap.New(zapcore.NewCore(
		zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
		zapcore.AddSync(ioutil.Discard),
		zapcore.InfoLevel,
	))
	for i := 0; i < b.N; i++ {
		logger.Info(fakeMessage, zap.String("foo", "bar"), zap.Int("int", 42))
	}
}

func BenchmarkZeroLog(b *testing.B) {
	logger := zerolog.New(ioutil.Discard).With().Timestamp().Logger()
	for i := 0; i < b.N; i++ {
		logger.Info().Str("foo", "bar").Int("int", 42).Msg(fakeMessage)
	}
}

func BenchmarkPhusLog(b *testing.B) {
	logger := log.Logger{
		TimeFormat: "", // uses rfc3339 by default
		Writer:     log.IOWriter{ioutil.Discard},
	}
	for i := 0; i < b.N; i++ {
		logger.Info().Str("foo", "bar").Int("int", 42).Msg(fakeMessage)
	}
}



A Performance result as below, for daily benchmark results see [github actions][benchmark]


Documentation

Index

Examples

Constants

View Source
const (

	// OffLevel
	OffLevel = zapcore.Level(-2)
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel = zapcore.DebugLevel
	// InfoLevel is the default logging priority.
	InfoLevel = zapcore.InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel = zapcore.WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel = zapcore.ErrorLevel
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel = zapcore.DPanicLevel
	// PanicLevel logs a message, then panics.
	PanicLevel = zapcore.PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel = zapcore.FatalLevel
)
View Source
const DefaultConfig = `` /* 301-byte string literal not displayed */

Variables

View Source
var StackTraceLevelEnabler = zap.NewAtomicLevelAt(zapcore.ErrorLevel)

Functions

func InitWithConfigContent

func InitWithConfigContent(content string) error

func SetLevel

func SetLevel(name string, level zapcore.Level)

func Sync

func Sync()

Types

type AppenderConfig

type AppenderConfig struct {
	Name  string `logos-config:"name" logos-validate:"required"`
	Level string `logos-config:"level"`
}

type Config

type Config struct {
	Appenders map[string][]*common.Config `logos-config:"appenders"`
	Loggers   Loggers                     `logos-config:"loggerConfigs"`
}

type Field

type Field = zapcore.Field

Field is an alias for Field. Aliasing this type dramatically improves the navigability of this package's API documentation.

func Any

func Any(key string, value interface{}) Field

Any takes a key and an arbitrary value and chooses the best way to represent them as a field, falling back to a reflection-based approach only if necessary.

Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between them. To minimize surprises, []byte values are treated as binary blobs, byte values are treated as uint8, and runes are always treated as integers.

func Binary

func Binary(key string, val []byte) Field

Binary constructs a field that carries an opaque binary blob.

Binary data is serialized in an encoding-appropriate format. For example, zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text, use ByteString.

func Bool

func Bool(key string, val bool) Field

Bool constructs a field that carries a bool.

func Boolp

func Boolp(key string, val *bool) Field

Boolp constructs a field that carries a *bool. The returned Field will safely and explicitly represent `nil` when appropriate.

func ByteString

func ByteString(key string, val []byte) Field

ByteString constructs a field that carries UTF-8 encoded text as a []byte. To log opaque binary blobs (which aren't necessarily valid UTF-8), use Binary.

func Complex128

func Complex128(key string, val complex128) Field

Complex128 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex128 to interface{}).

func Complex128p

func Complex128p(key string, val *complex128) Field

Complex128p constructs a field that carries a *complex128. The returned Field will safely and explicitly represent `nil` when appropriate.

func Complex64

func Complex64(key string, val complex64) Field

Complex64 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex64 to interface{}).

func Complex64p

func Complex64p(key string, val *complex64) Field

Complex64p constructs a field that carries a *complex64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Duration

func Duration(key string, val time.Duration) Field

Duration constructs a field with the given key and value. The encoder controls how the duration is serialized.

func Durationp

func Durationp(key string, val *time.Duration) Field

Durationp constructs a field that carries a *time.Duration. The returned Field will safely and explicitly represent `nil` when appropriate.

func Float32

func Float32(key string, val float32) Field

Float32 constructs a field that carries a float32. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Float32p

func Float32p(key string, val *float32) Field

Float32p constructs a field that carries a *float32. The returned Field will safely and explicitly represent `nil` when appropriate.

func Float64

func Float64(key string, val float64) Field

Float64 constructs a field that carries a float64. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Float64p

func Float64p(key string, val *float64) Field

Float64p constructs a field that carries a *float64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int

func Int(key string, val int) Field

Int constructs a field with the given key and value.

func Int16

func Int16(key string, val int16) Field

Int16 constructs a field with the given key and value.

func Int16p

func Int16p(key string, val *int16) Field

Int16p constructs a field that carries a *int16. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int32

func Int32(key string, val int32) Field

Int32 constructs a field with the given key and value.

func Int32p

func Int32p(key string, val *int32) Field

Int32p constructs a field that carries a *int32. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int64

func Int64(key string, val int64) Field

Int64 constructs a field with the given key and value.

func Int64p

func Int64p(key string, val *int64) Field

Int64p constructs a field that carries a *int64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int8

func Int8(key string, val int8) Field

Int8 constructs a field with the given key and value.

func Int8p

func Int8p(key string, val *int8) Field

Int8p constructs a field that carries a *int8. The returned Field will safely and explicitly represent `nil` when appropriate.

func Intp

func Intp(key string, val *int) Field

Intp constructs a field that carries a *int. The returned Field will safely and explicitly represent `nil` when appropriate.

func Namespace

func Namespace(key string) Field

Namespace creates a named, isolated scope within the logger's context. All subsequent fields will be added to the new namespace.

This helps prevent key collisions when injecting loggers into sub-components or third-party libraries.

func Object

func Object(key string, val zapcore.ObjectMarshaler) Field

Object constructs a field with the given key and ObjectMarshaler. It provides a flexible, but still type-safe and efficient, way to add map- or struct-like user-defined types to the logging context. The struct's MarshalLogObject method is called lazily.

func Reflect

func Reflect(key string, val interface{}) Field

Reflect constructs a field with the given key and an arbitrary object. It uses an encoding-appropriate, reflection-based function to lazily serialize nearly any object into the logging context, but it's relatively slow and allocation-heavy. Outside tests, Any is always a better choice.

If encoding fails (e.g., trying to serialize a map[int]string to JSON), Reflect includes the error message in the final log output.

func Skip

func Skip() Field

Skip constructs a no-op field, which is often useful when handling invalid inputs in other Field constructors.

func Stack

func Stack(key string) Field

Stack constructs a field that stores a stacktrace of the current goroutine under provided key. Keep in mind that taking a stacktrace is eager and expensive (relatively speaking); this function both makes an allocation and takes about two microseconds.

func StackSkip

func StackSkip(key string, skip int) Field

StackSkip constructs a field similarly to Stack, but also skips the given number of frames from the top of the stacktrace.

func String

func String(key string, val string) Field

String constructs a field with the given key and value.

func Stringer

func Stringer(key string, val fmt.Stringer) Field

Stringer constructs a field with the given key and the output of the value's String method. The Stringer's String method is called lazily.

func Stringp

func Stringp(key string, val *string) Field

Stringp constructs a field that carries a *string. The returned Field will safely and explicitly represent `nil` when appropriate.

func Time

func Time(key string, val time.Time) Field

Time constructs a Field with the given key and value. The encoder controls how the time is serialized.

func Timep

func Timep(key string, val *time.Time) Field

Timep constructs a field that carries a *time.Time. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint

func Uint(key string, val uint) Field

Uint constructs a field with the given key and value.

func Uint16

func Uint16(key string, val uint16) Field

Uint16 constructs a field with the given key and value.

func Uint16p

func Uint16p(key string, val *uint16) Field

Uint16p constructs a field that carries a *uint16. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint32

func Uint32(key string, val uint32) Field

Uint32 constructs a field with the given key and value.

func Uint32p

func Uint32p(key string, val *uint32) Field

Uint32p constructs a field that carries a *uint32. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint64

func Uint64(key string, val uint64) Field

Uint64 constructs a field with the given key and value.

func Uint64p

func Uint64p(key string, val *uint64) Field

Uint64p constructs a field that carries a *uint64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint8

func Uint8(key string, val uint8) Field

Uint8 constructs a field with the given key and value.

func Uint8p

func Uint8p(key string, val *uint8) Field

Uint8p constructs a field that carries a *uint8. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uintp

func Uintp(key string, val *uint) Field

Uintp constructs a field that carries a *uint. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uintptr

func Uintptr(key string, val uintptr) Field

Uintptr constructs a field with the given key and value.

func Uintptrp

func Uintptrp(key string, val *uintptr) Field

Uintptrp constructs a field that carries a *uintptr. The returned Field will safely and explicitly represent `nil` when appropriate.

type Logger

type Logger interface {
	// Debug uses fmt.Sprint to construct and log a message.
	Debug(msg string, fields ...Field)

	// Info uses fmt.Sprint to construct and log a message.
	Info(msg string, fields ...Field)

	// Warn uses fmt.Sprint to construct and log a message.
	Warn(msg string, fields ...Field)

	// Error uses fmt.Sprint to construct and log a message.
	Error(msg string, fields ...Field)

	// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit(1).
	Fatal(msg string, fields ...Field)

	// Panic uses fmt.Sprint to construct and log a message, then panics.
	Panic(msg string, fields ...Field)

	// DPanic uses fmt.Sprint to construct and log a message. In development, the
	// logger then panics.
	DPanic(msg string, fields ...Field)

	Sync() error

	Sugar() SugaredLogger
}

func New

func New(name string) Logger
Example
package main

import (
	"github.com/v8platform/logos"
)

func main() {

	log := logos.New("github.com/v8platform/test")
	log.Info("Error")
	log.Sync()

}
Output:

2021-01-28T22:10:37.059+0300	INFO	github.com/v8platform/test	Error	{"url": "url", "attempt": 3, "backoff": 1}

type LoggerConfig

type LoggerConfig struct {
	Name           string           `logos-config:"name" logos-validate:"required"`
	Level          string           `logos-config:"level"`
	AddCaller      bool             `logos-config:"add_caller"`
	TraceLevel     string           `logos-config:"trace_level"`
	AppenderRefs   []string         `logos-config:"appender_refs"`
	AppenderConfig []AppenderConfig `logos-config:"appenders"`
}

type Loggers

type Loggers struct {
	Root   RootLogger     `logos-config:"root"`
	Logger []LoggerConfig `logos-config:"logger"`
}

type RootLogger

type RootLogger struct {
	Level          string           `logos-config:"level"`
	AppenderRefs   []string         `logos-config:"appender_refs"`
	AppenderConfig []AppenderConfig `logos-config:"appenders"`
}

type ScanConfig

type ScanConfig struct {
	Scan       bool   `logos-config:"scan"`
	ScanPeriod string `logos-config:"scan_period"`
}

type SugaredLogger

type SugaredLogger interface {
	// Debug uses fmt.Sprint to construct and log a message.
	Debug(msg string, fields ...Field)

	// Info uses fmt.Sprint to construct and log a message.
	Info(msg string, fields ...Field)

	// Warn uses fmt.Sprint to construct and log a message.
	Warn(msg string, fields ...Field)

	// Error uses fmt.Sprint to construct and log a message.
	Error(msg string, fields ...Field)

	// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit(1).
	Fatal(msg string, fields ...Field)

	// Panic uses fmt.Sprint to construct and log a message, then panics.
	Panic(msg string, fields ...Field)

	// DPanic uses fmt.Sprint to construct and log a message. In development, the
	// logger then panics.
	DPanic(msg string, fields ...Field)

	// Debugf uses fmt.Sprintf to construct and log a message.
	Debugf(format string, args ...interface{})

	// Infof uses fmt.Sprintf to log a templated message.
	Infof(format string, args ...interface{})

	// Warnf uses fmt.Sprintf to log a templated message.
	Warnf(format string, args ...interface{})

	// Errorf uses fmt.Sprintf to log a templated message.
	Errorf(format string, args ...interface{})

	// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit(1).
	Fatalf(format string, args ...interface{})

	// Panicf uses fmt.Sprintf to log a templated message, then panics.
	Panicf(format string, args ...interface{})

	// DPanicf uses fmt.Sprintf to log a templated message. In development, the
	// logger then panics.
	DPanicf(format string, args ...interface{})

	// Debugw logs a message with some additional context. The additional context
	// is added in the form of key-value pairs. The optimal way to write the value
	// to the log message will be inferred by the value's type. To explicitly
	// specify a type you can pass a Field such as logp.Stringer.
	Debugw(msg string, keysAndValues ...interface{})

	// Infow logs a message with some additional context. The additional context
	// is added in the form of key-value pairs. The optimal way to write the value
	// to the log message will be inferred by the value's type. To explicitly
	// specify a type you can pass a Field such as logp.Stringer.
	Infow(msg string, keysAndValues ...interface{})

	// Warnw logs a message with some additional context. The additional context
	// is added in the form of key-value pairs. The optimal way to write the value
	// to the log message will be inferred by the value's type. To explicitly
	// specify a type you can pass a Field such as logp.Stringer.
	Warnw(msg string, keysAndValues ...interface{})

	// Errorw logs a message with some additional context. The additional context
	// is added in the form of key-value pairs. The optimal way to write the value
	// to the log message will be inferred by the value's type. To explicitly
	// specify a type you can pass a Field such as logp.Stringer.
	Errorw(msg string, keysAndValues ...interface{})

	// Fatalw logs a message with some additional context, then calls os.Exit(1).
	// The additional context is added in the form of key-value pairs. The optimal
	// way to write the value to the log message will be inferred by the value's
	// type. To explicitly specify a type you can pass a Field such as
	// logp.Stringer.
	Fatalw(msg string, keysAndValues ...interface{})

	// Panicw logs a message with some additional context, then panics. The
	// additional context is added in the form of key-value pairs. The optimal way
	// to write the value to the log message will be inferred by the value's type.
	// To explicitly specify a type you can pass a Field such as logp.Stringer.
	Panicw(msg string, keysAndValues ...interface{})

	// DPanicw logs a message with some additional context. The logger panics only
	// in Development mode.  The additional context is added in the form of
	// key-value pairs. The optimal way to write the value to the log message will
	// be inferred by the value's type. To explicitly specify a type you can pass a
	// Field such as logp.Stringer.
	DPanicw(msg string, keysAndValues ...interface{})

	Sync() error

	Desugar() Logger
}

Directories

Path Synopsis
encoder
internal

Jump to

Keyboard shortcuts

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