Skip to content

Commit

Permalink
log: changed log library to zap
Browse files Browse the repository at this point in the history
  • Loading branch information
azak-azkaran committed Nov 6, 2020
1 parent e0dff19 commit 4c1c93f
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 167 deletions.
13 changes: 6 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"errors"
"log"
"strings"
"time"

Expand Down Expand Up @@ -303,7 +302,7 @@ func ParseConfiguration(confi *Configuration) {
if viper.IsSet(MAIN_TIME_DURATION) {
dur, err := time.ParseDuration(viper.GetString(MAIN_TIME_DURATION))
if err != nil {
log.Println("Error parsing duration: ", err)
Sugar.Error("Error parsing duration: ", err)
dur = 30 * time.Minute
}
confi.TimeBetweenStart = dur
Expand Down Expand Up @@ -341,8 +340,8 @@ func ParseConfiguration(confi *Configuration) {
confi.SecretID = viper.GetString(MAIN_VAULT_SECRET_ID)
}

log.Println("Agent initalzing on: ", confi.Hostname)
log.Println("Agent Configuration:",
Sugar.Warn("Agent initalzing on: ", confi.Hostname)
Sugar.Info("Agent Configuration:",
"\nAddress: ", confi.Address,
"\nPath to DB:", confi.PathDB,
"\nTime Between Backup Runs: ", confi.TimeBetweenStart,
Expand All @@ -355,11 +354,11 @@ func ParseConfiguration(confi *Configuration) {
if (confi.RoleID == "" && confi.SecretID == "") || (confi.RoleID == "" && confi.SecretID != "") || (confi.RoleID != "" && confi.SecretID == "") {
confi.RoleID = ""
confi.SecretID = ""
log.Println("Secret ID and Role ID reset")
Sugar.Info("Secret ID and Role ID reset")
confi.useLogin = false
} else {
log.Println("RoleID: ", confi.RoleID)
log.Println("SecretID: ", confi.SecretID)
Sugar.Info("RoleID: ", confi.RoleID)
Sugar.Info("SecretID: ", confi.SecretID)
confi.useLogin = true
}
}
13 changes: 6 additions & 7 deletions git.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"log"
"os"
"strings"

Expand All @@ -15,8 +14,8 @@ func GitClone(repo string, dir string, home string, personal string) error {
var err error

dir = strings.ReplaceAll(dir, HOME, home)
log.Println("Checkout out to Repo: ", repo)
log.Println("Checkout out to Dir: ", dir)
Sugar.Info("Checkout out to Repo: ", repo)
Sugar.Debug("Checkout out to Dir: ", dir)

cloneOptions := git.CloneOptions{
URL: repo,
Expand All @@ -42,7 +41,7 @@ func GitClone(repo string, dir string, home string, personal string) error {
if err != nil {
return err
}
log.Println("Checkout out Ref: ", ref)
Sugar.Debug("Checkout out Ref: ", ref)
return nil
}

Expand All @@ -54,7 +53,7 @@ func GitCreateRemote(dir string, home string, repoUrl string) error {
}
_, err = r.Remote(GIT_REMOTE_NAME)
if err != nil && err == git.ErrRemoteNotFound {
log.Println("Adding remote: ", GIT_REMOTE_NAME)
Sugar.Debug("Adding remote: ", GIT_REMOTE_NAME)
_, err = r.CreateRemote(&config.RemoteConfig{
Name: GIT_REMOTE_NAME,
URLs: []string{repoUrl},
Expand All @@ -66,7 +65,7 @@ func GitCreateRemote(dir string, home string, repoUrl string) error {

func GitPull(dir string, home string, personal string) error {
path := strings.ReplaceAll(dir, HOME, home)
log.Println("Pulling from: ", path)
Sugar.Info("Pulling from: ", path)
r, err := git.PlainOpen(path)
if err != nil {
return err
Expand Down Expand Up @@ -100,6 +99,6 @@ func GitPull(dir string, home string, personal string) error {
if err != nil {
return err
}
log.Println("Checkout out Ref: ", ref)
Sugar.Debug("Checkout out Ref: ", ref)
return nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.3
github.com/stretchr/testify v1.5.1
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 // indirect
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f // indirect
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 // indirect
Expand Down
25 changes: 25 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
Expand Down Expand Up @@ -280,6 +281,7 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand Down Expand Up @@ -339,21 +341,38 @@ github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0B
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 h1:IaQbIIB2X/Mp/DKctl6ROxz1KyMlKp4uyvL6+kQ7C88=
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -412,6 +431,10 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down Expand Up @@ -440,6 +463,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.55.0 h1:E8yzL5unfpW3M6fz/eB7Cb5MQAYSZ7GKo4Qth+N2sgQ=
Expand All @@ -460,3 +484,4 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
5 changes: 2 additions & 3 deletions gocryptfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"errors"
"io"
"log"
"os"
"os/exec"
"strings"
Expand All @@ -17,7 +16,7 @@ func MountFolders(home string, config []GocryptConfig) []*exec.Cmd {
cmd := mount(home, folderconfig)
err := IsEmpty(home, folderconfig.MountPoint)
if err != nil {
log.Println("ERROR", err)
Sugar.Error("ERROR", err)
} else {
output = append(output, cmd)
}
Expand Down Expand Up @@ -46,7 +45,7 @@ func MountGocryptfs(cryptoDir string, folder string, home string, duration time.

command = command + " " + cryptoDir + " " + folder

log.Println("Mounting: ", folder, " Duration", duration.String(), " AllowOther", allowOther)
Sugar.Debug("Mounting: ", folder, " Duration", duration.String(), " AllowOther", allowOther)
cmd = exec.Command("bash", "-c", command)
cmd.Env = os.Environ()
cmd.Stdin = strings.NewReader(pwd)
Expand Down
47 changes: 23 additions & 24 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"log"
"os/exec"

cmap "github.com/orcaman/concurrent-map"
Expand All @@ -20,9 +19,21 @@ type Job struct {
finished bool
}

func Log(toQueue string, p bool) {
if p {
log.Println("INFO: " + toQueue)
func LogJobStatus(job *Job) {
if !job.printOutput {
return
}

if job.Stdout.Len() > 0 {
Sugar.Info(job.Stdout.String())
} else {
Sugar.Info("No Output in stdout")
}

if job.Stderr.Len() > 0 {
Sugar.Info(job.Stderr.String())
} else {
Sugar.Info("No Output in stderr")
}
}

Expand All @@ -33,21 +44,10 @@ func (job *Job) IsFinished() bool {
func (job *Job) QueueStatus() {
job.finished = true
if job.Cmd != nil && job.Cmd.Process == nil {
log.Println("Process not found")
Sugar.Info("Process not found")
return
}

if job.Stdout.Len() > 0 {
Log(job.Stdout.String(), job.printOutput)
} else {
Log("No Output in stdout", job.printOutput)
}

if job.Stderr.Len() > 0 {
Log(job.Stderr.String(), job.printOutput)
} else {
Log("No Output in stderr", job.printOutput)
}
LogJobStatus(job)
}

func CreateJobFromFunction(f func() error, name string) Job {
Expand Down Expand Up @@ -75,7 +75,7 @@ func CreateJobFromCommand(cmd *exec.Cmd, name string) Job {
if ok {
oldCmd := v.(*Job)
if oldCmd.Cmd.Process != nil {
log.Println("Found job:", name, "\tPID: ", oldCmd.Cmd.Process.Pid)
Sugar.Info("Found job:", name, "\tPID: ", oldCmd.Cmd.Process.Pid)
}
}
}
Expand All @@ -96,7 +96,7 @@ func CreateJobFromCommand(cmd *exec.Cmd, name string) Job {

func (job *Job) RunJob(printOutput bool) error {
job.printOutput = printOutput
log.Println("Starting job: ", job.Name)
Sugar.Info("Starting job: ", job.Name)
return job.doJob()
}

Expand All @@ -109,12 +109,11 @@ func (job *Job) doJob() error {

func (job *Job) RunJobBackground(printOutput bool) error {
go func() {
log.Println("Starting job in background: ", job.Name)
Sugar.Info("Starting job in background: ", job.Name)
job.printOutput = printOutput
err := job.doJob()
if err != nil {
log.Println("ERROR: ", err)
log.Println(job.Stderr.String())
Sugar.Error("ERROR: ", err, "\n", job.Stderr.String())
}
}()
return nil
Expand All @@ -124,9 +123,9 @@ func (job *Job) DontRun(printOutput bool) error {
job.printOutput = printOutput

if job.Cmd != nil {
log.Println("Not Runing: ", job.Cmd)
Sugar.Info("Not Runing: ", job.Cmd)
} else {
log.Println("Not Runing: ", job.Name)
Sugar.Info("Not Runing: ", job.Name)
}

job.QueueStatus()
Expand Down
7 changes: 3 additions & 4 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"testing"
Expand Down Expand Up @@ -83,11 +82,11 @@ func TestJobQueueStatus(t *testing.T) {
job := CreateJobFromCommand(cmd, "test1")
var infoBuffer bytes.Buffer

log.SetOutput(&infoBuffer)
GetLogger().SetOutput(&infoBuffer)
job.QueueStatus()

assert.NotEmpty(t, infoBuffer)

log.SetOutput(os.Stdout)
log.Println("test: ", infoBuffer.String())
GetLogger().SetOutput(os.Stdout)
Sugar.Info("test: ", infoBuffer.String())
}
Loading

0 comments on commit 4c1c93f

Please sign in to comment.