Skip to content

Commit

Permalink
Use timestamp instead of unique ids for logging output data
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 2, 2023
1 parent bb10d59 commit 5fc2139
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions cli/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package executor
import (
"crypto/tls"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -20,7 +19,6 @@ import (
"github.com/essentialkaos/ek/v12/errutil"
"github.com/essentialkaos/ek/v12/fsutil"
"github.com/essentialkaos/ek/v12/log"
"github.com/essentialkaos/ek/v12/passwd"
"github.com/essentialkaos/ek/v12/req"
"github.com/essentialkaos/ek/v12/sliceutil"
"github.com/essentialkaos/ek/v12/strutil"
Expand Down Expand Up @@ -470,7 +468,7 @@ func skipCommand(c *recipe.Command, tags []string, lastSkippedGroupID uint8, fin
return !sliceutil.Contains(tags, c.Tag) && !sliceutil.Contains(tags, "*")
}

// logError logs error data
// logError saves output data into a file
func logError(e *Executor, c *recipe.Command, a *recipe.Action, ce *CommandEnv, err error) {
if e.config.ErrsDir == "" {
return
Expand All @@ -487,14 +485,14 @@ func logError(e *Executor, c *recipe.Command, a *recipe.Action, ce *CommandEnv,
}
}

id := passwd.GenPassword(8, passwd.STRENGTH_MEDIUM)
origin := getErrorOrigin(c, a, id)
ts := time.Now().UnixMicro()
origin := getErrorOrigin(c, a, ts)

e.logger.Info("(%s) %v", origin, err)

if ce != nil && !ce.output.IsEmpty() {
output := fmt.Sprintf("%s-output-%s.log", recipeName, id)
err := ioutil.WriteFile(fmt.Sprintf("%s/%s", e.config.ErrsDir, output), ce.output.Bytes(), 0644)
output := fmt.Sprintf("%s-output-%d.log", recipeName, ts)
err := os.WriteFile(fmt.Sprintf("%s/%s", e.config.ErrsDir, output), ce.output.Bytes(), 0644)

if err != nil {
e.logger.Info("(%s) Can't save output data: %v", origin, err)
Expand All @@ -503,17 +501,17 @@ func logError(e *Executor, c *recipe.Command, a *recipe.Action, ce *CommandEnv,
}

// getErrorOrigin returns info about error origin
func getErrorOrigin(c *recipe.Command, a *recipe.Action, id string) string {
func getErrorOrigin(c *recipe.Command, a *recipe.Action, ts int64) string {
switch a {
case nil:
return fmt.Sprintf(
"id: %s | command: %d | line: %d",
id, c.Index()+1, c.Line,
"ts: %d | command: %d | line: %d",
ts, c.Index()+1, c.Line,
)
default:
return fmt.Sprintf(
"id: %s | command: %d | action: %d:%s | line: %d",
id, c.Index()+1, a.Index()+1, a.Name, a.Line,
"ts: %d | command: %d | action: %d:%s | line: %d",
ts, c.Index()+1, a.Index()+1, a.Name, a.Line,
)
}
}
Expand Down

0 comments on commit 5fc2139

Please sign in to comment.