Files
netbird/client/ui/services/debug.go
Zoltán Papp ff6aef5e2a feat(ui): GUI debug logging follows daemon log level + debug bundle
When the daemon is set to debug/trace, the GUI now automatically writes a
rotated gui-client.log in the user's config dir and the daemon's debug bundle
collects it. The UI learns the level both at startup (daemon already in debug)
and live, by piggybacking the existing SubscribeEvents stream: the daemon
publishes a marked log-level-changed SystemEvent (and a per-subscription
snapshot), which DaemonFeed routes to guilog.DebugLog instead of an OS toast.
The UI registers its log path via a new RegisterUILog RPC so the root daemon,
which can't resolve the user's config dir, knows where to find the file.

Manual --log-file (any value) disables the daemon-driven file logging.

Fix: client/ui SetLogLevel looked up proto.LogLevel_value with the lowercase
logrus name, which never matched the uppercase enum keys and silently fell back
to INFO — so trace/debug requests from the bundle flow had no effect.
2026-06-11 18:34:48 +02:00

132 lines
3.7 KiB
Go

//go:build !android && !ios && !freebsd && !js
package services
import (
"context"
"fmt"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/netbirdio/netbird/client/proto"
"github.com/netbirdio/netbird/version"
)
// DebugBundleParams configures what the daemon collects when generating a
// debug bundle.
type DebugBundleParams struct {
Anonymize bool `json:"anonymize"`
SystemInfo bool `json:"systemInfo"`
UploadURL string `json:"uploadUrl"`
LogFileCount uint32 `json:"logFileCount"`
}
// DebugBundleResult mirrors DebugBundleResponse — Path is set on local-only
// bundles, UploadedKey on successful uploads, UploadFailureReason on failed
// uploads.
type DebugBundleResult struct {
Path string `json:"path"`
UploadedKey string `json:"uploadedKey"`
UploadFailureReason string `json:"uploadFailureReason"`
}
// LogLevel is a single log-level value the daemon understands ("error",
// "warn", "info", "debug", "trace").
type LogLevel struct {
Level string `json:"level"`
}
// Debug groups debug / log-level / packet-trace RPCs.
type Debug struct {
conn DaemonConn
}
func NewDebug(conn DaemonConn) *Debug {
return &Debug{conn: conn}
}
func (s *Debug) Bundle(ctx context.Context, p DebugBundleParams) (DebugBundleResult, error) {
cli, err := s.conn.Client()
if err != nil {
return DebugBundleResult{}, err
}
resp, err := cli.DebugBundle(ctx, &proto.DebugBundleRequest{
Anonymize: p.Anonymize,
SystemInfo: p.SystemInfo,
UploadURL: p.UploadURL,
LogFileCount: p.LogFileCount,
CliVersion: version.NetbirdVersion(),
})
if err != nil {
return DebugBundleResult{}, err
}
return DebugBundleResult{
Path: resp.GetPath(),
UploadedKey: resp.GetUploadedKey(),
UploadFailureReason: resp.GetUploadFailureReason(),
}, nil
}
func (s *Debug) GetLogLevel(ctx context.Context) (LogLevel, error) {
cli, err := s.conn.Client()
if err != nil {
return LogLevel{}, err
}
resp, err := cli.GetLogLevel(ctx, &proto.GetLogLevelRequest{})
if err != nil {
return LogLevel{}, err
}
return LogLevel{Level: resp.GetLevel().String()}, nil
}
// RevealFile opens the OS file manager focused on the given path. Wails'
// Browser.OpenURL refuses non-http(s) schemes, so the UI calls this binding
// instead of constructing a file:// URL.
func (s *Debug) RevealFile(_ context.Context, path string) error {
if path == "" {
return fmt.Errorf("empty path")
}
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("open", "-R", path)
case "windows":
cmd = exec.Command("explorer", "/select,"+path)
default:
cmd = exec.Command("xdg-open", filepath.Dir(path))
}
return cmd.Start()
}
// RegisterUILog tells the daemon the absolute path of the GUI's log file so
// the daemon's debug bundle can collect it (the daemon runs as root and can't
// resolve the user's config dir). Called by LogLevelWatcher on each daemon
// (re)connect.
func (s *Debug) RegisterUILog(ctx context.Context, path string) error {
cli, err := s.conn.Client()
if err != nil {
return err
}
_, err = cli.RegisterUILog(ctx, &proto.RegisterUILogRequest{Path: path})
return err
}
func (s *Debug) SetLogLevel(ctx context.Context, lvl LogLevel) error {
cli, err := s.conn.Client()
if err != nil {
return err
}
// proto.LogLevel_value keys are the enum names (TRACE/DEBUG/INFO/...), but
// callers (the React side, GetLogLevel) use the lowercase logrus names
// ("trace"/"debug"/...). Upper-case before the lookup so a lowercase level
// doesn't silently fall back to INFO.
level, ok := proto.LogLevel_value[strings.ToUpper(lvl.Level)]
if !ok {
level = int32(proto.LogLevel_INFO)
}
_, err = cli.SetLogLevel(ctx, &proto.SetLogLevelRequest{Level: proto.LogLevel(level)})
return err
}