summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/mattermost-server/v6/shared/mlog/default.go
blob: 0567c0164853f7148ed633d9998a97261a603121 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package mlog

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"
)

// defaultLog manually encodes the log to STDERR, providing a basic, default logging implementation
// before mlog is fully configured.
func defaultLog(level Level, msg string, fields ...Field) {
	mFields := make(map[string]string)
	buf := &bytes.Buffer{}

	for _, fld := range fields {
		buf.Reset()
		fld.ValueString(buf, shouldQuote)
		mFields[fld.Key] = buf.String()
	}

	log := struct {
		Level   string            `json:"level"`
		Message string            `json:"msg"`
		Fields  map[string]string `json:"fields,omitempty"`
	}{
		level.Name,
		msg,
		mFields,
	}

	if b, err := json.Marshal(log); err != nil {
		fmt.Fprintf(os.Stderr, `{"level":"error","msg":"failed to encode log message"}%s`, "\n")
	} else {
		fmt.Fprintf(os.Stderr, "%s\n", b)
	}
}

func defaultIsLevelEnabled(level Level) bool {
	return true
}

func defaultCustomMultiLog(lvl []Level, msg string, fields ...Field) {
	for _, level := range lvl {
		defaultLog(level, msg, fields...)
	}
}

// shouldQuote returns true if val contains any characters that require quotations.
func shouldQuote(val string) bool {
	for _, c := range val {
		if !((c >= '0' && c <= '9') ||
			(c >= 'a' && c <= 'z') ||
			(c >= 'A' && c <= 'Z') ||
			c == '-' || c == '.' || c == '_' || c == '/' || c == '@' || c == '^' || c == '+') {
			return true
		}
	}
	return false
}