Add event hook for sending logs to frontend

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer
2019-12-02 00:30:58 +01:00
parent 063be1650e
commit a2963e9fc6
15 changed files with 128 additions and 54 deletions

View File

@@ -8,23 +8,32 @@
slider-color="blue-grey darken-1"
height="64"
>
<v-tab id="tab-upload">
<v-tab id="tab-settings-general">
General
</v-tab>
<v-tab-item>
<p-tab-general></p-tab-general>
</v-tab-item>
<v-tab id="tab-settings-logs">
Logs
</v-tab>
<v-tab-item>
<p-tab-logs></p-tab-logs>
</v-tab-item>
</v-tabs>
</div>
</template>
<script>
import tabGeneral from "pages/settings/general.vue";
import tabLogs from "pages/settings/logs.vue";
export default {
name: 'p-page-settings',
components: {
'p-tab-general': tabGeneral,
'p-tab-logs': tabLogs,
},
data() {
return {

View File

@@ -0,0 +1,27 @@
<template>
<div class="p-tab p-tab-logs">
<v-container fluid>
<p>Work in progress</p>
</v-container>
</div>
</template>
<script>
import Settings from "model/settings";
import options from "resources/options.json";
export default {
name: 'p-tab-logs',
data() {
return {
};
},
methods: {
load() {
},
},
created() {
this.load();
},
};
</script>

View File

@@ -7,10 +7,8 @@ https://github.com/photoprism/photoprism/wiki
*/
package api
import "github.com/sirupsen/logrus"
import (
"github.com/photoprism/photoprism/internal/event"
)
var log *logrus.Logger
func init() {
log = logrus.StandardLogger()
}
var log = event.Log

View File

@@ -30,7 +30,7 @@ func wsReader(ws *websocket.Conn) {
func wsWriter(ws *websocket.Conn, conf *config.Config) {
pingTicker := time.NewTicker(10 * time.Second)
s := event.Subscribe("notify.*", "index.*", "upload.*", "import.*", "config.*")
s := event.Subscribe("log.*", "notify.*", "index.*", "upload.*", "import.*", "config.*")
defer func() {
pingTicker.Stop()

View File

@@ -11,16 +11,12 @@ import (
"os"
"syscall"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/util"
"github.com/sevlyar/go-daemon"
"github.com/sirupsen/logrus"
)
var log *logrus.Logger
func init() {
log = logrus.StandardLogger()
}
var log = event.Log
func childAlreadyRunning(filePath string) (pid int, running bool) {
if !util.Exists(filePath) {

View File

@@ -11,31 +11,34 @@ import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
gc "github.com/patrickmn/go-cache"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/models"
"github.com/photoprism/photoprism/internal/tidb"
"github.com/photoprism/photoprism/internal/util"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
tensorflow "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/urfave/cli"
gc "github.com/patrickmn/go-cache"
)
var log = event.Log
type Config struct {
db *gorm.DB
cache *gc.Cache
cache *gc.Cache
config *Params
}
func initLogger(debug bool) {
log.SetFormatter(&log.TextFormatter{
log.SetFormatter(&logrus.TextFormatter{
DisableColors: false,
FullTimestamp: true,
})
if debug {
log.SetLevel(log.DebugLevel)
log.SetLevel(logrus.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
log.SetLevel(logrus.InfoLevel)
}
}
@@ -223,15 +226,15 @@ func (c *Config) AdminPassword() string {
}
// LogLevel returns the logrus log level.
func (c *Config) LogLevel() log.Level {
func (c *Config) LogLevel() logrus.Level {
if c.Debug() {
c.config.LogLevel = "debug"
}
if logLevel, err := log.ParseLevel(c.config.LogLevel); err == nil {
if logLevel, err := logrus.ParseLevel(c.config.LogLevel); err == nil {
return logLevel
} else {
return log.InfoLevel
return logrus.InfoLevel
}
}

View File

@@ -9,7 +9,6 @@ import (
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/photoprism/photoprism/internal/util"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)

View File

@@ -10,9 +10,8 @@ import (
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/photoprism/photoprism/internal/util"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
log "github.com/sirupsen/logrus"
)
const (
@@ -76,7 +75,7 @@ func TestConfig() *Config {
}
func NewTestConfig() *Config {
log.SetLevel(log.DebugLevel)
log.SetLevel(logrus.DebugLevel)
c := &Config{config: NewTestParams()}
err := c.Init(context.Background())
@@ -89,7 +88,7 @@ func NewTestConfig() *Config {
}
func NewTestErrorConfig() *Config {
log.SetLevel(log.DebugLevel)
log.SetLevel(logrus.DebugLevel)
c := &Config{config: NewTestParamsError()}
err := c.Init(context.Background())

View File

@@ -2,19 +2,15 @@ package event
import (
"github.com/leandro-lugaresi/hub"
"github.com/sirupsen/logrus"
)
type Hub = hub.Hub
type Data = hub.Fields
type Message = hub.Message
var log *logrus.Logger
var channelCap = 10
var sharedHub = NewHub()
func init() {
log = logrus.StandardLogger()
}
func NewHub () *Hub {
return hub.New()
@@ -25,27 +21,26 @@ func SharedHub() *Hub {
}
func Error(msg string) {
log.Error(msg)
Log.Error(msg)
Publish("notify.error", Data{"msg": msg})
}
func Success(msg string) {
log.Info(msg)
Log.Info(msg)
Publish("notify.success", Data{"msg": msg})
}
func Info(msg string) {
log.Info(msg)
Log.Info(msg)
Publish("notify.info", Data{"msg": msg})
}
func Warning(msg string) {
log.Warn(msg)
Log.Warn(msg)
Publish("notify.warning", Data{"msg": msg})
}
func Publish (event string, data Data) {
log.Infof("publish %s: %v", event, data)
SharedHub().Publish(Message{
Name: event,
Fields: data,

50
internal/event/log.go Normal file
View File

@@ -0,0 +1,50 @@
package event
import (
"os"
"github.com/leandro-lugaresi/hub"
"github.com/sirupsen/logrus"
)
var Log *logrus.Logger
type Hook struct {
hub *hub.Hub
}
func NewHook(hub *hub.Hub) *Hook {
return &Hook{hub: hub}
}
func (h *Hook) Fire(entry *logrus.Entry) error {
h.hub.Publish(Message{
Name: "log." + entry.Level.String(),
Fields: Data{
"time": entry.Time,
"level": entry.Level.String(),
"msg": entry.Message,
},
})
return nil
}
func (h *Hook) Levels() []logrus.Level {
return logrus.AllLevels
}
func init() {
hooks := logrus.LevelHooks{}
hooks.Add(NewHook(SharedHub()))
Log = &logrus.Logger{
Out: os.Stderr,
Formatter: &logrus.TextFormatter{},
Hooks: hooks,
Level: logrus.DebugLevel,
ExitFunc: os.Exit,
ReportCaller: false,
}
}

View File

@@ -7,10 +7,8 @@ https://github.com/photoprism/photoprism/wiki
*/
package photoprism
import "github.com/sirupsen/logrus"
import (
"github.com/photoprism/photoprism/internal/event"
)
var log *logrus.Logger
func init() {
log = logrus.StandardLogger()
}
var log = event.Log

View File

@@ -7,9 +7,11 @@ import (
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config"
log "github.com/sirupsen/logrus"
"github.com/photoprism/photoprism/internal/event"
)
var log = event.Log
// Start the REST API server using the configuration provided
func Start(ctx context.Context, conf *config.Config) {
if conf.HttpServerMode() != "" {

View File

@@ -24,6 +24,7 @@ import (
"time"
"github.com/opentracing/opentracing-go"
"github.com/photoprism/photoprism/internal/event"
"github.com/pingcap/errors"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
@@ -49,9 +50,10 @@ import (
xserver "github.com/pingcap/tidb/x-server"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
log "github.com/sirupsen/logrus"
)
var log = event.Log
var (
cfg *config.Config
storage kv.Storage

View File

@@ -3,8 +3,6 @@ package tidb
import (
"database/sql"
"fmt"
log "github.com/sirupsen/logrus"
)
func InitDatabase(port uint, password string) error {

View File

@@ -7,10 +7,8 @@ https://github.com/photoprism/photoprism/wiki
*/
package util
import "github.com/sirupsen/logrus"
import (
"github.com/photoprism/photoprism/internal/event"
)
var log *logrus.Logger
func init() {
log = logrus.StandardLogger()
}
var log = event.Log