mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-15 11:52:14 +01:00
WIP: refactoring logging system
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/mafredri/cdp/devtool"
|
||||
log "github.com/thecodingmachine/gotenberg/internal/pkg/logger"
|
||||
)
|
||||
|
||||
const warmupTime = 10 * time.Second
|
||||
@@ -15,9 +16,9 @@ type chrome struct {
|
||||
|
||||
// NewChrome returns a Google Chrome
|
||||
// headless process.
|
||||
func NewChrome(debug bool) Process {
|
||||
func NewChrome(logger *log.StandardLogger) Process {
|
||||
return &chrome{
|
||||
manager: &processManager{verbose: debug},
|
||||
manager: &processManager{logger: logger},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,19 +61,18 @@ func (p *chrome) viable() bool {
|
||||
// check if Google Chrome is correctly running.
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
p.manager.notifyf(`%v: checking Chrome liveness via debug version endpoint
|
||||
'http://localhost:9222/json/version'`, p.name())
|
||||
p.manager.logger.Debugf("%s: checking liveness via debug version endpoint http://localhost:9222/json/version", p.Fullname())
|
||||
v, err := devtool.New("http://localhost:9222").Version(ctx)
|
||||
if err != nil {
|
||||
p.manager.notifyf("%s: %s version endpoint returned error: %v", p.name(), p.Fullname(), err)
|
||||
p.manager.logger.Debugf("%s: debug version endpoint returned error: %v", p.Fullname(), err)
|
||||
return false
|
||||
}
|
||||
p.manager.notifyf("%s: %s returned version info: %+v", p.name(), p.Fullname(), *v)
|
||||
p.manager.logger.Debugf("%s: debug version endpoint returned version info: %+v", p.Fullname(), *v)
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *chrome) warmup() {
|
||||
p.manager.notifyf("%s: allowing %s %v to startup", p.name(), p.Fullname(), warmupTime)
|
||||
p.manager.logger.Debugf("%s: allowing %v to startup", p.Fullname(), warmupTime)
|
||||
time.Sleep(warmupTime)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,8 @@ import (
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/notify"
|
||||
log "github.com/thecodingmachine/gotenberg/internal/pkg/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -31,7 +30,7 @@ type Process interface {
|
||||
|
||||
type processManager struct {
|
||||
heuristicState int32
|
||||
verbose bool
|
||||
logger *log.StandardLogger
|
||||
}
|
||||
|
||||
func (m *processManager) start(p Process) error {
|
||||
@@ -83,43 +82,35 @@ func (m *processManager) pm2(p Process, cmdName string) error {
|
||||
"pm2",
|
||||
cmdArgs...,
|
||||
)
|
||||
m.notifyf("executing command '%v'", strings.Join(cmd.Args, " "))
|
||||
if m.verbose {
|
||||
processStdErr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting stderr from %s: %s", p.Fullname(), err)
|
||||
}
|
||||
processStdOut, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting stdout from %s: %s", p.Fullname(), err)
|
||||
}
|
||||
readFromPipe := func(name string, reader io.ReadCloser) {
|
||||
r := bufio.NewReader(reader)
|
||||
defer reader.Close() // nolint: errcheck
|
||||
for {
|
||||
line, _, err := r.ReadLine()
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
m.notifyf("error reading from %s for process %s", name, p.Fullname())
|
||||
}
|
||||
break
|
||||
}
|
||||
if len(line) != 0 {
|
||||
m.notifyf("%s %s: %s", p.name(), name, string(line))
|
||||
m.logger.Debugf("executing command: %v", strings.Join(cmd.Args, " "))
|
||||
processStdOut, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting stdout from %s: %s", p.Fullname(), err)
|
||||
}
|
||||
processStdErr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting stderr from %s: %s", p.Fullname(), err)
|
||||
}
|
||||
readFromPipe := func(outputType string, reader io.ReadCloser) {
|
||||
r := bufio.NewReader(reader)
|
||||
defer reader.Close() // nolint: errcheck
|
||||
for {
|
||||
line, _, err := r.ReadLine()
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
m.logger.Errorf("error reading from %s for process %s", outputType, p.Fullname())
|
||||
}
|
||||
break
|
||||
}
|
||||
if len(line) != 0 {
|
||||
m.logger.Debugf("%s %s: %s", p.Fullname(), outputType, string(line))
|
||||
}
|
||||
}
|
||||
go readFromPipe("stdout", processStdOut)
|
||||
go readFromPipe("stderr", processStdErr)
|
||||
}
|
||||
go readFromPipe("stdout", processStdOut)
|
||||
go readFromPipe("stderr", processStdErr)
|
||||
if err := cmd.Start(); err != nil {
|
||||
return fmt.Errorf("%s %s with PM2: %v", cmdName, p.Fullname(), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *processManager) notifyf(format string, args ...interface{}) {
|
||||
if m.verbose {
|
||||
notify.Printf(fmt.Sprintf("%v: %s", time.Now().Format(time.RFC3339), format), args...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
package pm2
|
||||
|
||||
import (
|
||||
log "github.com/thecodingmachine/gotenberg/internal/pkg/logger"
|
||||
)
|
||||
|
||||
type unoconv struct {
|
||||
manager *processManager
|
||||
}
|
||||
|
||||
// NewUnoconv returns a unoconv listener
|
||||
// process.
|
||||
func NewUnoconv(debug bool) Process {
|
||||
func NewUnoconv(logger *log.StandardLogger) Process {
|
||||
return &unoconv{
|
||||
manager: &processManager{verbose: debug},
|
||||
manager: &processManager{logger: logger},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user