mirror of
https://github.com/netfun2000/rttys_zhaojh329.git
synced 2026-02-27 09:53:24 +08:00
Remove log file fallback for non-TTY output
The feature that wrote logs to a file when stdout wasn't a terminal is removed. Systemd services already handle log persistence through journald and its logging facilities. This duplication is unnecessary and conflicts with standard systemd logging practices. View log in systemd: ``` journalctl -u rttys ``` or ``` journalctl -u rttys -f ``` Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
@@ -15,7 +15,6 @@ require (
|
||||
github.com/rs/zerolog v1.34.0
|
||||
github.com/urfave/cli/v3 v3.3.8
|
||||
github.com/valyala/bytebufferpool v1.0.0
|
||||
golang.org/x/term v0.32.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@@ -106,8 +106,6 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
|
||||
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
|
||||
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
|
||||
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
|
||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
|
||||
|
||||
-45
@@ -1,51 +1,15 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/dwdcth/consoleEx"
|
||||
"github.com/mattn/go-colorable"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
type logFileHook struct {
|
||||
err error
|
||||
path string
|
||||
}
|
||||
|
||||
var logFile = &logFileHook{}
|
||||
|
||||
func (h *logFileHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
|
||||
if h.err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(h.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
h.err = err
|
||||
log.Fatal().Msg(err.Error())
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
f.WriteString(zerolog.TimestampFunc().Format(zerolog.TimeFieldFormat) + " |")
|
||||
f.WriteString(strings.ToUpper(level.String()) + "| ")
|
||||
|
||||
pc, file, line, ok := runtime.Caller(3)
|
||||
if ok {
|
||||
f.WriteString(zerolog.CallerMarshalFunc(pc, file, line) + " |")
|
||||
}
|
||||
|
||||
f.WriteString(msg)
|
||||
f.WriteString("\n")
|
||||
}
|
||||
|
||||
func init() {
|
||||
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
||||
return filepath.Base(file) + ":" + strconv.Itoa(line)
|
||||
@@ -54,20 +18,11 @@ func init() {
|
||||
out := consoleEx.ConsoleWriterEx{Out: colorable.NewColorableStdout()}
|
||||
logger := zerolog.New(out).With().Timestamp().Logger()
|
||||
|
||||
if !term.IsTerminal(int(os.Stdout.Fd())) {
|
||||
logger = logger.Hook(logFile)
|
||||
}
|
||||
|
||||
log.Logger = logger
|
||||
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
}
|
||||
|
||||
// SetPath set the log file path
|
||||
func SetPath(path string) {
|
||||
logFile.path = path
|
||||
}
|
||||
|
||||
func Verbose() {
|
||||
log.Logger = log.Logger.With().Caller().Logger()
|
||||
}
|
||||
|
||||
@@ -46,21 +46,11 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
defaultLogPath := "/var/log/rttys.log"
|
||||
if runtime.GOOS == "windows" {
|
||||
defaultLogPath = "rttys.log"
|
||||
}
|
||||
|
||||
cmd := &cli.Command{
|
||||
Name: "rttys",
|
||||
Usage: "The server side for rtty",
|
||||
Version: RttysVersion,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "log",
|
||||
Value: defaultLogPath,
|
||||
Usage: "log file path",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "log-level",
|
||||
Value: "info",
|
||||
@@ -141,8 +131,6 @@ func main() {
|
||||
func cmdAction(c context.Context, cmd *cli.Command) error {
|
||||
defer logPanic()
|
||||
|
||||
xlog.SetPath(cmd.String("log"))
|
||||
|
||||
switch cmd.String("log-level") {
|
||||
case "debug":
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
|
||||
Reference in New Issue
Block a user