mirror of
https://github.com/hanzozt/zt.git
synced 2026-11-03 05:01:58 +00:00
- Repo names: ziti-* → zt-* - Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt - Docker images: hanzozt/ziti-* → hanzozt/zt-* - C/Java/JS symbols: ziti_* → zt_* - File and directory names updated
179 lines
5.0 KiB
Go
179 lines
5.0 KiB
Go
//go:build all
|
|
|
|
/*
|
|
Copyright NetFoundry Inc.
|
|
|
|
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
|
|
|
|
https://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 main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/michaelquigley/pfxlog"
|
|
"github.com/hanzozt/foundation/v2/debugz"
|
|
"github.com/hanzozt/transport/v2"
|
|
"github.com/hanzozt/transport/v2/tcp"
|
|
"github.com/hanzozt/transport/v2/tls"
|
|
"github.com/hanzozt/transport/v2/transwarp"
|
|
"github.com/hanzozt/transport/v2/transwarptls"
|
|
"github.com/hanzozt/transport/v2/wss"
|
|
_ "github.com/hanzozt/zt/zttest/zt-traffic-test/client"
|
|
_ "github.com/hanzozt/zt/zttest/zt-traffic-test/loop2"
|
|
_ "github.com/hanzozt/zt/zttest/zt-traffic-test/loop3"
|
|
_ "github.com/hanzozt/zt/zttest/zt-traffic-test/loop4"
|
|
"github.com/hanzozt/zt/zttest/zt-traffic-test/subcmd"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func init() {
|
|
pfxlog.GlobalInit(logrus.InfoLevel, pfxlog.DefaultOptions().SetTrimPrefix("github.com/hanzozt/").NoColor())
|
|
transport.AddAddressParser(tls.AddressParser{})
|
|
transport.AddAddressParser(tcp.AddressParser{})
|
|
transport.AddAddressParser(transwarp.AddressParser{})
|
|
transport.AddAddressParser(transwarptls.AddressParser{})
|
|
transport.AddAddressParser(wss.AddressParser{})
|
|
|
|
logrus.SetFormatter(&JSONFormatter{})
|
|
}
|
|
|
|
func main() {
|
|
subcmd.Execute()
|
|
}
|
|
|
|
type fieldKey string
|
|
|
|
// FieldMap allows customization of the key names for default fields.
|
|
type FieldMap map[fieldKey]string
|
|
|
|
func (f FieldMap) resolve(key fieldKey) string {
|
|
if k, ok := f[key]; ok {
|
|
return k
|
|
}
|
|
|
|
return string(key)
|
|
}
|
|
|
|
// JSONFormatter formats logs into parsable json
|
|
type JSONFormatter struct {
|
|
// TimestampFormat sets the format used for marshaling timestamps.
|
|
// The format to use is the same than for time.Format or time.Parse from the standard
|
|
// library.
|
|
// The standard Library already provides a set of predefined format.
|
|
TimestampFormat string
|
|
|
|
// DisableTimestamp allows disabling automatic timestamps in output
|
|
DisableTimestamp bool
|
|
|
|
// DisableHTMLEscape allows disabling html escaping in output
|
|
DisableHTMLEscape bool
|
|
|
|
// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
|
|
DataKey string
|
|
|
|
// FieldMap allows users to customize the names of keys for default fields.
|
|
// As an example:
|
|
// formatter := &JSONFormatter{
|
|
// FieldMap: FieldMap{
|
|
// FieldKeyTime: "@timestamp",
|
|
// FieldKeyLevel: "@level",
|
|
// FieldKeyMsg: "@message",
|
|
// FieldKeyFunc: "@caller",
|
|
// },
|
|
// }
|
|
FieldMap FieldMap
|
|
|
|
// CallerPrettyfier can be set by the user to modify the content
|
|
// of the function and file keys in the json data when ReportCaller is
|
|
// activated. If any of the returned value is the empty string the
|
|
// corresponding key will be removed from json fields.
|
|
CallerPrettyfier func(*runtime.Frame) (function string, file string)
|
|
|
|
// PrettyPrint will indent all json logs
|
|
PrettyPrint bool
|
|
}
|
|
|
|
// Format renders a single log entry
|
|
func (f *JSONFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
data := make(logrus.Fields, len(entry.Data)+4)
|
|
for k, v := range entry.Data {
|
|
switch v := v.(type) {
|
|
case error:
|
|
// Otherwise errors are ignored by `encoding/json`
|
|
// https://github.com/sirupsen/logrus/issues/137
|
|
data[k] = v.Error()
|
|
default:
|
|
data[k] = v
|
|
}
|
|
}
|
|
|
|
if f.DataKey != "" {
|
|
newData := make(logrus.Fields, 4)
|
|
newData[f.DataKey] = data
|
|
data = newData
|
|
}
|
|
|
|
// prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
|
|
|
|
timestampFormat := f.TimestampFormat
|
|
if timestampFormat == "" {
|
|
timestampFormat = time.RFC3339
|
|
}
|
|
|
|
//if entry. != "" {
|
|
// data[f.FieldMap.resolve(logrus.FieldKeyLogrusError)] = entry.err
|
|
//}
|
|
if !f.DisableTimestamp {
|
|
data[f.FieldMap.resolve(logrus.FieldKeyTime)] = entry.Time.Format(timestampFormat)
|
|
}
|
|
data[f.FieldMap.resolve(logrus.FieldKeyMsg)] = entry.Message
|
|
data[f.FieldMap.resolve(logrus.FieldKeyLevel)] = entry.Level.String()
|
|
if entry.HasCaller() {
|
|
funcVal := entry.Caller.Function
|
|
fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
|
|
if f.CallerPrettyfier != nil {
|
|
funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
|
|
}
|
|
if funcVal != "" {
|
|
data[f.FieldMap.resolve(logrus.FieldKeyFunc)] = funcVal
|
|
}
|
|
if fileVal != "" {
|
|
data[f.FieldMap.resolve(logrus.FieldKeyFile)] = fileVal
|
|
}
|
|
}
|
|
|
|
var b *bytes.Buffer
|
|
if entry.Buffer != nil {
|
|
b = entry.Buffer
|
|
} else {
|
|
b = &bytes.Buffer{}
|
|
}
|
|
|
|
encoder := json.NewEncoder(b)
|
|
encoder.SetEscapeHTML(!f.DisableHTMLEscape)
|
|
if f.PrettyPrint {
|
|
encoder.SetIndent("", " ")
|
|
}
|
|
if err := encoder.Encode(data); err != nil {
|
|
debugz.DumpLocalStack()
|
|
return nil, fmt.Errorf("failed to marshal fields to JSON, %w", err)
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
}
|