Scraper log improvements (#1741)

* Fix logs from scraper and plugins not being shown in UI

Using `logger.` in the logger package to write logs is "incorrect". This
as the package contains a variable named `logger` which contains the
logrus instance. So instead of the log line being handled by the custom
log implementation / wrapper which makes sure the lines are shown in the
UI as well, it's written to logrus directly meaning the wrapper is
skipped.

This "issue" is obviously triggered because in any other place
`logger.X` can be used and it will used the custom logger package /
wrapper which works fine.

* Add plugin / scraper name to logging output

Indicate which plugin / scraper wrote a log message by including its
name to the `[Scrape]` prefix.

* Add missing addLogItem call
This commit is contained in:
gitgiggety 2021-09-19 02:06:34 +02:00 committed by GitHub
parent 66f92c5dcc
commit b83ce29ac4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 15 deletions

View file

@ -182,6 +182,11 @@ func Progressf(format string, args ...interface{}) {
func Trace(args ...interface{}) { func Trace(args ...interface{}) {
logger.Trace(args...) logger.Trace(args...)
l := &LogItem{
Type: "trace",
Message: fmt.Sprint(args...),
}
addLogItem(l)
} }
func Tracef(format string, args ...interface{}) { func Tracef(format string, args ...interface{}) {

View file

@ -146,19 +146,19 @@ func (log *PluginLogger) HandleStderrLine(line string) {
switch *level { switch *level {
case TraceLevel: case TraceLevel:
logger.Trace(log.Prefix, ll) Trace(log.Prefix, ll)
case DebugLevel: case DebugLevel:
logger.Debug(log.Prefix, ll) Debug(log.Prefix, ll)
case InfoLevel: case InfoLevel:
logger.Info(log.Prefix, ll) Info(log.Prefix, ll)
case WarningLevel: case WarningLevel:
logger.Warn(log.Prefix, ll) Warn(log.Prefix, ll)
case ErrorLevel: case ErrorLevel:
logger.Error(log.Prefix, ll) Error(log.Prefix, ll)
case ProgressLevel: case ProgressLevel:
p, err := strconv.ParseFloat(ll, 64) p, err := strconv.ParseFloat(ll, 64)
if err != nil { if err != nil {
logger.Errorf("Error parsing progress value '%s': %s", ll, err.Error()) Errorf("Error parsing progress value '%s': %s", ll, err.Error())
} else { } else {
// only pass progress through if channel present // only pass progress through if channel present
if log.ProgressChan != nil { if log.ProgressChan != nil {

View file

@ -1,22 +1,23 @@
package plugin package plugin
import ( import (
"fmt"
"io" "io"
"github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/logger"
) )
func (t *pluginTask) handlePluginStderr(pluginOutputReader io.ReadCloser) { func (t *pluginTask) handlePluginStderr(name string, pluginOutputReader io.ReadCloser) {
logLevel := logger.PluginLogLevelFromName(t.plugin.PluginErrLogLevel) logLevel := logger.PluginLogLevelFromName(t.plugin.PluginErrLogLevel)
if logLevel == nil { if logLevel == nil {
// default log level to error // default log level to error
logLevel = &logger.ErrorLevel logLevel = &logger.ErrorLevel
} }
const pluginPrefix = "[Plugin] " const pluginPrefix = "[Plugin / %s] "
lgr := logger.PluginLogger{ lgr := logger.PluginLogger{
Prefix: pluginPrefix, Prefix: fmt.Sprintf(pluginPrefix, name),
DefaultLogLevel: logLevel, DefaultLogLevel: logLevel,
ProgressChan: t.progress, ProgressChan: t.progress,
} }

View file

@ -70,7 +70,7 @@ func (t *rawPluginTask) Start() error {
return fmt.Errorf("error running plugin: %s", err.Error()) return fmt.Errorf("error running plugin: %s", err.Error())
} }
go t.handlePluginStderr(stderr) go t.handlePluginStderr(t.plugin.Name, stderr)
t.cmd = cmd t.cmd = cmd
// send the stdout to the plugin output // send the stdout to the plugin output

View file

@ -64,7 +64,7 @@ func (t *rpcPluginTask) Start() error {
return err return err
} }
go t.handlePluginStderr(pluginErrReader) go t.handlePluginStderr(t.plugin.Name, pluginErrReader)
iface := rpcPluginClient{ iface := rpcPluginClient{
Client: t.client, Client: t.client,

View file

@ -3,6 +3,7 @@ package scraper
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io" "io"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -65,7 +66,7 @@ func (s *scriptScraper) runScraperScript(inString string, out interface{}) error
return errors.New("error running scraper script") return errors.New("error running scraper script")
} }
go handleScraperStderr(stderr) go handleScraperStderr(s.config.Name, stderr)
logger.Debugf("Scraper script <%s> started", strings.Join(cmd.Args, " ")) logger.Debugf("Scraper script <%s> started", strings.Join(cmd.Args, " "))
@ -248,11 +249,11 @@ func findPythonExecutable() (string, error) {
return "python3", nil return "python3", nil
} }
func handleScraperStderr(scraperOutputReader io.ReadCloser) { func handleScraperStderr(name string, scraperOutputReader io.ReadCloser) {
const scraperPrefix = "[Scrape] " const scraperPrefix = "[Scrape / %s] "
lgr := logger.PluginLogger{ lgr := logger.PluginLogger{
Prefix: scraperPrefix, Prefix: fmt.Sprintf(scraperPrefix, name),
DefaultLogLevel: &logger.ErrorLevel, DefaultLogLevel: &logger.ErrorLevel,
} }
lgr.HandlePluginStdErr(scraperOutputReader) lgr.HandlePluginStdErr(scraperOutputReader)