diff --git a/cmd/backrest/backrest.go b/cmd/backrest/backrest.go
index 8fc1d58d..82457f5d 100644
--- a/cmd/backrest/backrest.go
+++ b/cmd/backrest/backrest.go
@@ -9,8 +9,8 @@ import (
"os"
"os/signal"
"path"
+ "path/filepath"
"runtime"
- "strings"
"sync"
"sync/atomic"
"syscall"
@@ -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 {
@@ -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()},
@@ -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)
+}
diff --git a/cmd/backrestmon/backrestmon.go b/cmd/backrestmon/backrestmon.go
index 2387e5c8..5eb01757 100644
--- a/cmd/backrestmon/backrestmon.go
+++ b/cmd/backrestmon/backrestmon.go
@@ -6,7 +6,6 @@ package main
import (
"context"
"fmt"
- "io"
"os"
"os/exec"
"path/filepath"
@@ -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"
)
@@ -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)
@@ -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()
@@ -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()
}
@@ -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")
-}
diff --git a/install.sh b/install.sh
index e1936415..59a5a202 100755
--- a/install.sh
+++ b/install.sh
@@ -66,10 +66,6 @@ create_launchd_plist() {
KeepAlive
- StandardOutPath
- /tmp/backrest.log
- StandardErrorPath
- /tmp/backrest.log
EnvironmentVariables
PATH
diff --git a/internal/env/environment.go b/internal/env/environment.go
index 9fd7d4d4..1e08be37 100644
--- a/internal/env/environment.go
+++ b/internal/env/environment.go
@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
+ "path/filepath"
"runtime"
"strings"
)
@@ -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 {
diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.go
index 7e145cf0..854c4c9f 100644
--- a/internal/orchestrator/orchestrator.go
+++ b/internal/orchestrator/orchestrator.go
@@ -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)),