You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
9.9 KiB

3 years ago
package log
import (
"context"
3 years ago
"errors"
3 years ago
"fmt"
3 years ago
"io"
"os"
"gitoa.ru/go-4devs/log/entry"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/level"
)
var _ io.Writer = (Logger)(nil)
var (
ErrIgnoredKey = errors.New("ignored key without a value")
ErrNonStringKeys = errors.New("ignored key-value pairs with non-string keys")
3 years ago
)
3 years ago
func writeOutput(_ int, err error) {
if err != nil {
fmt.Fprint(os.Stderr, err)
}
}
3 years ago
// Logger logged message.
3 years ago
type Logger func(ctx context.Context, entry *entry.Entry) (int, error)
3 years ago
3 years ago
func (l Logger) log(ctx context.Context, level level.Level, args ...interface{}) {
writeOutput(l.write(ctx, level, fmt.Sprint(args...)))
3 years ago
}
3 years ago
func (l Logger) Write(in []byte) (int, error) {
return l.write(context.Background(), level.Info, string(in))
3 years ago
}
3 years ago
func (l Logger) write(ctx context.Context, level level.Level, msg string, fields ...field.Field) (int, error) {
3 years ago
writeEntry := entry.Get()
3 years ago
defer func() {
3 years ago
entry.Put(writeEntry)
3 years ago
}()
3 years ago
return l(ctx, writeEntry.SetLevel(level).SetMessage(msg).Add(fields...))
3 years ago
}
3 years ago
func (l Logger) logKVs(ctx context.Context, level level.Level, msg string, args ...interface{}) {
writeOutput(l.write(ctx, level, msg, l.kv(ctx, args...)...))
3 years ago
}
3 years ago
func (l Logger) logKV(ctx context.Context, level level.Level, msg string, fields ...field.Field) {
writeOutput(l.write(ctx, level, msg, fields...))
}
func (l Logger) logf(ctx context.Context, level level.Level, format string, args ...interface{}) {
writeOutput(l.write(ctx, level, fmt.Sprintf(format, args...)))
}
func (l Logger) logln(ctx context.Context, level level.Level, args ...interface{}) {
writeOutput(l.write(ctx, level, fmt.Sprintln(args...)))
}
func (l Logger) kv(ctx context.Context, args ...interface{}) field.Fields {
3 years ago
kvEntry := entry.Get()
3 years ago
defer func() {
3 years ago
entry.Put(kvEntry)
3 years ago
}()
3 years ago
for i := 0; i < len(args); i++ {
3 years ago
if f, ok := args[i].(field.Field); ok {
3 years ago
kvEntry = kvEntry.Add(f)
3 years ago
3 years ago
continue
}
if i == len(args)-1 {
3 years ago
l.logKV(ctx, level.Critical, fmt.Sprint("Ignored key without a value.", args[i]), kvEntry.Fields()...)
3 years ago
3 years ago
break
}
i++
key, val := args[i-1], args[i]
if keyStr, ok := key.(string); ok {
3 years ago
kvEntry = kvEntry.AddAny(keyStr, val)
3 years ago
3 years ago
continue
}
3 years ago
l.logKV(ctx, level.Critical, fmt.Sprint("Ignored key-value pairs with non-string keys.", key, val), kvEntry.Fields()...)
3 years ago
}
3 years ago
return kvEntry.Fields()
3 years ago
}
// With adds middlewares to logger.
func (l Logger) With(mw ...Middleware) Logger {
return With(l, mw...)
}
// Emerg log by emergency level.
func (l Logger) Emerg(ctx context.Context, args ...interface{}) {
3 years ago
l.log(ctx, level.Emergency, args...)
3 years ago
}
// Alert log by alert level.
func (l Logger) Alert(ctx context.Context, args ...interface{}) {
3 years ago
l.log(ctx, level.Alert, args...)
3 years ago
}
// Crit log by critical level.
func (l Logger) Crit(ctx context.Context, args ...interface{}) {
3 years ago
l.log(ctx, level.Critical, args...)
3 years ago
}
// Err log by error level.
func (l Logger) Err(ctx context.Context, args ...interface{}) {
3 years ago
l.log(ctx, level.Error, args...)
3 years ago
}
// Warn log by warning level.
func (l Logger) Warn(ctx context.Context, args ...interface{}) {
3 years ago
l.log(ctx, level.Warning, args...)
3 years ago
}
// Notice log by notice level.
func (l Logger) Notice(ctx context.Context, args ...interface{}) {
3 years ago
l.log(ctx, level.Notice, args...)
3 years ago
}
// Info log by info level.
func (l Logger) Info(ctx context.Context, args ...interface{}) {
3 years ago
l.log(ctx, level.Info, args...)
3 years ago
}
// Debug log by debug level.
func (l Logger) Debug(ctx context.Context, args ...interface{}) {
3 years ago
l.log(ctx, level.Debug, args...)
3 years ago
}
// Print log by info level and arguments.
func (l Logger) Print(args ...interface{}) {
3 years ago
l.log(context.Background(), level.Info, args...)
3 years ago
}
// Fatal log by alert level and arguments.
func (l Logger) Fatal(args ...interface{}) {
3 years ago
l.log(context.Background(), level.Alert, args...)
3 years ago
}
// Panic log by emergency level and arguments.
func (l Logger) Panic(args ...interface{}) {
3 years ago
l.log(context.Background(), level.Emergency, args...)
3 years ago
}
// Println log by info level and arguments.
func (l Logger) Println(args ...interface{}) {
3 years ago
l.logln(context.Background(), level.Info, args...)
3 years ago
}
// Fatalln log by alert level and arguments.
func (l Logger) Fatalln(args ...interface{}) {
3 years ago
l.logln(context.Background(), level.Alert, args...)
3 years ago
}
// Panicln log by emergency level and arguments.
func (l Logger) Panicln(args ...interface{}) {
3 years ago
l.logln(context.Background(), level.Emergency, args...)
}
// EmergKVs sugared log by emergency level and key-values.
func (l Logger) EmergKVs(ctx context.Context, msg string, args ...interface{}) {
l.logKVs(ctx, level.Emergency, msg, args...)
}
// AlertKVs sugared log by alert level and key-values.
func (l Logger) AlertKVs(ctx context.Context, msg string, args ...interface{}) {
l.logKVs(ctx, level.Alert, msg, args...)
}
// CritKVs sugared log by critcal level and key-values.
func (l Logger) CritKVs(ctx context.Context, msg string, args ...interface{}) {
l.logKVs(ctx, level.Critical, msg, args...)
}
// ErrKVs sugared log by error level and key-values.
func (l Logger) ErrKVs(ctx context.Context, msg string, args ...interface{}) {
l.logKVs(ctx, level.Error, msg, args...)
}
// WarnKVs sugared log by warning level and key-values.
func (l Logger) WarnKVs(ctx context.Context, msg string, args ...interface{}) {
l.logKVs(ctx, level.Warning, msg, args...)
}
// NoticeKVs sugared log by notice level and key-values.
func (l Logger) NoticeKVs(ctx context.Context, msg string, args ...interface{}) {
l.logKVs(ctx, level.Notice, msg, args...)
}
// InfoKVs sugared log by info level and key-values.
func (l Logger) InfoKVs(ctx context.Context, msg string, args ...interface{}) {
l.logKVs(ctx, level.Info, msg, args...)
}
// DebugKVs sugared log by debug level and key-values.
func (l Logger) DebugKVs(ctx context.Context, msg string, args ...interface{}) {
l.logKVs(ctx, level.Debug, msg, args...)
3 years ago
}
// EmergKV log by emergency level and key-values.
3 years ago
func (l Logger) EmergKV(ctx context.Context, msg string, args ...field.Field) {
l.logKV(ctx, level.Emergency, msg, args...)
3 years ago
}
// AlertKV log by alert level and key-values.
3 years ago
func (l Logger) AlertKV(ctx context.Context, msg string, args ...field.Field) {
l.logKV(ctx, level.Alert, msg, args...)
3 years ago
}
// CritKV log by critcal level and key-values.
3 years ago
func (l Logger) CritKV(ctx context.Context, msg string, args ...field.Field) {
l.logKV(ctx, level.Critical, msg, args...)
3 years ago
}
// ErrKV log by error level and key-values.
3 years ago
func (l Logger) ErrKV(ctx context.Context, msg string, args ...field.Field) {
l.logKV(ctx, level.Error, msg, args...)
3 years ago
}
// WarnKV log by warning level and key-values.
3 years ago
func (l Logger) WarnKV(ctx context.Context, msg string, args ...field.Field) {
l.logKV(ctx, level.Warning, msg, args...)
3 years ago
}
// NoticeKV log by notice level and key-values.
3 years ago
func (l Logger) NoticeKV(ctx context.Context, msg string, args ...field.Field) {
l.logKV(ctx, level.Notice, msg, args...)
3 years ago
}
// InfoKV log by info level and key-values.
3 years ago
func (l Logger) InfoKV(ctx context.Context, msg string, args ...field.Field) {
l.logKV(ctx, level.Info, msg, args...)
3 years ago
}
// DebugKV log by debug level and key-values.
3 years ago
func (l Logger) DebugKV(ctx context.Context, msg string, args ...field.Field) {
l.logKV(ctx, level.Debug, msg, args...)
3 years ago
}
// Emergf log by emergency level by format and arguments.
func (l Logger) Emergf(ctx context.Context, format string, args ...interface{}) {
3 years ago
l.logf(ctx, level.Emergency, format, args...)
3 years ago
}
// Alertf log by alert level by format and arguments.
func (l Logger) Alertf(ctx context.Context, format string, args ...interface{}) {
3 years ago
l.logf(ctx, level.Alert, format, args...)
3 years ago
}
// Critf log by critical level by format and arguments.
func (l Logger) Critf(ctx context.Context, format string, args ...interface{}) {
3 years ago
l.logf(ctx, level.Critical, format, args...)
3 years ago
}
// Errf log by error level by format and arguments.
func (l Logger) Errf(ctx context.Context, format string, args ...interface{}) {
3 years ago
l.logf(ctx, level.Error, format, args...)
3 years ago
}
// Warnf log by warning level by format and arguments.
func (l Logger) Warnf(ctx context.Context, format string, args ...interface{}) {
3 years ago
l.logf(ctx, level.Warning, format, args...)
3 years ago
}
// Noticef log by notice level by format and arguments.
func (l Logger) Noticef(ctx context.Context, format string, args ...interface{}) {
3 years ago
l.logf(ctx, level.Notice, format, args...)
3 years ago
}
// Infof log by info level by format and arguments.
func (l Logger) Infof(ctx context.Context, format string, args ...interface{}) {
3 years ago
l.logf(ctx, level.Info, format, args...)
3 years ago
}
// Debugf log by debug level by format and arguments.
func (l Logger) Debugf(ctx context.Context, format string, args ...interface{}) {
3 years ago
l.logf(ctx, level.Debug, format, args...)
3 years ago
}
// Printf log by info level by format and arguments without context.
func (l Logger) Printf(format string, args ...interface{}) {
3 years ago
l.logf(context.Background(), level.Info, format, args...)
3 years ago
}
// Fatalf log by alert level by format and arguments without context.
func (l Logger) Fatalf(format string, args ...interface{}) {
3 years ago
l.logf(context.Background(), level.Alert, format, args...)
3 years ago
}
// Panicf log by emergency level and arguments without context.
func (l Logger) Panicf(format string, args ...interface{}) {
3 years ago
l.logf(context.Background(), level.Emergency, format, args...)
}
func (l Logger) Writer(ctx context.Context, level level.Level, fields ...field.Field) io.Writer {
return writer{
ctx: ctx,
level: level,
Logger: l,
fields: fields,
}
}
3 years ago
//nolint: containedctx
3 years ago
type writer struct {
ctx context.Context
level level.Level
fields []field.Field
Logger
}
func (w writer) WithLevel(level level.Level) writer {
return writer{
level: level,
Logger: w.Logger,
ctx: w.ctx,
fields: w.fields,
}
}
func (w writer) WithContext(ctx context.Context) writer {
return writer{
level: w.level,
Logger: w.Logger,
ctx: ctx,
fields: w.fields,
}
}
func (w writer) WithFields(fields ...field.Field) writer {
return writer{
level: w.level,
Logger: w.Logger,
ctx: w.ctx,
fields: fields,
}
}
3 years ago
func (w writer) Write(in []byte) (int, error) {
return w.write(w.ctx, w.level, string(in), w.fields...)
3 years ago
}