mirror of
https://github.com/Luzifer/s3sync.git
synced 2024-12-20 19:41:15 +00:00
Added levels to logging to silence output if required
This commit is contained in:
parent
9d01798e78
commit
1675060782
3 changed files with 64 additions and 9 deletions
47
logger/logger.go
Normal file
47
logger/logger.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
package logger
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type LogLevel uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
Error LogLevel = iota
|
||||||
|
Warning
|
||||||
|
Info
|
||||||
|
Debug
|
||||||
|
)
|
||||||
|
|
||||||
|
// Logger is a wrapper around output to filter according to levels
|
||||||
|
type Logger struct {
|
||||||
|
Level LogLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(logLevel LogLevel) *Logger {
|
||||||
|
return &Logger{
|
||||||
|
Level: logLevel,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Log(level LogLevel, line string) {
|
||||||
|
if l.Level >= level {
|
||||||
|
fmt.Println(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) LogF(level LogLevel, line string, args ...interface{}) {
|
||||||
|
if l.Level >= level {
|
||||||
|
fmt.Printf(line, args...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) ErrorF(line string, args ...interface{}) {
|
||||||
|
l.LogF(Error, line, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) InfoF(line string, args ...interface{}) {
|
||||||
|
l.LogF(Info, line, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) DebugF(line string, args ...interface{}) {
|
||||||
|
l.LogF(Debug, line, args...)
|
||||||
|
}
|
24
main.go
24
main.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Luzifer/s3sync/logger"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,7 +19,9 @@ var (
|
||||||
Public bool
|
Public bool
|
||||||
PrintVersion bool
|
PrintVersion bool
|
||||||
MaxThreads int
|
MaxThreads int
|
||||||
|
logLevel uint
|
||||||
}{}
|
}{}
|
||||||
|
stdout *logger.Logger
|
||||||
version = "dev"
|
version = "dev"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -53,6 +56,11 @@ func main() {
|
||||||
app.Flags().BoolVarP(&cfg.Delete, "delete", "d", false, "Delete files on remote not existing on local")
|
app.Flags().BoolVarP(&cfg.Delete, "delete", "d", false, "Delete files on remote not existing on local")
|
||||||
app.Flags().BoolVar(&cfg.PrintVersion, "version", false, "Print version and quit")
|
app.Flags().BoolVar(&cfg.PrintVersion, "version", false, "Print version and quit")
|
||||||
app.Flags().IntVar(&cfg.MaxThreads, "max-threads", 10, "Use max N parallel threads for file sync")
|
app.Flags().IntVar(&cfg.MaxThreads, "max-threads", 10, "Use max N parallel threads for file sync")
|
||||||
|
app.Flags().UintVar(&cfg.logLevel, "loglevel", 2, "Amount of log output (0 = Error only, 3 = Debug)")
|
||||||
|
|
||||||
|
app.ParseFlags(os.Args[1:])
|
||||||
|
|
||||||
|
stdout = logger.New(logger.LogLevel(cfg.logLevel))
|
||||||
|
|
||||||
app.Execute()
|
app.Execute()
|
||||||
}
|
}
|
||||||
|
@ -94,28 +102,28 @@ func execSync(cmd *cobra.Command, args []string) {
|
||||||
if needsCopy {
|
if needsCopy {
|
||||||
l, err := local.ReadFile(path.Join(localPath, localFile.Filename))
|
l, err := local.ReadFile(path.Join(localPath, localFile.Filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("(%d / %d) %s ERR: %s\n", i+1, len(localFiles), localFile.Filename, err)
|
stdout.ErrorF("(%d / %d) %s ERR: %s\n", i+1, len(localFiles), localFile.Filename, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer, err := ioutil.ReadAll(l)
|
buffer, err := ioutil.ReadAll(l)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("(%d / %d) %s ERR: %s\n", i+1, len(localFiles), localFile.Filename, err)
|
stdout.ErrorF("(%d / %d) %s ERR: %s\n", i+1, len(localFiles), localFile.Filename, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
l.Close()
|
l.Close()
|
||||||
|
|
||||||
err = remote.WriteFile(path.Join(remotePath, localFile.Filename), bytes.NewReader(buffer), cfg.Public)
|
err = remote.WriteFile(path.Join(remotePath, localFile.Filename), bytes.NewReader(buffer), cfg.Public)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("(%d / %d) %s ERR: %s\n", i+1, len(localFiles), localFile.Filename, err)
|
stdout.ErrorF("(%d / %d) %s ERR: %s\n", i+1, len(localFiles), localFile.Filename, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("(%d / %d) %s OK\n", i+1, len(localFiles), localFile.Filename)
|
stdout.InfoF("(%d / %d) %s OK\n", i+1, len(localFiles), localFile.Filename)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("(%d / %d) %s Skip\n", i+1, len(localFiles), localFile.Filename)
|
stdout.DebugF("(%d / %d) %s Skip\n", i+1, len(localFiles), localFile.Filename)
|
||||||
}(i, localFile)
|
}(i, localFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,10 +142,10 @@ func execSync(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
if needsDeletion {
|
if needsDeletion {
|
||||||
if err := remote.DeleteFile(path.Join(remotePath, remoteFile.Filename)); err != nil {
|
if err := remote.DeleteFile(path.Join(remotePath, remoteFile.Filename)); err != nil {
|
||||||
fmt.Printf("delete: %s ERR: %s\n", remoteFile.Filename, err)
|
stdout.ErrorF("delete: %s ERR: %s\n", remoteFile.Filename, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("delete: %s OK\n", remoteFile.Filename)
|
stdout.InfoF("delete: %s OK\n", remoteFile.Filename)
|
||||||
}
|
}
|
||||||
}(remoteFile)
|
}(remoteFile)
|
||||||
}
|
}
|
||||||
|
@ -146,7 +154,7 @@ func execSync(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
func errExit(err error) {
|
func errExit(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("ERR: %s\n", err)
|
stdout.ErrorF("ERR: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
s3.go
2
s3.go
|
@ -78,7 +78,7 @@ func (s *s3Provider) ListFiles(prefix string) ([]file, error) {
|
||||||
case err := <-errChan:
|
case err := <-errChan:
|
||||||
return out, err
|
return out, err
|
||||||
case <-doneTimer.C:
|
case <-doneTimer.C:
|
||||||
fmt.Printf("Scanning prefixes (%d working, %d left)...\r", len(syncChan), len(prefixChan))
|
stdout.DebugF("Scanning prefixes (%d working, %d left)...\r", len(syncChan), len(prefixChan))
|
||||||
if len(prefixChan) == 0 && len(syncChan) == 0 {
|
if len(prefixChan) == 0 && len(syncChan) == 0 {
|
||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
return out, nil
|
return out, nil
|
||||||
|
|
Loading…
Reference in a new issue