MM-18258 Converting to structured logging the file utils/html.go (#12134)
* Converting to structured logging the file utils/html.go * using any instead of type assertion
Этот коммит содержится в:
коммит произвёл
Miguel de la Cruz
родитель
9f54e8267f
Коммит
4a3d7b9389
@@ -6,7 +6,6 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -28,7 +27,7 @@ type HTMLTemplateWatcher struct {
|
|||||||
|
|
||||||
func NewHTMLTemplateWatcher(directory string) (*HTMLTemplateWatcher, error) {
|
func NewHTMLTemplateWatcher(directory string) (*HTMLTemplateWatcher, error) {
|
||||||
templatesDir, _ := fileutils.FindDir(directory)
|
templatesDir, _ := fileutils.FindDir(directory)
|
||||||
mlog.Debug(fmt.Sprintf("Parsing server templates at %v", templatesDir))
|
mlog.Debug("Parsing server templates", mlog.String("templates_directory", templatesDir))
|
||||||
|
|
||||||
ret := &HTMLTemplateWatcher{
|
ret := &HTMLTemplateWatcher{
|
||||||
stop: make(chan struct{}),
|
stop: make(chan struct{}),
|
||||||
@@ -60,15 +59,15 @@ func NewHTMLTemplateWatcher(directory string) (*HTMLTemplateWatcher, error) {
|
|||||||
return
|
return
|
||||||
case event := <-watcher.Events:
|
case event := <-watcher.Events:
|
||||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||||
mlog.Info(fmt.Sprintf("Re-parsing templates because of modified file %v", event.Name))
|
mlog.Info("Re-parsing templates because of modified file", mlog.String("file_name", event.Name))
|
||||||
if htmlTemplates, err := template.ParseGlob(filepath.Join(templatesDir, "*.html")); err != nil {
|
if htmlTemplates, err := template.ParseGlob(filepath.Join(templatesDir, "*.html")); err != nil {
|
||||||
mlog.Error(fmt.Sprintf("Failed to parse templates %v", err))
|
mlog.Error("Failed to parse templates.", mlog.Err(err))
|
||||||
} else {
|
} else {
|
||||||
ret.templates.Store(htmlTemplates)
|
ret.templates.Store(htmlTemplates)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case err := <-watcher.Errors:
|
case err := <-watcher.Errors:
|
||||||
mlog.Error(fmt.Sprintf("Failed in directory watcher %s", err))
|
mlog.Error("Failed in directory watcher", mlog.Err(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -113,7 +112,7 @@ func (t *HTMLTemplate) RenderToWriter(w io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := t.Templates.ExecuteTemplate(w, t.TemplateName, t); err != nil {
|
if err := t.Templates.ExecuteTemplate(w, t.TemplateName, t); err != nil {
|
||||||
mlog.Error(fmt.Sprintf("Error rendering template %v err=%v", t.TemplateName, err))
|
mlog.Error("Error rendering template", mlog.String("template_name", t.TemplateName), mlog.Err(err))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,7 +139,11 @@ func escapeForHtml(arg interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
return safeArg
|
return safeArg
|
||||||
default:
|
default:
|
||||||
mlog.Warn(fmt.Sprintf("Unable to escape value for HTML template %v of type %v", arg, reflect.ValueOf(arg).Type()))
|
mlog.Warn(
|
||||||
|
"Unable to escape value for HTML template",
|
||||||
|
mlog.Any("html_template", arg),
|
||||||
|
mlog.String("template_type", reflect.ValueOf(arg).Type().String()),
|
||||||
|
)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user