dyclog

package module
v1.0.0-rc.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: Apache-2.0 Imports: 15 Imported by: 1

README

Overview

The go-dyclog SDK provides simple log APIs for douyincloud developer

How It Works

// main.go
package main

import (
	"context"
	
	"github.com/bytedance/go-dyclog"
)

func main() {
	// Start your vefaas function =D.
	vefaas.Start(handler)
}

// Define your handler function.
func handler(ctx context.Context, r *events.HTTPRequest) (*events.EventResponse, error) {
	// inject logid into context
	dyclog.InjectLogIDToCtx(ctx, r.Headers["x-tt-logid"])
	// close logger
	defer func() {
		_ = dyclog.Close()
	}()

	// Support Debug, Info, Warn, Error, Fatal 
	dyclog.Debug("received new request: %s %s, request id: %s\n", r.HTTPMethod, r.Path, vefaascontext.RequestIdFrom)
	dyclog.CtxDebug(ctx, "received new request: %s %s, request id: %s\n", r.HTTPMethod, r.Path, vefaascontext.RequestIdFrom)

	return &events.EventResponse{
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
		Body: []byte("Hello veFaaS!"),
	}, nil
}

Features

  • Simple architectural design
  • Concise interface abstraction
  • A certain scalability
  • Support base log levels
  • Support customization Formatter
  • Support customization Writer

Interfaces

basis log methods

  • func Debug(format string, args ...interface{})
  • func Info(format string, args ...interface{})
  • func Notice(format string, args ...interface{})
  • func Warn(format string, args ...interface{})
  • func Error(format string, args ...interface{})
  • func Fatal(format string, args ...interface{})

with context basis log methods

  • func CtxDebug(ctx context.Context, format string, args ...interface{})
  • func CtxInfo(ctx context.Context, format string, args ...interface{})
  • func CtxNotice(ctx context.Context, format string, args ...interface{})
  • func CtxWarn(ctx context.Context, format string, args ...interface{})
  • func CtxError(ctx context.Context, format string, args ...interface{})
  • func CtxFatal(ctx context.Context, format string, args ...interface{})

setting methods

  • func SetWriter(writer LogWriter)
  • func SetFormatter(formatter Formatter)
  • func SetLevel(level Level)
  • func Flush() error
  • func Close() error

Examples

The following two methods use ConsoleWriter by default to output logs through stdout

// the first method
import "github.com/bytedance/go-dyclog"

func func main() {
    dyclog.Debug("test go-dyclog!")
    dyclog.CtxDebug(context.Background(), "test go-dyclog!")
    _ = dyclog.Close()
}
// the second method
import "github.com/bytedance/go-dyclog"

var Logger *dyclog.Logger

func init() {
    Logger = NewDefaultLogger()
}
func func main() {
    Logger.Debug("test go-dyclog!")
    Logger.CtxDebug(context.Background(), "test go-dyclog!")
    _ = Logger.Close()
}

The following cases use FileWriter to write log files. Douyin Cloud does not support file log collection for the time being, so stay tuned


import "github.com/bytedance/go-dyclog"

var Logger *dyclog.Logger

func init() {
    // the second args, RotationWindow, enum 0 Daily 1 Hourly
    Logger = dyclog.NewLogger(dyclog.NewFileWriter("./logs/dyc.log", 0))
}

func func main() {
    Logger.Debug("test go-dyclog!")
    Logger.CtxDebug(context.Background(), "test go-dyclog!")
    _ = Logger.Close()
}

Security

If you discover a potential security issue in this project, or think you may have discovered a security issue, we ask that you notify Bytedance Security via our security center or vulnerability reporting email.

Please do not create a public GitHub issue.

License

This project is licensed under the Apache-2.0 License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidLogLevel = errors.New("logger: invalid log level")

Functions

func Close

func Close() error

func CtxDebug

func CtxDebug(ctx context.Context, format string, args ...interface{})

func CtxError

func CtxError(ctx context.Context, format string, args ...interface{})

func CtxFatal

func CtxFatal(ctx context.Context, format string, args ...interface{})

func CtxInfo

func CtxInfo(ctx context.Context, format string, args ...interface{})

func CtxNotice

func CtxNotice(ctx context.Context, format string, args ...interface{})

func CtxWarn

func CtxWarn(ctx context.Context, format string, args ...interface{})

func Debug

func Debug(format string, args ...interface{})

func Error

func Error(format string, args ...interface{})

func Fatal

func Fatal(format string, args ...interface{})

func Flush

func Flush() error

func GetCaller

func GetCaller(depth int) *runtime.Frame

func GetCallerLocation

func GetCallerLocation(caller *runtime.Frame) (string, int)

func GetLocalIP

func GetLocalIP() string

func GetLogIDFromCtx

func GetLogIDFromCtx(ctx context.Context) string

func GetRemoteIP

func GetRemoteIP(req *http.Request) string

func Info

func Info(format string, args ...interface{})

func InjectLogIDToCtx

func InjectLogIDToCtx(ctx context.Context, logID string) context.Context

func Notice

func Notice(format string, args ...interface{})

func SetCallDepth

func SetCallDepth(depth int)

func SetFormatter

func SetFormatter(formatter Formatter)

func SetLevel

func SetLevel(level Level)

func SetWriter

func SetWriter(writer LogWriter)

func Warn

func Warn(format string, args ...interface{})

Types

type AsyncWriter

type AsyncWriter struct {
	LogWriter
	// contains filtered or unexported fields
}

