github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/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  	"encoding/json"
     8  	"fmt"
     9  )
    10  
    11  // defaultLog manually encodes the log to STDOUT, providing a basic, default logging implementation
    12  // before mlog is fully configured.
    13  func defaultLog(level, msg string, fields ...Field) {
    14  	log := struct {
    15  		Level   string  `json:"level"`
    16  		Message string  `json:"msg"`
    17  		Fields  []Field `json:"fields,omitempty"`
    18  	}{
    19  		level,
    20  		msg,
    21  		fields,
    22  	}
    23  
    24  	if b, err := json.Marshal(log); err != nil {
    25  		fmt.Printf(`{"level":"error","msg":"failed to encode log message"}%s`, "\n")
    26  	} else {
    27  		fmt.Printf("%s\n", b)
    28  	}
    29  }
    30  
    31  func defaultDebugLog(msg string, fields ...Field) {
    32  	defaultLog("debug", msg, fields...)
    33  }
    34  
    35  func defaultInfoLog(msg string, fields ...Field) {
    36  	defaultLog("info", msg, fields...)
    37  }
    38  
    39  func defaultWarnLog(msg string, fields ...Field) {
    40  	defaultLog("warn", msg, fields...)
    41  }
    42  
    43  func defaultErrorLog(msg string, fields ...Field) {
    44  	defaultLog("error", msg, fields...)
    45  }
    46  
    47  func defaultCriticalLog(msg string, fields ...Field) {
    48  	// We map critical to error in zap, so be consistent.
    49  	defaultLog("error", msg, fields...)
    50  }