github.com/adacta-ru/mattermost-server/v5@v5.31.1/mlog/default.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package mlog 5 6 import ( 7 "context" 8 "encoding/json" 9 "errors" 10 "fmt" 11 "os" 12 13 "github.com/mattermost/logr" 14 ) 15 16 // defaultLog manually encodes the log to STDERR, providing a basic, default logging implementation 17 // before mlog is fully configured. 18 func defaultLog(level, msg string, fields ...Field) { 19 log := struct { 20 Level string `json:"level"` 21 Message string `json:"msg"` 22 Fields []Field `json:"fields,omitempty"` 23 }{ 24 level, 25 msg, 26 fields, 27 } 28 29 if b, err := json.Marshal(log); err != nil { 30 fmt.Fprintf(os.Stderr, `{"level":"error","msg":"failed to encode log message"}%s`, "\n") 31 } else { 32 fmt.Fprintf(os.Stderr, "%s\n", b) 33 } 34 } 35 36 func defaultDebugLog(msg string, fields ...Field) { 37 defaultLog("debug", msg, fields...) 38 } 39 40 func defaultInfoLog(msg string, fields ...Field) { 41 defaultLog("info", msg, fields...) 42 } 43 44 func defaultWarnLog(msg string, fields ...Field) { 45 defaultLog("warn", msg, fields...) 46 } 47 48 func defaultErrorLog(msg string, fields ...Field) { 49 defaultLog("error", msg, fields...) 50 } 51 52 func defaultCriticalLog(msg string, fields ...Field) { 53 // We map critical to error in zap, so be consistent. 54 defaultLog("error", msg, fields...) 55 } 56 57 func defaultCustomLog(lvl LogLevel, msg string, fields ...Field) { 58 // custom log levels are only output once log targets are configured. 59 } 60 61 func defaultCustomMultiLog(lvl []LogLevel, msg string, fields ...Field) { 62 // custom log levels are only output once log targets are configured. 63 } 64 65 func defaultFlush(ctx context.Context) error { 66 return nil 67 } 68 69 func defaultAdvancedConfig(cfg LogTargetCfg) error { 70 // mlog.ConfigAdvancedConfig should not be called until default 71 // logger is replaced with mlog.Logger instance. 72 return errors.New("cannot config advanced logging on default logger") 73 } 74 75 func defaultAdvancedShutdown(ctx context.Context) error { 76 return nil 77 } 78 79 func defaultAddTarget(targets ...logr.Target) error { 80 // mlog.AddTarget should not be called until default 81 // logger is replaced with mlog.Logger instance. 82 return errors.New("cannot AddTarget on default logger") 83 } 84 85 func defaultRemoveTargets(ctx context.Context, f func(TargetInfo) bool) error { 86 // mlog.RemoveTargets should not be called until default 87 // logger is replaced with mlog.Logger instance. 88 return errors.New("cannot RemoveTargets on default logger") 89 } 90 91 func defaultEnableMetrics(collector logr.MetricsCollector) error { 92 // mlog.EnableMetrics should not be called until default 93 // logger is replaced with mlog.Logger instance. 94 return errors.New("cannot EnableMetrics on default logger") 95 }