Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log file #417

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ func general(k *Key) error {
if err != nil {
return err
}
log.SetLogger(log.Must(log.NewLeveled(level)))
var logFile *string
if k.LogFile != "" {
logFile = &k.LogFile
}
log.SetLogger(log.Must(log.NewLeveled(level, logFile)))

if k.Interface != "" {
iface, err := net.InterfaceByName(k.Interface)
Expand Down
1 change: 1 addition & 0 deletions engine/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Key struct {
RestAPI string `yaml:"restapi"`
Device string `yaml:"device"`
LogLevel string `yaml:"loglevel"`
LogFile string `yaml:"logfile"`
Interface string `yaml:"interface"`
TCPModerateReceiveBuffer bool `yaml:"tcp-moderate-receive-buffer"`
TCPSendBufferSize string `yaml:"tcp-send-buffer-size"`
Expand Down
9 changes: 8 additions & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
SetLogger(zap.Must(zap.NewProduction()))
}

func NewLeveled(l Level, options ...Option) (*Logger, error) {
func NewLeveled(l Level, logFile *string, options ...Option) (*Logger, error) {
switch l {
case SilentLevel:
return zap.NewNop(), nil
Expand All @@ -27,6 +27,13 @@ func NewLeveled(l Level, options ...Option) (*Logger, error) {
case InfoLevel, WarnLevel, ErrorLevel, DPanicLevel, PanicLevel, FatalLevel:
cfg := zap.NewProductionConfig()
cfg.Level.SetLevel(l)
logPath := []string{"stderr"}
if logFile != nil {
logPath = []string{*logFile}
fmt.Println("log output :", *logFile)
}
cfg.ErrorOutputPaths = logPath
cfg.OutputPaths = logPath
return cfg.Build(options...)
default:
return nil, fmt.Errorf("invalid level: %s", l)
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func init() {
flag.StringVar(&key.Device, "device", "", "Use this device [driver://]name")
flag.StringVar(&key.Interface, "interface", "", "Use network INTERFACE (Linux/MacOS only)")
flag.StringVar(&key.LogLevel, "loglevel", "info", "Log level [debug|info|warn|error|silent]")
flag.StringVar(&key.LogFile, "logfile", "", "Log file path [./output.log]")
flag.StringVar(&key.Proxy, "proxy", "", "Use this proxy [protocol://]host[:port]")
flag.StringVar(&key.RestAPI, "restapi", "", "HTTP statistic server listen address")
flag.StringVar(&key.TCPSendBufferSize, "tcp-sndbuf", "", "Set TCP send buffer size for netstack")
Expand Down