Skip to content

Commit

Permalink
fix: write debug-level logs to data dir on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Aug 26, 2024
1 parent bb00afa commit a9eb786
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 67 deletions.
61 changes: 40 additions & 21 deletions cmd/backrest/backrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
"syscall"
Expand All @@ -32,12 +32,14 @@ import (
"go.uber.org/zap/zapcore"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"gopkg.in/natefinch/lumberjack.v2"
)

var InstallDepsOnly = flag.Bool("install-deps-only", false, "install dependencies and exit")

func main() {
flag.Parse()
installLoggers()

resticPath, err := resticinstaller.FindOrInstallResticBinary()
if err != nil {
Expand Down Expand Up @@ -133,26 +135,6 @@ func main() {
wg.Wait()
}

func init() {
if !strings.HasPrefix(os.Getenv("ENV"), "prod") {
c := zap.NewDevelopmentEncoderConfig()
c.EncodeLevel = zapcore.CapitalColorLevelEncoder
c.EncodeTime = zapcore.ISO8601TimeEncoder
l := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(c),
zapcore.AddSync(colorable.NewColorableStdout()),
zapcore.DebugLevel,
))
zap.ReplaceGlobals(l)
} else {
zap.ReplaceGlobals(zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(os.Stdout),
zapcore.DebugLevel,
)))
}
}

func createConfigProvider() config.ConfigStore {
return &config.CachingValidatingStore{
ConfigStore: &config.JsonFileStore{Path: env.ConfigFilePath()},
Expand Down Expand Up @@ -203,3 +185,40 @@ func newForceKillHandler() func() {
zap.S().Warn("attempting graceful shutdown, to force termination press Ctrl+C again")
}
}

func installLoggers() {
// Pretty logging for console
c := zap.NewDevelopmentEncoderConfig()
c.EncodeLevel = zapcore.CapitalColorLevelEncoder
c.EncodeTime = zapcore.ISO8601TimeEncoder
pretty := zapcore.NewCore(
zapcore.NewConsoleEncoder(c),
zapcore.AddSync(colorable.NewColorableStdout()),
zapcore.InfoLevel,
)

// JSON logging to log directory
logsDir := env.LogsPath()
if err := os.MkdirAll(logsDir, 0755); err != nil {
zap.ReplaceGlobals(zap.New(pretty))
zap.S().Errorf("error creating logs directory %q, will only log to console for now: %v", err)
return
}

writer := &lumberjack.Logger{
Filename: filepath.Join(logsDir, "backrest.log"),
MaxSize: 5, // megabytes
MaxBackups: 3,
MaxAge: 14,
Compress: true,
}

ugly := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(writer),
zapcore.DebugLevel,
)

zap.ReplaceGlobals(zap.New(zapcore.NewTee(pretty, ugly)))
zap.S().Infof("writing logs to: %v", logsDir)
}
42 changes: 1 addition & 41 deletions cmd/backrestmon/backrestmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/garethgeorge/backrest/internal/env"
"github.com/getlantern/systray"
"github.com/ncruces/zenity"
lumberjack "gopkg.in/natefinch/lumberjack.v2"

_ "embed"
)
Expand All @@ -25,13 +23,6 @@ import (
var icon []byte

func main() {
l, err := createLogWriter()
if err != nil {
reportError(err)
return
}
defer l.Close()

backrest, err := findBackrest()
if err != nil {
reportError(err)
Expand All @@ -45,14 +36,6 @@ func main() {
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "ENV=production")

pr, pw := io.Pipe()
cmd.Stdout = pw
cmd.Stderr = pw

go func() {
io.Copy(l, pr)
}()

if err := cmd.Start(); err != nil {
reportError(err)
cancel()
Expand Down Expand Up @@ -80,7 +63,7 @@ func main() {
mOpenLog.ClickedCh = make(chan struct{})
go func() {
for range mOpenLog.ClickedCh {
cmd := exec.Command(`explorer`, `/select,`, logsPath())
cmd := exec.Command(`explorer`, `/select,`, env.LogsPath())
cmd.Start()
go cmd.Wait()
}
Expand Down Expand Up @@ -147,26 +130,3 @@ func openBrowser(url string) error {
func reportError(err error) {
zenity.Error(err.Error(), zenity.Title("Backrest Error"))
}

func createLogWriter() (io.WriteCloser, error) {
logsDir := logsPath()
fmt.Printf("Logging to %s\n", logsDir)
if err := os.MkdirAll(logsDir, 0755); err != nil {
return nil, err
}

l := &lumberjack.Logger{
Filename: filepath.Join(logsDir, "backrest.log"),
MaxSize: 5, // megabytes
MaxBackups: 3,
MaxAge: 14,
Compress: true,
}

return l, nil
}

func logsPath() string {
dataDir := env.DataDir()
return filepath.Join(dataDir, "processlogs")
}
4 changes: 0 additions & 4 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ create_launchd_plist() {
</array>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/backrest.log</string>
<key>StandardErrorPath</key>
<string>/tmp/backrest.log</string>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
Expand Down
6 changes: 6 additions & 0 deletions internal/env/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
Expand Down Expand Up @@ -74,6 +75,11 @@ func ResticBinPath() string {
return ""
}

func LogsPath() string {
dataDir := DataDir()
return filepath.Join(dataDir, "processlogs")
}

func getHomeDir() string {
home, err := os.UserHomeDir()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func NewOrchestrator(resticBin string, cfg *v1.Config, log *oplog.OpLog, logStor
}
}

zap.S().Info("scrubbed operation log for incomplete operations",
zap.L().Info("scrubbed operation log for incomplete operations",
zap.Duration("duration", time.Since(startTime)),
zap.Int("incomplete_ops", len(incompleteOps)),
zap.Int("incomplete_repos", len(incompleteRepos)),
Expand Down

0 comments on commit a9eb786

Please sign in to comment.