github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/shared/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 defaultIsLevelEnabled(level LogLevel) bool {
    37  	return true
    38  }
    39  
    40  func defaultDebugLog(msg string, fields ...Field) {
    41  	defaultLog("debug", msg, fields...)
    42  }
    43  
    44  func defaultInfoLog(msg string, fields ...Field) {
    45  	defaultLog("info", msg, fields...)
    46  }
    47  
    48  func defaultWarnLog(msg string, fields ...Field) {
    49  	defaultLog("warn", msg, fields...)
    50  }
    51  
    52  func defaultErrorLog(msg string, fields ...Field) {
    53  	defaultLog("error", msg, fields...)
    54  }
    55  
    56  func defaultCriticalLog(msg string, fields ...Field) {
    57  	// We map critical to error in zap, so be consistent.
    58  	defaultLog("error", msg, fields...)
    59  }
    60  
    61  func defaultCustomLog(lvl LogLevel, msg string, fields ...Field) {
    62  	// custom log levels are only output once log targets are configured.
    63  }
    64  
    65  func defaultCustomMultiLog(lvl []LogLevel, msg string, fields ...Field) {
    66  	// custom log levels are only output once log targets are configured.
    67  }
    68  
    69  func defaultFlush(ctx context.Context) error {
    70  	return nil
    71  }
    72  
    73  func defaultAdvancedConfig(cfg LogTargetCfg) error {
    74  	// mlog.ConfigAdvancedConfig should not be called until default
    75  	// logger is replaced with mlog.Logger instance.
    76  	return errors.New("cannot config advanced logging on default logger")
    77  }
    78  
    79  func defaultAdvancedShutdown(ctx context.Context) error {
    80  	return nil
    81  }
    82  
    83  func defaultAddTarget(targets ...logr.Target) error {
    84  	// mlog.AddTarget should not be called until default
    85  	// logger is replaced with mlog.Logger instance.
    86  	return errors.New("cannot AddTarget on default logger")
    87  }
    88  
    89  func defaultRemoveTargets(ctx context.Context, f func(TargetInfo) bool) error {
    90  	// mlog.RemoveTargets should not be called until default
    91  	// logger is replaced with mlog.Logger instance.
    92  	return errors.New("cannot RemoveTargets on default logger")
    93  }
    94  
    95  func defaultEnableMetrics(collector logr.MetricsCollector) error {
    96  	// mlog.EnableMetrics should not be called until default
    97  	// logger is replaced with mlog.Logger instance.
    98  	return errors.New("cannot EnableMetrics on default logger")
    99  }