improving logging (#5)

* small refactoring of error messages

* (very) small refactoring of structs instantiation

* improving logging with debug messages

* using INFO level as default + changing previous debug message to info message
This commit is contained in:
Julien Neuhart
2018-05-04 17:43:19 +02:00
committed by GitHub
parent 810d35ffbe
commit 2871fb1ffe
30 changed files with 1275 additions and 61 deletions

View File

@@ -9,6 +9,7 @@ import (
gfile "github.com/thecodingmachine/gotenberg/app/converter/file"
"github.com/thecodingmachine/gotenberg/app/converter/process"
"github.com/thecodingmachine/gotenberg/app/logger"
"github.com/satori/go.uuid"
)
@@ -22,7 +23,7 @@ type Converter struct {
// NoFileToConvertError is raided when a request has not file attached to it.
type NoFileToConvertError struct{}
const noFileToConvertErrorMessage = "No file to convert"
const noFileToConvertErrorMessage = "no file to convert"
func (e *NoFileToConvertError) Error() string {
return noFileToConvertErrorMessage
@@ -38,6 +39,8 @@ func NewConverter(r *http.Request) (*Converter, error) {
return nil, err
}
logger.Debugf("created working directory %s", c.workingDir)
reader, err := r.MultipartReader()
if err != nil {
return c, err
@@ -101,5 +104,11 @@ func (c *Converter) Convert() (string, error) {
// Clear removes all file inside its working directory.
func (c *Converter) Clear() error {
return os.RemoveAll(c.workingDir)
if err := os.RemoveAll(c.workingDir); err != nil {
logger.Error(fmt.Errorf("failed to remove working directory %s", c.workingDir))
return err
}
logger.Debugf("removed working directory %s", c.workingDir)
return nil
}

View File

@@ -48,6 +48,13 @@ func load(configurationFilePath string) {
config.ParseFile(path)
}
func TestNoFileToConvertError(t *testing.T) {
err := &NoFileToConvertError{}
if err.Error() != noFileToConvertErrorMessage {
t.Errorf("Error returned a wrong message: got '%s' want '%s'", err.Error(), noFileToConvertErrorMessage)
}
}
func TestNewConverter(t *testing.T) {
var (
path string
@@ -141,10 +148,3 @@ func TestClear(t *testing.T) {
t.Error("Converter should have been able to clear itself")
}
}
func TestNoFileToConvertError(t *testing.T) {
err := &NoFileToConvertError{}
if err.Error() != noFileToConvertErrorMessage {
t.Errorf("Error returned a wrong message: got '%s' want '%s'", err.Error(), noFileToConvertErrorMessage)
}
}

View File

@@ -8,7 +8,9 @@ import (
"path/filepath"
"github.com/thecodingmachine/gotenberg/app/config"
"github.com/thecodingmachine/gotenberg/app/logger"
"github.com/dustin/go-humanize"
"github.com/satori/go.uuid"
)
@@ -30,10 +32,7 @@ func NewFile(workingDir string, r io.Reader, fileName string) (*File, error) {
return nil, err
}
f := &File{
Extension: ext,
Path: MakeFilePath(workingDir, ext),
}
f := &File{ext, MakeFilePath(workingDir, ext)}
file, err := os.Create(f.Path)
if err != nil {
@@ -42,7 +41,7 @@ func NewFile(workingDir string, r io.Reader, fileName string) (*File, error) {
defer file.Close()
_, err = io.Copy(file, r)
n, err := io.Copy(file, r)
if err != nil {
return nil, err
}
@@ -50,6 +49,7 @@ func NewFile(workingDir string, r io.Reader, fileName string) (*File, error) {
// resets the read pointer.
file.Seek(0, 0)
logger.Debugf("working file %s has been created from %s (%s copied)", f.Path, fileName, humanize.Bytes(uint64(n)))
return f, nil
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/thecodingmachine/gotenberg/app/config"
gfile "github.com/thecodingmachine/gotenberg/app/converter/file"
"github.com/thecodingmachine/gotenberg/app/logger"
)
type runner struct {
@@ -23,8 +24,10 @@ type commandTimeoutError struct {
timeout int
}
const commandTimeoutErrorMessage = "the command '%s' has reached the %d second(s) timeout"
func (e *commandTimeoutError) Error() string {
return fmt.Sprintf("The command '%s' has reached the %d second(s) timeout", e.command, e.timeout)
return fmt.Sprintf(commandTimeoutErrorMessage, e.command, e.timeout)
}
// run runs the given command. If timeout is reached or
@@ -34,6 +37,8 @@ func (r *runner) run(command string, timeout int) error {
defer r.mu.Unlock()
cmd := exec.Command("/bin/sh", "-c", command)
logger.Debugf("executing command %s", cmd.Args)
if err := cmd.Start(); err != nil {
return err
}
@@ -49,10 +54,8 @@ func (r *runner) run(command string, timeout int) error {
if err := cmd.Process.Kill(); err != nil {
return err
}
return &commandTimeoutError{
command: command,
timeout: timeout,
}
return &commandTimeoutError{command, timeout}
case err := <-done:
if err != nil {
return err
@@ -70,10 +73,7 @@ type conversionData struct {
// Unconv converts a file to PDF and returns the new file path.
func Unconv(workingDir string, file *gfile.File) (string, error) {
cmdData := &conversionData{
FilePath: file.Path,
ResultFilePath: gfile.MakeFilePath(workingDir, ".pdf"),
}
cmdData := &conversionData{file.Path, gfile.MakeFilePath(workingDir, ".pdf")}
cmd, err := config.GetCommand(file.Extension)
if err != nil {
@@ -90,6 +90,7 @@ func Unconv(workingDir string, file *gfile.File) (string, error) {
return "", err
}
logger.Debugf("created %s from %s", cmdData.ResultFilePath, cmdData.FilePath)
return cmdData.ResultFilePath, nil
}
@@ -101,10 +102,7 @@ type mergeData struct {
// Merge merges many PDF files to one unique PDF file and returns the new file path.
func Merge(workingDir string, filesPaths []string) (string, error) {
cmdData := &mergeData{
FilesPaths: filesPaths,
ResultFilePath: gfile.MakeFilePath(workingDir, ".pdf"),
}
cmdData := &mergeData{filesPaths, gfile.MakeFilePath(workingDir, ".pdf")}
cmd, err := config.GetCommand(".pdf")
if err != nil {
@@ -121,5 +119,6 @@ func Merge(workingDir string, filesPaths []string) (string, error) {
return "", err
}
logger.Debugf("created %s from %+v", cmdData.ResultFilePath, cmdData.FilesPaths)
return cmdData.ResultFilePath, nil
}

View File

@@ -28,6 +28,15 @@ func load(configurationFilePath string) {
config.ParseFile(path)
}
func TestCommandTimeoutError(t *testing.T) {
err := &commandTimeoutError{"echo hello", 30}
expected := fmt.Sprintf(commandTimeoutErrorMessage, err.command, err.timeout)
if err.Error() != expected {
t.Errorf("Error returned a wrong message: got '%s' want '%s'", err.Error(), expected)
}
}
func TestRun(t *testing.T) {
var cmd string
@@ -118,14 +127,3 @@ func TestMerge(t *testing.T) {
os.RemoveAll(workingDir)
}
func TestCommandTimeoutError(t *testing.T) {
err := &commandTimeoutError{
command: "echo hello",
timeout: 30,
}
expected := fmt.Sprintf("The command '%s' has reached the %d second(s) timeout", err.command, err.timeout)
if err.Error() != expected {
t.Errorf("Error returned a wrong message: got '%s' want '%s'", err.Error(), expected)
}
}