WIP: refactoring logging system

This commit is contained in:
Julien Neuhart
2019-07-01 17:11:23 +02:00
parent 8ca9866440
commit a4aa8dafac
19 changed files with 225 additions and 391 deletions

View File

@@ -1,5 +0,0 @@
/*
Package notify helps displaying nice outputs
to the user.
*/
package notify

View File

@@ -1,35 +0,0 @@
package notify
import (
"fmt"
"os"
"github.com/labstack/gommon/color"
)
// Print prints a message to stdout.
func Print(message string) {
stdout := color.New()
stdout.SetOutput(os.Stdout)
stdout.Printf("⇨ %s\n", message)
}
// Printf prints a formatted message to stdout.
func Printf(format string, a ...interface{}) {
message := fmt.Sprintf(format, a...)
Print(message)
}
// WarnPrint prints a warning to stderr.
func WarnPrint(err error) {
stderr := color.New()
stderr.SetOutput(os.Stderr)
stderr.Printf("%s\n", color.Yellow(fmt.Sprintf("⇨ warn: %v", err)))
}
// ErrPrint prints an error to stderr.
func ErrPrint(err error) {
stderr := color.New()
stderr.SetOutput(os.Stderr)
stderr.Printf("%s\n", color.Red(fmt.Sprintf("⇨ error: %v", err)))
}

View File

@@ -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)
}

View File

@@ -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...)
}
}

View File

@@ -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},
}
}

View File

@@ -7,9 +7,9 @@ import (
"io/ioutil"
"path/filepath"
"github.com/labstack/gommon/random"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
"github.com/thecodingmachine/gotenberg/internal/pkg/rand"
)
// NewMarkdown returns a Markdown printer.
@@ -27,10 +27,7 @@ func NewMarkdown(fpath string, opts *ChromeOptions) (Printer, error) {
if err := tmpl.Execute(&buffer, data); err != nil {
return nil, fmt.Errorf("%s: executing template: %v", fpath, err)
}
baseFilename, err := rand.Get()
if err != nil {
return nil, err
}
baseFilename := random.String(32)
dst := fmt.Sprintf("%s/%s.html", dirPath, baseFilename)
if err := ioutil.WriteFile(dst, buffer.Bytes(), 0644); err != nil {
return nil, fmt.Errorf("%s: writing file: %v", dst, err)

View File

@@ -9,7 +9,7 @@ import (
"sync"
"time"
"github.com/thecodingmachine/gotenberg/internal/pkg/rand"
"github.com/labstack/gommon/random"
)
type office struct {
@@ -38,10 +38,7 @@ func (p *office) Print(destination string) error {
fpaths := make([]string, len(p.fpaths))
dirPath := filepath.Dir(destination)
for i, fpath := range p.fpaths {
baseFilename, err := rand.Get()
if err != nil {
return err
}
baseFilename := random.String(32)
tmpDest := fmt.Sprintf("%s/%d%s.pdf", dirPath, i, baseFilename)
if err := unoconv(ctx, fpath, tmpDest, p.opts); err != nil {
return err

View File

@@ -1,7 +0,0 @@
/*
Package rand helps generating a random string.
It should be used for creating directory and
file names in order to avoid collision.
*/
package rand

View File

@@ -1,17 +0,0 @@
package rand
import (
"crypto/rand"
"encoding/hex"
"fmt"
)
// Get returns a random string.
func Get() (string, error) {
randBytes := make([]byte, 16)
_, err := rand.Read(randBytes)
if err != nil {
return "", fmt.Errorf("creating random string: %v", err)
}
return hex.EncodeToString(randBytes), nil
}

View File

@@ -1,16 +0,0 @@
package rand
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGet(t *testing.T) {
rand1, err := Get()
require.Nil(t, err)
rand2, err := Get()
require.Nil(t, err)
assert.NotEqual(t, rand1, rand2)
}