Documentation ¶
Index ¶
- Constants
- Variables
- func CancelRedirectStdLog()
- func InitWithConfigContent(content string) error
- func RedirectStdLog() func()
- func SetLevel(name string, level zapcore.Level, appender ...string)
- func Sync()
- func ToCtx(ctx context.Context, logger Logger) context.Context
- type Field
- func Any(key string, value interface{}) Field
- func Binary(key string, val []byte) Field
- func Bool(key string, val bool) Field
- func Boolp(key string, val *bool) Field
- func ByteString(key string, val []byte) Field
- func Complex128(key string, val complex128) Field
- func Complex128p(key string, val *complex128) Field
- func Complex64(key string, val complex64) Field
- func Complex64p(key string, val *complex64) Field
- func Duration(key string, val time.Duration) Field
- func Durationp(key string, val *time.Duration) Field
- func Error(err error) Field
- func Errors(key string, errs []error) Field
- func Float32(key string, val float32) Field
- func Float32p(key string, val *float32) Field
- func Float64(key string, val float64) Field
- func Float64p(key string, val *float64) Field
- func Int(key string, val int) Field
- func Int16(key string, val int16) Field
- func Int16p(key string, val *int16) Field
- func Int32(key string, val int32) Field
- func Int32p(key string, val *int32) Field
- func Int64(key string, val int64) Field
- func Int64p(key string, val *int64) Field
- func Int8(key string, val int8) Field
- func Int8p(key string, val *int8) Field
- func Intp(key string, val *int) Field
- func NamedError(key string, err error) Field
- func Namespace(key string) Field
- func Object(key string, val zapcore.ObjectMarshaler) Field
- func Reflect(key string, val interface{}) Field
- func Skip() Field
- func Stack(key string) Field
- func StackSkip(key string, skip int) Field
- func String(key string, val string) Field
- func Stringer(key string, val fmt.Stringer) Field
- func Stringp(key string, val *string) Field
- func Time(key string, val time.Time) Field
- func Timep(key string, val *time.Time) Field
- func Uint(key string, val uint) Field
- func Uint16(key string, val uint16) Field
- func Uint16p(key string, val *uint16) Field
- func Uint32(key string, val uint32) Field
- func Uint32p(key string, val *uint32) Field
- func Uint64(key string, val uint64) Field
- func Uint64p(key string, val *uint64) Field
- func Uint8(key string, val uint8) Field
- func Uint8p(key string, val *uint8) Field
- func Uintp(key string, val *uint) Field
- func Uintptr(key string, val uintptr) Field
- func Uintptrp(key string, val *uintptr) Field
- type Logger
- type SugaredLogger
Examples ¶
Constants ¶
const ( // OffLevel OffLevel = zapcore.Level(-2) // DebugLevel logs are typically voluminous, and are usually disabled in // production. DebugLevel = zapcore.DebugLevel // InfoLevel is the default logging priority. InfoLevel = zapcore.InfoLevel // WarnLevel logs are more important than Info, but don't need individual // human review. WarnLevel = zapcore.WarnLevel // ErrorLevel logs are high-priority. If an application is running smoothly, // it shouldn't generate any error-level logs. ErrorLevel = zapcore.ErrorLevel // DPanicLevel logs are particularly important errors. In development the // logger panics after writing the message. DPanicLevel = zapcore.DPanicLevel // PanicLevel logs a message, then panics. PanicLevel = zapcore.PanicLevel // FatalLevel logs a message, then calls os.Exit(1). FatalLevel = zapcore.FatalLevel )
Variables ¶
var (
ErrEnvConfigNotSet = errors.New("environment variable 'LOGOS_CONFIG' is not set")
)
var StackTraceLevelEnabler = zap.NewAtomicLevelAt(zapcore.PanicLevel)
Functions ¶
func CancelRedirectStdLog ¶ added in v0.9.0
func CancelRedirectStdLog()
func InitWithConfigContent ¶
func RedirectStdLog ¶ added in v0.9.0
func RedirectStdLog() func()
Types ¶
type Field ¶
Field is an alias for Field. Aliasing this type dramatically improves the navigability of this package's API documentation.
func Any ¶
Any takes a key and an arbitrary value and chooses the best way to represent them as a field, falling back to a reflection-based approach only if necessary.
Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between them. To minimize surprises, []byte values are treated as binary blobs, byte values are treated as uint8, and runes are always treated as integers.
func Binary ¶
Binary constructs a field that carries an opaque binary blob.
Binary data is serialized in an encoding-appropriate format. For example, zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text, use ByteString.
func Boolp ¶
Boolp constructs a field that carries a *bool. The returned Field will safely and explicitly represent `nil` when appropriate.
func ByteString ¶
ByteString constructs a field that carries UTF-8 encoded text as a []byte. To log opaque binary blobs (which aren't necessarily valid UTF-8), use Binary.
func Complex128 ¶
func Complex128(key string, val complex128) Field
Complex128 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex128 to interface{}).
func Complex128p ¶
func Complex128p(key string, val *complex128) Field
Complex128p constructs a field that carries a *complex128. The returned Field will safely and explicitly represent `nil` when appropriate.
func Complex64 ¶
Complex64 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex64 to interface{}).
func Complex64p ¶
Complex64p constructs a field that carries a *complex64. The returned Field will safely and explicitly represent `nil` when appropriate.
func Duration ¶
Duration constructs a field with the given key and value. The encoder controls how the duration is serialized.
func Durationp ¶
Durationp constructs a field that carries a *time.Duration. The returned Field will safely and explicitly represent `nil` when appropriate.
func Float32 ¶
Float32 constructs a field that carries a float32. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.
func Float32p ¶
Float32p constructs a field that carries a *float32. The returned Field will safely and explicitly represent `nil` when appropriate.
func Float64 ¶
Float64 constructs a field that carries a float64. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.
func Float64p ¶
Float64p constructs a field that carries a *float64. The returned Field will safely and explicitly represent `nil` when appropriate.
func Int16p ¶
Int16p constructs a field that carries a *int16. The returned Field will safely and explicitly represent `nil` when appropriate.
func Int32p ¶
Int32p constructs a field that carries a *int32. The returned Field will safely and explicitly represent `nil` when appropriate.
func Int64p ¶
Int64p constructs a field that carries a *int64. The returned Field will safely and explicitly represent `nil` when appropriate.
func Int8p ¶
Int8p constructs a field that carries a *int8. The returned Field will safely and explicitly represent `nil` when appropriate.
func Intp ¶
Intp constructs a field that carries a *int. The returned Field will safely and explicitly represent `nil` when appropriate.
func NamedError ¶
NamedError constructs a field that lazily stores err.Error() under the provided key. Errors which also implement fmt.Formatter (like those produced by github.com/pkg/errors) will also have their verbose representation stored under key+"Verbose". If passed a nil error, the field is a no-op.
For the common case in which the key is simply "error", the Error function is shorter and less repetitive.
func Namespace ¶
Namespace creates a named, isolated scope within the logger's context. All subsequent fields will be added to the new namespace.
This helps prevent key collisions when injecting loggers into sub-components or third-party libraries.
func Object ¶
func Object(key string, val zapcore.ObjectMarshaler) Field
Object constructs a field with the given key and ObjectMarshaler. It provides a flexible, but still type-safe and efficient, way to add map- or struct-like user-defined types to the logging context. The struct's MarshalLogObject method is called lazily.
func Reflect ¶
Reflect constructs a field with the given key and an arbitrary object. It uses an encoding-appropriate, reflection-based function to lazily serialize nearly any object into the logging context, but it's relatively slow and allocation-heavy. Outside tests, Any is always a better choice.
If encoding fails (e.g., trying to serialize a map[int]string to JSON), Reflect includes the error message in the final log output.
func Skip ¶
func Skip() Field
Skip constructs a no-op field, which is often useful when handling invalid inputs in other Field constructors.
func Stack ¶
Stack constructs a field that stores a stacktrace of the current goroutine under provided key. Keep in mind that taking a stacktrace is eager and expensive (relatively speaking); this function both makes an allocation and takes about two microseconds.
func StackSkip ¶
StackSkip constructs a field similarly to Stack, but also skips the given number of frames from the top of the stacktrace.
func Stringer ¶
Stringer constructs a field with the given key and the output of the value's String method. The Stringer's String method is called lazily.
func Stringp ¶
Stringp constructs a field that carries a *string. The returned Field will safely and explicitly represent `nil` when appropriate.
func Time ¶
Time constructs a Field with the given key and value. The encoder controls how the time is serialized.
func Timep ¶
Timep constructs a field that carries a *time.Time. The returned Field will safely and explicitly represent `nil` when appropriate.
func Uint16p ¶
Uint16p constructs a field that carries a *uint16. The returned Field will safely and explicitly represent `nil` when appropriate.
func Uint32p ¶
Uint32p constructs a field that carries a *uint32. The returned Field will safely and explicitly represent `nil` when appropriate.
func Uint64p ¶
Uint64p constructs a field that carries a *uint64. The returned Field will safely and explicitly represent `nil` when appropriate.
func Uint8p ¶
Uint8p constructs a field that carries a *uint8. The returned Field will safely and explicitly represent `nil` when appropriate.
func Uintp ¶
Uintp constructs a field that carries a *uint. The returned Field will safely and explicitly represent `nil` when appropriate.
type Logger ¶
type Logger interface { Named(s string) Logger With(fields ...Field) Logger Sync() error Sugar() SugaredLogger // contains filtered or unexported methods }
func New ¶
Example (Simple) ¶
package main import ( "github.com/khorevaa/logos" log2 "log" ) func main() { rawConfig := ` appenders: console: - name: CONSOLE target: stdout no_color: true encoder: console: disable_timestamp: true color_scheme: info_level: blue+b debug_level: green+b loggers: root: level: debug appender_refs: - CONSOLE ` err := logos.InitWithConfigContent(rawConfig) if err != nil { panic(err) } //logos.CancelRedirectStdLog() log2.Println("1") log := logos.New("<your-package-name>") // like github.com/khorevaa/logos log.Info("This is me first log. Hello world logging systems") //cancel() //log2.Println("2") }
Output: INFO stdlog 1 INFO <your-package-name> This is me first log. Hello world logging systems
Example (With_color_scheme) ¶
package main import ( "errors" "github.com/khorevaa/logos" ) func main() { rawConfig := ` appenders: console: - name: CONSOLE target: stdout no_color: true encoder: console: disable_timestamp: true color_scheme: info_level: blue+b debug_level: green+b loggers: root: level: debug appender_refs: - CONSOLE ` logos.InitWithConfigContent(rawConfig) log := logos.New("<your-package-name>") // like github.com/khorevaa/logos log.Info("This is me first log. Hello world logging systems") err := errors.New("log system error") log.Debug("This is me first error", logos.Any("err", err)) }
Output: INFO <your-package-name> This is me first log. Hello world logging systems DEBUG <your-package-name> This is me first error err=log system error
Example (With_config_json) ¶
package main import ( "github.com/khorevaa/logos" ) func main() { rawConfig := ` appenders: console: - name: CONSOLE target: stdout encoder: json: loggers: root: level: info appender_refs: - CONSOLE ` logos.InitWithConfigContent(rawConfig) log := logos.New("<your-package-name>") // like github.com/khorevaa/logos log.Info("This is me first log. Hello world logging systems") }
Output:
type SugaredLogger ¶
type SugaredLogger interface { // Debugw logs a message with some additional context. The additional context // is added in the form of key-value pairs. The optimal way to write the value // to the log message will be inferred by the value's type. To explicitly // specify a type you can pass a Field such as logos.Stringer. Debugw(msg string, keysAndValues ...interface{}) // Infow logs a message with some additional context. The additional context // is added in the form of key-value pairs. The optimal way to write the value // to the log message will be inferred by the value's type. To explicitly // specify a type you can pass a Field such as logos.Stringer. Infow(msg string, keysAndValues ...interface{}) // Warnw logs a message with some additional context. The additional context // is added in the form of key-value pairs. The optimal way to write the value // to the log message will be inferred by the value's type. To explicitly // specify a type you can pass a Field such as logos.Stringer. Warnw(msg string, keysAndValues ...interface{}) // Errorw logs a message with some additional context. The additional context // is added in the form of key-value pairs. The optimal way to write the value // to the log message will be inferred by the value's type. To explicitly // specify a type you can pass a Field such as logos.Stringer. Errorw(msg string, keysAndValues ...interface{}) // Fatalw logs a message with some additional context, then calls os.Exit(1). // The additional context is added in the form of key-value pairs. The optimal // way to write the value to the log message will be inferred by the value's // type. To explicitly specify a type you can pass a Field such as // logos.Stringer. Fatalw(msg string, keysAndValues ...interface{}) // Panicw logs a message with some additional context, then panics. The // additional context is added in the form of key-value pairs. The optimal way // to write the value to the log message will be inferred by the value's type. // To explicitly specify a type you can pass a Field such as logos.Stringer. Panicw(msg string, keysAndValues ...interface{}) // DPanicw logs a message with some additional context. The logger panics only // in Development mode. The additional context is added in the form of // key-value pairs. The optimal way to write the value to the log message will // be inferred by the value's type. To explicitly specify a type you can pass a // Field such as logos.Stringer. DPanicw(msg string, keysAndValues ...interface{}) Sync() error Desugar() Logger // contains filtered or unexported methods }