GoDoxy/internal/logging/logging.go
Yuzerion 4a8bd48ad5
Some checks are pending
Docker Image CI (socket-proxy) / build (push) Waiting to run
fix: optimize memory usage, fix agent and code refactor (#118)
* refactor: simplify io code and make utils module independent

* fix(docker): agent and socket-proxy docker event flushing with modified reverse proxy handler

* refactor: remove unused code

* refactor: remove the use of logging module in most code

* refactor: streamline domain mismatch check in certState function

* tweak: use ecdsa p-256 for autocert

* fix(tests): update health check tests for invalid host and add case for port in host

* feat(acme): custom acme directory

* refactor: code refactor and improved context and error handling

* tweak: optimize memory usage under load

* fix(oidc): restore old user matching behavior

* docs: add ChatGPT assistant to README

---------

Co-authored-by: yusing <yusing@6uo.me>
2025-05-25 09:45:57 +08:00

67 lines
1.3 KiB
Go

package logging
import (
"io"
"log"
"os"
"strings"
"github.com/rs/zerolog"
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/utils/strutils"
zerologlog "github.com/rs/zerolog/log"
)
var (
logger zerolog.Logger
timeFmt string
level zerolog.Level
prefix string
)
func init() {
switch {
case common.IsTrace:
timeFmt = "04:05"
level = zerolog.TraceLevel
case common.IsDebug:
timeFmt = "01-02 15:04"
level = zerolog.DebugLevel
default:
timeFmt = "01-02 15:04"
level = zerolog.InfoLevel
}
prefixLength := len(timeFmt) + 5 // level takes 3 + 2 spaces
prefix = strings.Repeat(" ", prefixLength)
InitLogger(os.Stdout)
}
func fmtMessage(msg string) string {
lines := strutils.SplitRune(msg, '\n')
if len(lines) == 1 {
return msg
}
for i := 1; i < len(lines); i++ {
lines[i] = prefix + lines[i]
}
return strutils.JoinRune(lines, '\n')
}
func InitLogger(out ...io.Writer) {
writer := zerolog.ConsoleWriter{
Out: zerolog.MultiLevelWriter(out...),
TimeFormat: timeFmt,
FormatMessage: func(msgI interface{}) string { // pad spaces for each line
return fmtMessage(msgI.(string))
},
}
logger = zerolog.New(
writer,
).Level(level).With().Timestamp().Logger()
log.SetOutput(writer)
log.SetPrefix("")
log.SetFlags(0)
zerolog.TimeFieldFormat = timeFmt
zerologlog.Logger = logger
}