mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 19:29:57 +00:00
TUN-8735: add managed/local log collection
## Summary Adds a log collector for the managed/local runtimes. Closes TUN-8735 TUN-8736
This commit is contained in:
73
diagnostic/log_collector_host.go
Normal file
73
diagnostic/log_collector_host.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package diagnostic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
const (
|
||||
linuxManagedLogsPath = "/var/log/cloudflared.err"
|
||||
darwinManagedLogsPath = "/Library/Logs/com.cloudflare.cloudflared.err.log"
|
||||
)
|
||||
|
||||
type HostLogCollector struct {
|
||||
client HTTPClient
|
||||
}
|
||||
|
||||
func NewHostLogCollector(client HTTPClient) *HostLogCollector {
|
||||
return &HostLogCollector{
|
||||
client,
|
||||
}
|
||||
}
|
||||
|
||||
func getServiceLogPath() (string, error) {
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
{
|
||||
path := darwinManagedLogsPath
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
userHomeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error getting user home: %w", err)
|
||||
}
|
||||
|
||||
return filepath.Join(userHomeDir, darwinManagedLogsPath), nil
|
||||
}
|
||||
case "linux":
|
||||
{
|
||||
return linuxManagedLogsPath, nil
|
||||
}
|
||||
default:
|
||||
return "", ErrManagedLogNotFound
|
||||
}
|
||||
}
|
||||
|
||||
func (collector *HostLogCollector) Collect(ctx context.Context) (*LogInformation, error) {
|
||||
logConfiguration, err := collector.client.GetLogConfiguration(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting log configuration: %w", err)
|
||||
}
|
||||
|
||||
if logConfiguration.uid == 0 {
|
||||
path, err := getServiceLogPath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewLogInformation(path, false, false), nil
|
||||
}
|
||||
|
||||
if logConfiguration.logFile != "" {
|
||||
return NewLogInformation(logConfiguration.logFile, false, false), nil
|
||||
} else if logConfiguration.logDirectory != "" {
|
||||
return NewLogInformation(logConfiguration.logDirectory, false, true), nil
|
||||
}
|
||||
|
||||
return nil, ErrMustNotBeEmpty
|
||||
}
|
Reference in New Issue
Block a user