Documentation ¶
Overview ¶
Example ¶
// logging with the default global logger Debugf("This is a debug of %s", "hello world") // create a new logger var log = SpawnSimple(WithFields(map[string]interface{}{"component": "mypackage"}), WithTags("user", "account")) // log something log.Write("this is a message and we use the tag info instead of a log level", Tags{"bootstrap", "info"}) // log with extra data log.Dump("A message with structured data", nil, KV{"id": 10, "age": 50, "name": "john smith"}) // create a new logger from another logger userLog := log.SpawnSimple(WithFields(KV{"user_type": "me", "user_id": 1234})) // logging with the new logger userLog.Dump("here's a log with some extra data", Tags{"moardata"}, KV{"kiss": "is simple"}) // change the logging format and min log level Reconfigure( WithLevel(DEBUG), WithFormat(FormatJSON), ) // do a debug log userLog.Dbg("this will show up now", nil, nil)
Output:
Index ¶
- func Debug(msg string)
- func DebugData(msg string, data map[string]interface{})
- func Debugf(format string, args ...interface{})
- func Error(msg string)
- func ErrorData(msg string, data map[string]interface{})
- func Errorf(format string, args ...interface{})
- func F(format string, args ...interface{}) string
- func GetCallerInfo(depth int, fullPath bool) string
- func Info(msg string)
- func InfoData(msg string, data map[string]interface{})
- func Infof(format string, args ...interface{})
- func Log(msg string, tags []string)
- func Logd(msg string, tags []string, fields map[string]interface{})
- func Print(args ...interface{})
- func Printf(format string, args ...interface{})
- func Println(args ...interface{})
- func Reconfigure(opts ...WriterOption)
- func Stringify(v interface{}, quoted bool) string
- func Warn(msg string)
- func WarnData(msg string, data map[string]interface{})
- func Warnf(format string, args ...interface{})
- func Write(msg string)
- func WriteData(msg string, data map[string]interface{})
- type Compatible
- type ConfigurableLogger
- type Entry
- type Fields
- type Format
- type KV
- type Level
- type Logger
- type Option
- type Simple
- type StdLogger
- func (l *StdLogger) AddData(data KV)
- func (l *StdLogger) AddFields(fields map[string]interface{})
- func (l *StdLogger) AddTags(tags ...string)
- func (l *StdLogger) Dbg(msg string, tags []string, data map[string]interface{})
- func (l *StdLogger) Debug(msg string)
- func (l *StdLogger) DebugData(msg string, data map[string]interface{})
- func (l *StdLogger) Debugf(format string, args ...interface{})
- func (l *StdLogger) Dump(msg string, tags []string, data map[string]interface{})
- func (l *StdLogger) Error(msg string)
- func (l *StdLogger) ErrorData(msg string, data map[string]interface{})
- func (l *StdLogger) Errorf(format string, args ...interface{})
- func (l *StdLogger) Info(msg string)
- func (l *StdLogger) InfoData(msg string, data map[string]interface{})
- func (l *StdLogger) Infof(format string, args ...interface{})
- func (l *StdLogger) Log(msg string, tags []string)
- func (l *StdLogger) Logd(msg string, tags []string, data map[string]interface{})
- func (l *StdLogger) Print(args ...interface{})
- func (l *StdLogger) Printf(format string, args ...interface{})
- func (l *StdLogger) Println(args ...interface{})
- func (l *StdLogger) Reconfigure(opts ...Option)
- func (l *StdLogger) Spawn(opts ...Option) Logger
- func (l *StdLogger) SpawnCompatible(opts ...Option) Compatible
- func (l *StdLogger) SpawnSimple(opts ...Option) Simple
- func (l *StdLogger) Warn(msg string)
- func (l *StdLogger) WarnData(msg string, data map[string]interface{})
- func (l *StdLogger) Warnf(format string, args ...interface{})
- func (l *StdLogger) With(tags []string, fields map[string]interface{}) Entry
- func (l *StdLogger) WithData(data map[string]interface{}) Entry
- func (l *StdLogger) WithFields(fields map[string]interface{}) Entry
- func (l *StdLogger) WithTags(tags ...string) Entry
- func (l *StdLogger) Write(msg string, tags []string)
- type Tags
- type Writer
- type WriterOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func F ¶ added in v1.0.0
F is a wrapper for fmt.Sprintf, its use is not recommended. If you do use it, you are labeling yourself lazy.
func GetCallerInfo ¶ added in v1.0.0
GetCallerInfo is a helper for returning a string with "filename:line" of the call depth provided. Use this to add to a log field.
func Print ¶
func Print(args ...interface{})
Print for compatibility with Go's official log package.
func Printf ¶
func Printf(format string, args ...interface{})
Printf for compatibility with Go's official log package.
func Println ¶ added in v1.0.0
func Println(args ...interface{})
Println for compatibility with Go's official log package.
Types ¶
type Compatible ¶ added in v1.0.0
type Compatible interface { SpawnCompatible(...Option) Compatible Print(...interface{}) Println(...interface{}) Printf(string, ...interface{}) }
Compatible is a compatibility interface for the standard logger for dropin replacement of the standard logger.
func SpawnCompatible ¶ added in v1.0.0
func SpawnCompatible(opts ...Option) Compatible
SpawnCompatible creates a new logger which implements the Compatible interface.
func SpawnCompatibleMute ¶ added in v1.2.0
func SpawnCompatibleMute() Compatible
SpawnCompatibleMute creates a new logger which implements the Compatible interface but blocks all logging. This is simply a helper in order to inject loggers into components to keep them quiet.
type ConfigurableLogger ¶ added in v1.0.0
type ConfigurableLogger interface { Reconfigure(...Option) AddFields(map[string]interface{}) AddTags(...string) AddData(KV) }
ConfigurableLogger interface defines the interface for a logger which can be reconfigured.
type Entry ¶ added in v1.2.0
type Entry interface { // With method spawns a new Entry from the current one adding the provided tags and fields. With(tags []string, fields map[string]interface{}) Entry // WithTags updates a new Entry logger with the provided tags. WithTags(tags ...string) Entry // WithData returns a new Entry logger with the provided data. WithData(data map[string]interface{}) Entry // WithFields returns a new Entry logger with the provided fields. WithFields(fields map[string]interface{}) Entry Debug(msg string) Info(msg string) Warn(msg string) Error(msg string) Debugf(format string, args ...interface{}) Infof(format string, args ...interface{}) Warnf(format string, args ...interface{}) Errorf(format string, args ...interface{}) }
Entry is a special logger aimed at function/line scoped logging.
type Fields ¶ added in v1.0.0
type Fields = map[string]interface{}
Fields is a type alias for map[string]interface{}. @deprecated use KV instead.
type KV ¶ added in v1.2.0
type KV = map[string]interface{}
KV is a type alias for map[string]interface{}.
type Level ¶
type Level uint8
Level for defining log level integer values for granular filtering.
const ( // RAW log level should be used only when dumping large log entries or entire structures, use in development only. RAW Level // DEBUG log level should be used for development only and potentially as on/off in particular situations for gathering more information during tricky debugging situations. DEBUG // INFO is anything relevant for the log analysis/consumer. For example, informing that a database connection was established, a message was received, etc. This should be used for log analysis, metrics and basic profiling of bottlenecks. INFO // WARN log level should be used in situations where many entries of this type should trigger an allert in your centralized logging system. It only makes sense if you plan to use such a system architecture. WARN // ERROR log level should be used in all error situations. Careful with what you consider an error. Typically if you are handling the situation or returning the error to the caller, you should not log it as an ERROR. ERROR // OFF log level should be used only by SetLevel method to disable logging. OFF )
type Logger ¶
type Logger interface { Spawn(...Option) Logger With(tags []string, fields map[string]interface{}) Entry WithTags(tags ...string) Entry WithFields(fields map[string]interface{}) Entry WithData(data map[string]interface{}) Entry Debug(msg string) Info(msg string) Warn(msg string) Error(msg string) Debugf(format string, args ...interface{}) Infof(format string, args ...interface{}) Warnf(format string, args ...interface{}) Errorf(format string, args ...interface{}) // DebugData ... // @deprecated use WithData().Debug() instead DebugData(msg string, data map[string]interface{}) // InfoData ... // @deprecated use WithData().Info() instead InfoData(msg string, data map[string]interface{}) // WarnData ... // @deprecated use WithData().Warn() instead WarnData(msg string, data map[string]interface{}) // ErrorData ... // @deprecated use WithData().Error() instead ErrorData(msg string, data map[string]interface{}) }
Logger is a typical logger interface.
type Option ¶ added in v1.0.0
type Option func(ConfigurableLogger)
Option ...
func WithFields ¶ added in v1.0.0
WithFields ...
type Simple ¶ added in v1.0.0
type Simple interface { // Spawn creates a new logger with the provided configuration. Use this to change your default logger's fields and tags. SpawnSimple(...Option) Simple // Log writes a log line with the provided message and a list of tags. Use nil value for tags if you don't want to add any. Tags are appended to the logger's tags. There is no check for repeated tags. // @deprecated. Log(message string, tags []string) // Logd is similar to Log except it allows passing additional structured data in the form of map[string]interface{}. // @deprecated. Logd(message string, tags []string, data map[string]interface{}) // Write writes a new log entry with the provided message and tags. The tags parameter can be nil and no extra tags will be included in the log entry. Write(msg string, tags []string) // Dump is similar to Write but it also logs extra data. The data parameter can be nil which has the same effect as calling the Write method. Dump(msg string, tags []string, data map[string]interface{}) // Dbg is similar to Dump except it will not print anything if the minimum log level has been set to something higher than DEBUG. Note that the Simple logger itself will ignore any other minimum log level set. Dbg(message string, tags []string, data map[string]interface{}) }
Simple interface is the a basic yet powerful interface for a logger. It encourages the use of tags instead of log levels for more control in terms of log filtering and search. It discourages fancy printf, requiring the user to run fmt.Printf or logger.F for a slightly shorter version.
func SpawnSimple ¶ added in v1.1.1
SpawnSimple returns a new logger which implements the Simple interface.
func SpawnSimpleMute ¶ added in v1.2.0
func SpawnSimpleMute() Simple
SpawnSimpleMute creates a new logger which implements the Simple interface but blocks all logging. This is simply a helper in order to inject loggers into components to keep them quiet.
type StdLogger ¶ added in v1.0.0
type StdLogger struct {
// contains filtered or unexported fields
}
StdLogger ...
func (*StdLogger) Dbg ¶ added in v1.2.0
Dbg is similar to Dump except it will not print anything if the minimum log level has been set to something higher than DEBUG. Note that the Simple logger itself will ignore any other minimum log level set.
func (*StdLogger) Dump ¶ added in v1.2.0
Dump is similar to Write but it also logs extra data. The data parameter can be nil which has the same effect as calling the Write method.
func (*StdLogger) Println ¶ added in v1.0.0
func (l *StdLogger) Println(args ...interface{})
Println ...
func (*StdLogger) Reconfigure ¶ added in v1.0.0
Reconfigure ...
func (*StdLogger) Spawn ¶ added in v1.0.0
Spawn creates a new instance of a Logger using the parent Logger as origin.
func (*StdLogger) SpawnCompatible ¶ added in v1.0.0
func (l *StdLogger) SpawnCompatible(opts ...Option) Compatible
SpawnCompatible creates a new instance of a Logger using the parent Logger as origin.
func (*StdLogger) SpawnSimple ¶ added in v1.1.0
SpawnSimple creates a new instance of a Logger using the parent Logger as origin.
func (*StdLogger) With ¶ added in v1.2.0
With returns a new instance of an Entry logger with the provided tags and fields.
func (*StdLogger) WithData ¶ added in v1.2.0
WithData creates a new instance of an Entry logger with the provided data.
func (*StdLogger) WithFields ¶ added in v1.2.0
WithFields creates a new instance of an Entry logger with the provided fields.
type Writer ¶ added in v1.0.0
type Writer interface { Spawn(...Option) Logger SpawnSimple(...Option) Simple SpawnCompatible(...Option) Compatible Reconfigure(...WriterOption) Write(msg string) WriteData(msg string, data map[string]interface{}) }
Writer ...
func NewWriter ¶ added in v1.0.0
func NewWriter(opts ...WriterOption) Writer
NewWriter creates a new log Writer.
type WriterOption ¶ added in v1.0.0
type WriterOption func(*writer)
WriterOption are options for all loggers implementing the Basic logger interface.
func WithImmutableFields ¶ added in v1.0.0
func WithImmutableFields(fields map[string]interface{}) WriterOption
WithImmutableFields option, a writer has the provided fields replace its list of fields. A writer's fields can not be changed or overriden by a logger and at Spawn time are copied to the spawn.
func WithImmutableTags ¶ added in v1.0.0
func WithImmutableTags(tags ...string) WriterOption
WithImmutableTags option, a writer has the tags provided replace its list of tags. A writer's tags can not be changed or overriden by a logger and at Spawn time all tags are copied to the spawn.
func WithOutput ¶ added in v1.0.0
func WithOutput(w io.Writer) WriterOption
WithOutput allows setting the output writer for the logger.