Documentation ¶
Overview ¶
Package log provides an easy-to-use and structured logger.
Example ¶
package main import ( "github.com/pthethanh/micro/log" ) func main() { log.Info("Hello, micro!") log.Infof("Hello, %s!", "jack") log.Warn("This is a warning") log.Debug("hello micro debug") log.Error("this is an error") //log.Panic("this is a panic") log.Fields("name", "micro", "site", "VN").Info("this is a log with fields") }
Output:
Index ¶
- Constants
- func Debug(v ...interface{})
- func Debugf(format string, v ...interface{})
- func Error(v ...interface{})
- func Errorf(format string, v ...interface{})
- func Info(v ...interface{})
- func Infof(format string, v ...interface{})
- func Init(opts ...Option) error
- func MustJSON(v interface{}) string
- func NewContext(ctx context.Context, logger Logger) context.Context
- func NewHTTPContextHandler(l Logger) func(http.Handler) http.Handler
- func Panic(v ...interface{})
- func Panicf(format string, v ...interface{})
- func StreamInterceptor(l Logger) grpc.StreamServerInterceptor
- func UnaryInterceptor(l Logger) grpc.UnaryServerInterceptor
- func Warn(v ...interface{})
- func Warnf(format string, v ...interface{})
- type Format
- type Level
- type Logger
- type Logrus
- func (l *Logrus) Context(ctx context.Context) Logger
- func (l *Logrus) Debug(v ...interface{})
- func (l *Logrus) Debugf(format string, v ...interface{})
- func (l *Logrus) Error(v ...interface{})
- func (l *Logrus) Errorf(format string, v ...interface{})
- func (l *Logrus) Fields(kv ...interface{}) Logger
- func (l *Logrus) Info(args ...interface{})
- func (l *Logrus) Infof(format string, v ...interface{})
- func (l *Logrus) Init(opts ...Option) error
- func (l *Logrus) Panic(v ...interface{})
- func (l *Logrus) Panicf(format string, v ...interface{})
- func (l *Logrus) Warn(v ...interface{})
- func (l *Logrus) Warnf(format string, v ...interface{})
- type Option
- type Options
Examples ¶
Constants ¶
const ( // CorrelationID is field name of Correlation ID that is used to track related logs. CorrelationID string = "correlation_id" )
Variables ¶
This section is empty.
Functions ¶
func Init ¶
Init init the root logger with options.
Example (FromEnvironmentVariables) ¶
package main import ( "github.com/pthethanh/micro/log" ) func main() { log.Init(log.FromEnv()) log.Info("hello") }
Output:
Example (WithOptions) ¶
package main import ( "os" "time" "github.com/pthethanh/micro/log" ) func main() { log.Init( log.WithFields("name", "my service"), log.WithFormat(log.FormatJSON), log.WithLevel(log.LevelDebug), log.WithTimeFormat(time.RFC1123), log.WithWriter(os.Stdout), ) log.Info("hello") }
Output:
func MustJSON ¶ added in v0.1.8
func MustJSON(v interface{}) string
MustJSON return JSON string of the given value. In case of a marshal failure, return string value using fmt package.
func NewContext ¶
NewContext return a new logger context.
func NewHTTPContextHandler ¶
NewHTTPContextHandler provides a context logger with correlation_id and other basic HTTP information. Value of correlation_id will be retrieved from X-Correlation-ID or X-Request-ID in header of the incoming request. If no value of correlation_id is provided, a new UUID will be generated. This middleware should be used for HTTP handler only. Since this method use a Hijack response writer, it cannot be used with option server.HTTPInterceptors.
func StreamInterceptor ¶
func StreamInterceptor(l Logger) grpc.StreamServerInterceptor
StreamInterceptor returns a grpc.StreamServerInterceptor that provides a context logger with correlation_id. It will try to looks for value of X-Correlation-ID or X-Request-ID in the metadata of the incoming request. If no value is provided, a new UUID will be generated. For REST API via gRPC Gateway, pass the value of X-Correlation-ID or X-Request-ID in the header.
func UnaryInterceptor ¶
func UnaryInterceptor(l Logger) grpc.UnaryServerInterceptor
UnaryInterceptor returns a grpc.UnaryServerInterceptor that provides a context logger with correlation_id. It will try to looks for value of X-Correlation-ID or X-Request-ID in the metadata of the incoming request. If no value is provided, a new UUID will be generated. For REST API via gRPC Gateway, pass the value of X-Correlation-ID or X-Request-ID in the header.
Types ¶
type Level ¶
type Level int32
Level is log level.
const ( // LevelPanic level, highest level of severity. Logs and then calls panic with the // message passed to Debug, Info, ... LevelPanic Level = iota // LevelFatal level. Logs and then calls os.Exit. It will exit even if the // logging level is set to Panic. LevelFatal // LevelError level. Logs. Used for errors that should definitely be noted. // Commonly used for hooks to send errors to an error tracking service. LevelError // LevelWarn level. Non-critical entries that deserve eyes. LevelWarn // LevelInfo level. General operational entries about what's going on inside the // application. LevelInfo // LevelDebug level. Usually only enabled when debugging. Very verbose logging. LevelDebug // LevelTrace level. Designates finer-grained informational events than the Debug. LevelTrace )
These are the different logging levels.
type Logger ¶
type Logger interface { // Init init the logger. Init(...Option) error // Infof print info with format. Infof(format string, v ...interface{}) // Debugf print debug with format. Debugf(format string, v ...interface{}) // Warnf print warning with format. Warnf(format string, v ...interface{}) // Errorf print error with format. Errorf(format string, v ...interface{}) // Panicf panic with format. Panicf(format string, v ...interface{}) // Info print info. Info(v ...interface{}) // Debug print debug. Debug(v ...interface{}) // Warn print warning. Warn(v ...interface{}) // Error print error. Error(v ...interface{}) // Panic panic. Panic(v ...interface{}) // Fields return new logger with the given fields. // The kv should be provided as key values pairs where key is a string. Fields(kv ...interface{}) Logger // Context provide a way to get a context logger, i.e... with request-id. Context(ctx context.Context) Logger }
Logger defines standard operations of a logger.
func FromContext ¶
FromContext get logger form context.
type Logrus ¶ added in v0.0.2
type Logrus struct {
// contains filtered or unexported fields
}
Logrus implement Logger interface using logrus.
type Option ¶
type Option = func(*Options)
Option is an option for configure logger.
func FromEnv ¶
func FromEnv(readOpts ...config.ReadOption) Option
FromEnv provides an option to load all options from environment variables. LOG_LEVEL default:"5" which is debug level LOG_FORMAT default:"json" LOG_TIME_FORMAT default:"Mon, 02 Jan 2006 15:04:05 -0700" LOG_OUTPUT, default to be stdout, use file://my.log for writing to a file. LOG_FIELDS is a map of key/value. i.e: name:myservice,site:vietnam
func FromOptions ¶ added in v0.1.0
FromOptions is an option to create new logger from an existing Options.
func WithFields ¶
func WithFields(kv ...interface{}) Option
WithFields provides an option to set logger fields.
func WithTimeFormat ¶
WithTimeFormat provides an option to set time format for logger.
func WithWriter ¶ added in v0.0.2
WithWriter provides an option to set a output writer.
type Options ¶
type Options struct { Level Level `envconfig:"LOG_LEVEL" default:"5"` Format Format `envconfig:"LOG_FORMAT" default:"json"` TimeFormat string `envconfig:"LOG_TIME_FORMAT" default:"Mon, 02 Jan 2006 15:04:05 -0700"` Output string `envconfig:"LOG_OUTPUT"` Fields map[string]string `envconfig:"LOG_FIELDS"` // contains filtered or unexported fields }
Options hold logger options
func (Options) GetFormat ¶
GetFormat return format of output log. If given format is not valid, JSON format is returned.