func (*AsyncWriter) Close

func (w *AsyncWriter) Close() error

func (*AsyncWriter) Flush

func (w *AsyncWriter) Flush() error

func (*AsyncWriter) Write

func (w *AsyncWriter) Write(log []byte) error

type BufferWriter

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

func (*BufferWriter) Bytes

func (bw *BufferWriter) Bytes() []byte

func (*BufferWriter) Close

func (bw *BufferWriter) Close() error

func (*BufferWriter) Flush

func (bw *BufferWriter) Flush() error

func (*BufferWriter) Reset

func (bw *BufferWriter) Reset() error

func (*BufferWriter) String

func (bw *BufferWriter) String() string

func (*BufferWriter) Write

func (bw *BufferWriter) Write(formatLog []byte) error

type ConsoleWriter

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

func (*ConsoleWriter) Close

func (cw *ConsoleWriter) Close() error

func (*ConsoleWriter) Flush

func (cw *ConsoleWriter) Flush() error

func (*ConsoleWriter) Write

func (cw *ConsoleWriter) Write(formatLog []byte) error

type FileOption

type FileOption func(writer *FileWriter)

func SetLimitFiles

func SetLimitFiles(n int) FileOption

type FileWriter

type FileWriter struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

FileWriter provides a file rotated output to loggers, it is thread-safe and uses memory buffer to boost file writing performance.

func (*FileWriter) Close

func (w *FileWriter) Close() error

func (*FileWriter) Flush

func (w *FileWriter) Flush() error

func (*FileWriter) Write

func (w *FileWriter) Write(formatLog []byte) error

type Formatter

type Formatter interface {
	Format(log) ([]byte, error)
}

type Level

type Level int
const (
	InvalidLevel Level = iota - 1
	DEBUG
	INFO
	NOTICE
	WARN
	ERROR
	FATAL
)

func MustParseLevel

func MustParseLevel(s string) Level

func ParseLevel

func ParseLevel(s string) (Level, error)

func (Level) String

func (l Level) String() string

type LogWriter

type LogWriter interface {
	io.Closer
	Write(log []byte) error
	Flush() error
}

func NewAsyncWriter

func NewAsyncWriter(w LogWriter, omit bool) LogWriter

func NewConsoleWriter

func NewConsoleWriter() LogWriter

func NewFileWriter

func NewFileWriter(filename string, window RotationWindow, options ...FileOption) LogWriter

NewFileWriter creates a FileWriter.

type Logger

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

func GetLogger

func GetLogger() *Logger

func NewDefaultLogger

func NewDefaultLogger() *Logger

func NewLogger

func NewLogger(writer LogWriter) *Logger

func (*Logger) Close

func (logger *Logger) Close() error

func (*Logger) CtxDebug

func (logger *Logger) CtxDebug(ctx context.Context, format string, args ...interface{})

func (*Logger) CtxError

func (logger *Logger) CtxError(ctx context.Context, format string, args ...interface{})

func (*Logger) CtxFatal

func (logger *Logger) CtxFatal(ctx context.Context, format string, args ...interface{})

func (*Logger) CtxInfo

func (logger *Logger) CtxInfo(ctx context.Context, format string, args ...interface{})

func (*Logger) CtxNotice

func (logger *Logger) CtxNotice(ctx context.Context, format string, args ...interface{})

func (*Logger) CtxWarn

func (logger *Logger) CtxWarn(ctx context.Context, format string, args ...interface{})

func (*Logger) Debug

func (logger *Logger) Debug(format string, args ...interface{})

func (*Logger) Error

func (logger *Logger) Error(format string, args ...interface{})

func (*Logger) Fatal

func (logger *Logger) Fatal(format string, args ...interface{})

func (*Logger) Flush

func (logger *Logger) Flush() error

func (*Logger) GetWriter

func (logger *Logger) GetWriter() LogWriter

func (*Logger) Info

func (logger *Logger) Info(format string, args ...interface{})

func (*Logger) Logf

func (logger *Logger) Logf(ctx context.Context, level Level, format string, args ...interface{})

func (*Logger) Notice

func (logger *Logger) Notice(format string, args ...interface{})

func (*Logger) SetCallDepth

func (logger *Logger) SetCallDepth(depth int)

func (*Logger) SetFormatter

func (logger *Logger) SetFormatter(formatter Formatter)

func (*Logger) SetLevel

func (logger *Logger) SetLevel(level Level)

func (*Logger) SetWriter

func (logger *Logger) SetWriter(writer LogWriter)

func (*Logger) Warn

func (logger *Logger) Warn(format string, args ...interface{})

type RotationWindow

type RotationWindow int8

RotationWindow allows to claim which rotation window provider uses.

const (
	// Daily means rotate daily.
	Daily RotationWindow = iota
	// Hourly means rotate hourly.
	Hourly
)

type TextFormatter

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

func NewDefaultTextFormatter

func NewDefaultTextFormatter() *TextFormatter

func NewTextFormatter

func NewTextFormatter(enableColor bool) *TextFormatter

func (*TextFormatter) Format

func (f *TextFormatter) Format(l log) ([]byte, error)

func (*TextFormatter) SetColor

func (f *TextFormatter) SetColor(enable bool)

func (*TextFormatter) SetQuote

func (f *TextFormatter) SetQuote(enable bool)

func (*TextFormatter) SetTimestamp

func (f *TextFormatter) SetTimestamp(enable bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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