Log container stdout at trace level
This commit is contained in:
parent
603e91af6f
commit
f75c70db94
37
container.go
37
container.go
@ -5,6 +5,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -64,6 +65,12 @@ func (c container) Build(imagePath string) (output string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c container) Run(workdir string, command string) (output string, err error) {
|
func (c container) Run(workdir string, command string) (output string, err error) {
|
||||||
|
flog := log.With().
|
||||||
|
Str("container", c.name).
|
||||||
|
Str("workdir", workdir).
|
||||||
|
Str("command", command).
|
||||||
|
Logger()
|
||||||
|
|
||||||
var args []string
|
var args []string
|
||||||
args = append(args, "run", "--rm")
|
args = append(args, "run", "--rm")
|
||||||
args = append(args, c.Args...)
|
args = append(args, c.Args...)
|
||||||
@ -78,23 +85,39 @@ func (c container) Run(workdir string, command string) (output string, err error
|
|||||||
|
|
||||||
log.Debug().Msgf("%v", cmd)
|
log.Debug().Msgf("%v", cmd)
|
||||||
|
|
||||||
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cmd.Stderr = cmd.Stdout
|
||||||
|
|
||||||
timer := time.AfterFunc(c.timeout, func() {
|
timer := time.AfterFunc(c.timeout, func() {
|
||||||
log.Info().Str("container", c.name).
|
flog.Info().Msg("killing container by timeout")
|
||||||
Str("workdir", workdir).
|
|
||||||
Str("command", command).
|
|
||||||
Msg("killing container by timeout")
|
|
||||||
cmd.Process.Kill()
|
cmd.Process.Kill()
|
||||||
})
|
})
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
|
|
||||||
raw, err := cmd.CombinedOutput()
|
err = cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
scanner := bufio.NewScanner(stdout)
|
||||||
|
for scanner.Scan() {
|
||||||
|
m := scanner.Text()
|
||||||
|
output += m + "\n"
|
||||||
|
flog.Trace().Str("stdout", m).Msg("")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = cmd.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e := fmt.Sprintf("error `%v` for cmd `%v` with output `%v`",
|
e := fmt.Sprintf("error `%v` for cmd `%v` with output `%v`",
|
||||||
err, command, string(raw))
|
err, command, output)
|
||||||
err = errors.New(e)
|
err = errors.New(e)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
output = string(raw)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
8
main.go
8
main.go
@ -42,14 +42,14 @@ type CLI struct {
|
|||||||
|
|
||||||
Version VersionFlag `name:"version" help:"print version information and quit"`
|
Version VersionFlag `name:"version" help:"print version information and quit"`
|
||||||
|
|
||||||
LogLevel LogLevelFlag `enum:"debug,info,warn,error" default:"info"`
|
LogLevel LogLevelFlag `enum:"trace,debug,info,warn,error" default:"info"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LogLevelFlag string
|
type LogLevelFlag string
|
||||||
|
|
||||||
func (loglevel LogLevelFlag) AfterApply() error {
|
func (loglevel LogLevelFlag) AfterApply() error {
|
||||||
switch loglevel {
|
switch loglevel {
|
||||||
case "debug":
|
case "debug", "trace":
|
||||||
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
||||||
short := file
|
short := file
|
||||||
for i := len(file) - 1; i > 0; i-- {
|
for i := len(file) - 1; i > 0; i-- {
|
||||||
@ -106,6 +106,8 @@ func main() {
|
|||||||
|
|
||||||
var loglevel zerolog.Level
|
var loglevel zerolog.Level
|
||||||
switch cli.LogLevel {
|
switch cli.LogLevel {
|
||||||
|
case "trace":
|
||||||
|
loglevel = zerolog.TraceLevel
|
||||||
case "debug":
|
case "debug":
|
||||||
loglevel = zerolog.DebugLevel
|
loglevel = zerolog.DebugLevel
|
||||||
case "info":
|
case "info":
|
||||||
@ -132,7 +134,7 @@ func main() {
|
|||||||
&LevelWriter{Writer: &lumberjack.Logger{
|
&LevelWriter{Writer: &lumberjack.Logger{
|
||||||
Filename: usr.HomeDir + "/.out-of-tree/logs/out-of-tree.log",
|
Filename: usr.HomeDir + "/.out-of-tree/logs/out-of-tree.log",
|
||||||
},
|
},
|
||||||
Level: zerolog.DebugLevel,
|
Level: zerolog.TraceLevel,
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user