Documentation ¶
Index ¶
- 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 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
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewContext ¶
NewContext return a new logger context.
func NewHTTPContextHandler ¶
NewHTTPContextHandler adds a context logger based on the given logger to each request. After a request passes through this handler, WithContext(req.Context()).Error(, "foo") will log to that logger and add useful context to each log entry.
func StreamInterceptor ¶
func StreamInterceptor(l Logger) grpc.StreamServerInterceptor
StreamInterceptor returns a grpc.StreamServerInterceptor that provides a context logger with request_id. If a request-id is already specified in the metadata, it will be used. Otherwise a new one will be generated. For REST API, use Grpc-Metadata-Request-Id as header key for passing a request id.
func UnaryInterceptor ¶
func UnaryInterceptor(l Logger) grpc.UnaryServerInterceptor
UnaryInterceptor returns a grpc.UnaryServerInterceptor that provides a context logger with request_id. If a request-id is already specified in the metadata, it will be used. Otherwise a new one will be generated. For REST API, use Grpc-Metadata-Request-Id as header key for passing a request id.
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() Option
FromEnv provides an option to load all options from environment. 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 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.