2021-02-21 10:02:42 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
2021-03-10 16:03:26 -05:00
|
|
|
"os"
|
2021-02-21 10:02:42 -05:00
|
|
|
|
|
|
|
logService "github.com/v2fly/v2ray-core/v4/app/log/command"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/base"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdLog = &base.Command{
|
|
|
|
CustomFlags: true,
|
|
|
|
UsageLine: "{{.Exec}} api log [--server=127.0.0.1:8080]",
|
|
|
|
Short: "log operations",
|
|
|
|
Long: `
|
|
|
|
Follow and print logs from v2ray.
|
|
|
|
|
2021-03-10 16:03:26 -05:00
|
|
|
> Make sure you have "LoggerService" set in "config.api.services"
|
|
|
|
of server config.
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
> It ignores -timeout flag while following logs
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
-restart
|
|
|
|
Restart the logger
|
|
|
|
|
|
|
|
-s, -server <server:port>
|
|
|
|
The API server address. Default 127.0.0.1:8080
|
|
|
|
|
|
|
|
-t, -timeout <seconds>
|
|
|
|
Timeout seconds to call API. Default 3
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
{{.Exec}} {{.LongName}}
|
|
|
|
{{.Exec}} {{.LongName}} --restart
|
|
|
|
`,
|
2021-03-10 16:03:26 -05:00
|
|
|
Run: executeLog,
|
2021-02-21 10:02:42 -05:00
|
|
|
}
|
|
|
|
|
2021-03-10 16:03:26 -05:00
|
|
|
func executeLog(cmd *base.Command, args []string) {
|
2021-02-21 10:02:42 -05:00
|
|
|
var restart bool
|
|
|
|
cmd.Flag.BoolVar(&restart, "restart", false, "")
|
|
|
|
setSharedFlags(cmd)
|
|
|
|
cmd.Flag.Parse(args)
|
|
|
|
|
|
|
|
if restart {
|
|
|
|
restartLogger()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
followLogger()
|
|
|
|
}
|
|
|
|
|
|
|
|
func restartLogger() {
|
|
|
|
conn, ctx, close := dialAPIServer()
|
|
|
|
defer close()
|
|
|
|
client := logService.NewLoggerServiceClient(conn)
|
|
|
|
r := &logService.RestartLoggerRequest{}
|
|
|
|
_, err := client.RestartLogger(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to restart logger: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func followLogger() {
|
|
|
|
conn, ctx, close := dialAPIServerWithoutTimeout()
|
|
|
|
defer close()
|
|
|
|
client := logService.NewLoggerServiceClient(conn)
|
|
|
|
r := &logService.FollowLogRequest{}
|
|
|
|
stream, err := client.FollowLog(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to follow logger: %s", err)
|
|
|
|
}
|
2021-03-10 16:03:26 -05:00
|
|
|
// work with `v2ray api log | grep expr`
|
|
|
|
log.SetOutput(os.Stdout)
|
2021-02-21 10:02:42 -05:00
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to fetch log: %s", err)
|
|
|
|
}
|
2021-03-10 16:03:26 -05:00
|
|
|
log.Println(resp.Message)
|
2021-02-21 10:02:42 -05:00
|
|
|
}
|
|
|
|
}
|