forgejo/vendor/github.com/couchbase/goutils/logging/logger_golog.go

274 lines
6.5 KiB
Go
Raw Normal View History

// Copyright (c) 2016-2019 Couchbase, Inc.
2019-02-05 17:52:51 +01:00
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package logging
import (
"encoding/json"
"fmt"
"io"
"log"
2021-03-01 00:08:33 +01:00
"strings"
2019-02-05 17:52:51 +01:00
"time"
)
type goLogger struct {
logger *log.Logger
level Level
entryFormatter formatter
}
const (
_LEVEL = "_level"
_MSG = "_msg"
_TIME = "_time"
_RLEVEL = "_rlevel"
)
func NewLogger(out io.Writer, lvl Level, fmtLogging LogEntryFormatter, fmtArgs ...interface{}) *goLogger {
2019-02-05 17:52:51 +01:00
logger := &goLogger{
logger: log.New(out, "", 0),
level: lvl,
}
if fmtLogging == JSONFORMATTER {
logger.entryFormatter = &jsonFormatter{}
} else if fmtLogging == KVFORMATTER {
logger.entryFormatter = &keyvalueFormatter{}
} else if fmtLogging == UNIFORMFORMATTER {
logger.entryFormatter = &uniformFormatter{
callback: fmtArgs[0].(ComponentCallback),
}
2019-02-05 17:52:51 +01:00
} else {
logger.entryFormatter = &textFormatter{}
}
return logger
}
2021-03-01 00:08:33 +01:00
// anonymous function variants
2019-02-05 17:52:51 +01:00
2021-03-01 00:08:33 +01:00
func (gl *goLogger) Loga(level Level, f func() string) {
2019-02-05 17:52:51 +01:00
if gl.logger == nil {
return
}
if level <= gl.level {
2021-03-01 00:08:33 +01:00
gl.log(level, NONE, f())
2019-02-05 17:52:51 +01:00
}
}
2021-03-01 00:08:33 +01:00
func (gl *goLogger) Debuga(f func() string) {
gl.Loga(DEBUG, f)
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
func (gl *goLogger) Tracea(f func() string) {
gl.Loga(TRACE, f)
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
func (gl *goLogger) Requesta(rlevel Level, f func() string) {
2019-02-05 17:52:51 +01:00
if gl.logger == nil {
return
}
if REQUEST <= gl.level {
2021-03-01 00:08:33 +01:00
gl.log(REQUEST, rlevel, f())
2019-02-05 17:52:51 +01:00
}
}
2021-03-01 00:08:33 +01:00
func (gl *goLogger) Infoa(f func() string) {
gl.Loga(INFO, f)
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
func (gl *goLogger) Warna(f func() string) {
gl.Loga(WARN, f)
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
func (gl *goLogger) Errora(f func() string) {
gl.Loga(ERROR, f)
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
func (gl *goLogger) Severea(f func() string) {
gl.Loga(SEVERE, f)
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
func (gl *goLogger) Fatala(f func() string) {
gl.Loga(FATAL, f)
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
// printf-style variants
2019-02-05 17:52:51 +01:00
func (gl *goLogger) Logf(level Level, format string, args ...interface{}) {
if gl.logger == nil {
return
}
if level <= gl.level {
2021-03-01 00:08:33 +01:00
gl.log(level, NONE, fmt.Sprintf(format, args...))
2019-02-05 17:52:51 +01:00
}
}
func (gl *goLogger) Debugf(format string, args ...interface{}) {
gl.Logf(DEBUG, format, args...)
}
func (gl *goLogger) Tracef(format string, args ...interface{}) {
gl.Logf(TRACE, format, args...)
}
func (gl *goLogger) Requestf(rlevel Level, format string, args ...interface{}) {
if gl.logger == nil {
return
}
if REQUEST <= gl.level {
2021-03-01 00:08:33 +01:00
gl.log(REQUEST, rlevel, fmt.Sprintf(format, args...))
2019-02-05 17:52:51 +01:00
}
}
func (gl *goLogger) Infof(format string, args ...interface{}) {
gl.Logf(INFO, format, args...)
}
func (gl *goLogger) Warnf(format string, args ...interface{}) {
gl.Logf(WARN, format, args...)
}
func (gl *goLogger) Errorf(format string, args ...interface{}) {
gl.Logf(ERROR, format, args...)
}
func (gl *goLogger) Severef(format string, args ...interface{}) {
gl.Logf(SEVERE, format, args...)
}
func (gl *goLogger) Fatalf(format string, args ...interface{}) {
gl.Logf(FATAL, format, args...)
}
func (gl *goLogger) Level() Level {
return gl.level
}
func (gl *goLogger) SetLevel(level Level) {
gl.level = level
}
2021-03-01 00:08:33 +01:00
func (gl *goLogger) log(level Level, rlevel Level, msg string) {
tm := time.Now().Format("2006-01-02T15:04:05.000-07:00") // time.RFC3339 with milliseconds
gl.logger.Print(gl.entryFormatter.format(tm, level, rlevel, msg))
2019-02-05 17:52:51 +01:00
}
type formatter interface {
2021-03-01 00:08:33 +01:00
format(string, Level, Level, string) string
2019-02-05 17:52:51 +01:00
}
type textFormatter struct {
}
// ex. 2016-02-10T09:15:25.498-08:00 [INFO] This is a message from test in text format
2021-03-01 00:08:33 +01:00
func (*textFormatter) format(tm string, level Level, rlevel Level, msg string) string {
b := &strings.Builder{}
appendValue(b, tm)
if rlevel != NONE {
fmt.Fprintf(b, "[%s,%s] ", level.String(), rlevel.String())
2019-02-05 17:52:51 +01:00
} else {
2021-03-01 00:08:33 +01:00
fmt.Fprintf(b, "[%s] ", level.String())
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
appendValue(b, msg)
2019-02-05 17:52:51 +01:00
b.WriteByte('\n')
2021-03-01 00:08:33 +01:00
return b.String()
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
func appendValue(b *strings.Builder, value interface{}) {
2019-02-05 17:52:51 +01:00
if _, ok := value.(string); ok {
fmt.Fprintf(b, "%s ", value)
} else {
fmt.Fprintf(b, "%v ", value)
}
}
type keyvalueFormatter struct {
}
// ex. _time=2016-02-10T09:15:25.498-08:00 _level=INFO _msg=This is a message from test in key-value format
2021-03-01 00:08:33 +01:00
func (*keyvalueFormatter) format(tm string, level Level, rlevel Level, msg string) string {
b := &strings.Builder{}
appendKeyValue(b, _TIME, tm)
appendKeyValue(b, _LEVEL, level.String())
if rlevel != NONE {
appendKeyValue(b, _RLEVEL, rlevel.String())
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
appendKeyValue(b, _MSG, msg)
2019-02-05 17:52:51 +01:00
b.WriteByte('\n')
2021-03-01 00:08:33 +01:00
return b.String()
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
func appendKeyValue(b *strings.Builder, key, value interface{}) {
2019-02-05 17:52:51 +01:00
if _, ok := value.(string); ok {
fmt.Fprintf(b, "%v=%s ", key, value)
} else {
fmt.Fprintf(b, "%v=%v ", key, value)
}
}
type jsonFormatter struct {
}
// ex. {"_level":"INFO","_msg":"This is a message from test in json format","_time":"2016-02-10T09:12:59.518-08:00"}
2021-03-01 00:08:33 +01:00
func (*jsonFormatter) format(tm string, level Level, rlevel Level, msg string) string {
data := make(map[string]interface{}, 4)
data[_TIME] = tm
data[_LEVEL] = level.String()
if rlevel != NONE {
data[_RLEVEL] = rlevel.String()
2019-02-05 17:52:51 +01:00
}
2021-03-01 00:08:33 +01:00
data[_MSG] = msg
serialized, _ := json.Marshal(data)
var b strings.Builder
b.Write(serialized)
b.WriteByte('\n')
return b.String()
2019-02-05 17:52:51 +01:00
}
type ComponentCallback func() string
type uniformFormatter struct {
callback ComponentCallback
}
// ex. 2019-03-15T11:28:07.652-04:00 DEBU COMPONENT.subcomponent This is a message from test in uniform format
var _LEVEL_UNIFORM = []string{
DEBUG: "DEBU",
TRACE: "TRAC",
REQUEST: "REQU",
INFO: "INFO",
WARN: "WARN",
ERROR: "ERRO",
SEVERE: "SEVE",
FATAL: "FATA",
NONE: "NONE",
}
func (level Level) UniformString() string {
return _LEVEL_UNIFORM[level]
}
2021-03-01 00:08:33 +01:00
func (uf *uniformFormatter) format(tm string, level Level, rlevel Level, msg string) string {
b := &strings.Builder{}
appendValue(b, tm)
component := uf.callback()
2021-03-01 00:08:33 +01:00
if rlevel != NONE {
// not really any accommodation for a composite level in the uniform standard; just output as abbr,abbr
2021-03-01 00:08:33 +01:00
fmt.Fprintf(b, "%s,%s %s ", level.UniformString(), rlevel.UniformString(), component)
} else {
2021-03-01 00:08:33 +01:00
fmt.Fprintf(b, "%s %s ", level.UniformString(), component)
}
2021-03-01 00:08:33 +01:00
appendValue(b, msg)
b.WriteByte('\n')
2021-03-01 00:08:33 +01:00
return b.String()
}