log

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2019 License: Apache-2.0 Imports: 8 Imported by: 3

README

Log Build Status GoDoc Go Report Card

A simple go log.

Install

go get github.com/haormj/log

Usage

package main

import (
	"context"

	"github.com/haormj/log"
)

func main() {
	l := log.Logger.Clone()

	l.With("main", "I'm main")
	ctx := log.NewContext(context.Background(), l)
	hello(ctx)
	world(ctx)
	l.Info("main", "end")
}

func hello(ctx context.Context) {
	l, _ := log.FromContext(ctx)
	l.With("hello", "1")
	l.Infow("this is hello function")
}

func world(ctx context.Context) {
	l, _ := log.FromContext(ctx)
	l.With("world", 2)
	l.Infof("this is %s function", "world")
}

If pass Log through Context, pay attention to the life cycle of the Context to prevent memory increase

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DEG level = "debug"
	INF level = "info"
	WRN level = "warn"
	ERR level = "error"
)
View Source
var (
	JSON    encoder = "json"
	Console encoder = "console"
)
View Source
var (
	// DefaultLog default use uber log
	Logger = NewLog()
)

Functions

func LevelEncoder

func LevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

LevelEncoder format level name

func NewContext

func NewContext(ctx context.Context, l Log) context.Context

NewContext put Log to context

func ParseEncoder

func ParseEncoder(e string) encoder

ParseEncoder parse string to encoder

func ParseLevel

func ParseLevel(l string) level

ParseLevel parse string to level

func TimeEncoder

func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

TimeEncoder format time

Types

type Log

type Log interface {
	Debug(keysAndValues ...interface{})
	Debugw(msg string, keysAndValues ...interface{})
	Debugf(format string, a ...interface{})

	Info(keysAndValues ...interface{})
	Infow(msg string, keysAndValues ...interface{})
	Infof(format string, a ...interface{})

	Warn(keysAndValues ...interface{})
	Warnw(msg string, keysAndValues ...interface{})
	Warnf(format string, a ...interface{})

	Error(keysAndValues ...interface{})
	Errorw(msg string, keysAndValues ...interface{})
	Errorf(format string, a ...interface{})

	With(keysAndValues ...interface{})
	Flush() error
	Clone() Log
}

Log interface

func FromContext

func FromContext(ctx context.Context) (Log, bool)

FromContext get Log from context

func NewLog

func NewLog(opts ...Option) Log

NewLog uber log

type Option

type Option func(*Options)

Option sugar for options

func Encoder

func Encoder(e encoder) Option

Encoder choose json/console default is console

func Filename

func Filename(name string) Option

Filename log file name

func Level

func Level(l level) Option

Level log level default is INF

type Options

type Options struct {
	Encoder        encoder
	Level          level
	MessageKey     string
	LevelKey       string
	TimeKey        string
	NameKey        string
	CallerKey      string
	StacktraceKey  string
	LineEnding     string
	EncodeLevel    zapcore.LevelEncoder
	EncodeTime     zapcore.TimeEncoder
	EncodeDuration zapcore.DurationEncoder
	EncodeCaller   zapcore.CallerEncoder
	EncodeName     zapcore.NameEncoder
	Filename       string
	MaxSize        int
	MaxAge         int
	MaxBackups     int
	LocalTime      bool
	Compress       bool
}

Options uber log

type Uber

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

Uber log

func (*Uber) Clone

func (u *Uber) Clone() Log

func (*Uber) Debug

func (u *Uber) Debug(keysAndValues ...interface{})

Debug key value

func (*Uber) Debugf

func (u *Uber) Debugf(format string, a ...interface{})

Debugf formats according to a format specifier

func (*Uber) Debugw

func (u *Uber) Debugw(msg string, keysAndValues ...interface{})

Debugw with message

func (*Uber) Error

func (u *Uber) Error(keysAndValues ...interface{})

Error key value

func (*Uber) Errorf

func (u *Uber) Errorf(format string, a ...interface{})

Errorf formats according to a format specifier

func (*Uber) Errorw

func (u *Uber) Errorw(msg string, keysAndValues ...interface{})

Errorw with message

func (*Uber) Flush

func (u *Uber) Flush() error

Flush buffered log

func (*Uber) Info

func (u *Uber) Info(keysAndValues ...interface{})

Info key value

func (*Uber) Infof

func (u *Uber) Infof(format string, a ...interface{})

Infof formats according to a format specifier

func (*Uber) Infow

func (u *Uber) Infow(msg string, keysAndValues ...interface{})

Infow with message

func (*Uber) Warn

func (u *Uber) Warn(keysAndValues ...interface{})

Warn key value

func (*Uber) Warnf

func (u *Uber) Warnf(format string, a ...interface{})

Warnf formats according to a format specifier

func (*Uber) Warnw

func (u *Uber) Warnw(msg string, keysAndValues ...interface{})

Warnw with message

func (*Uber) With

func (u *Uber) With(keysAndValues ...interface{})

With key value

Jump to

Keyboard shortcuts

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