Skip to content

Commit

Permalink
Improve UI logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DingDongSoLong4 committed Apr 27, 2023
1 parent 55d3dee commit 57a4978
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 114 deletions.
4 changes: 2 additions & 2 deletions graphql/documents/queries/misc.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ query Stats {
}
}

query Logs {
logs {
query Logs($minLevel: LogLevel) {
logs(minLevel: $minLevel) {
...LogEntryData
}
}
Expand Down
6 changes: 3 additions & 3 deletions graphql/documents/subscriptions.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ subscription JobsSubscribe {
}
}

subscription LoggingSubscribe {
loggingSubscribe {
subscription LoggingSubscribe($minLevel: LogLevel) {
loggingSubscribe(minLevel: $minLevel) {
...LogEntryData
}
}

subscription ScanCompleteSubscribe {
scanCompleteSubscribe
}
}
12 changes: 10 additions & 2 deletions graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ type Query {
"""Organize scene markers by tag for a given scene ID"""
sceneMarkerTags(scene_id: ID!): [SceneMarkerTag!]!

logs: [LogEntry!]!
""""Retrieve server log messages"""
logs(
"""Specifies minimum LogLevel of messages to return. Returns all messages if omitted."""
minLevel: LogLevel
): [LogEntry!]!

# Scrapers

Expand Down Expand Up @@ -344,7 +348,11 @@ type Subscription {
"""Update from the metadata manager"""
jobsSubscribe: JobStatusUpdate!

loggingSubscribe: [LogEntry!]!
""""Subscription to receive server log messages"""
loggingSubscribe(
"""Specifies minimum LogLevel of messages to return. Returns all messages if omitted."""
minLevel: LogLevel
): [LogEntry!]!

scanCompleteSubscribe: Boolean!
}
Expand Down
4 changes: 2 additions & 2 deletions graphql/schema/types/logging.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
scalar Time

enum LogLevel {
Progress
Trace
Debug
Info
Progress
Warning
Error
}
Expand All @@ -14,4 +14,4 @@ type LogEntry {
time: Time!
level: LogLevel!
message: String!
}
}
10 changes: 8 additions & 2 deletions internal/api/resolver_query_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import (
"context"

"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/pkg/models"
)

func (r *queryResolver) Logs(ctx context.Context) ([]*LogEntry, error) {
func (r *queryResolver) Logs(ctx context.Context, minLevel *models.LogLevel) ([]*LogEntry, error) {
logger := manager.GetInstance().Logger
logCache := logger.GetLogCache()
level := ""
if minLevel != nil {
level = minLevel.String()
}
logCache := logger.GetLogCache(level)

ret := make([]*LogEntry, len(logCache))

for i, entry := range logCache {
Expand Down
37 changes: 21 additions & 16 deletions internal/api/resolver_subscription_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,47 @@ import (

"github.com/stashapp/stash/internal/log"
"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/pkg/models"
)

func getLogLevel(logType string) LogLevel {
func getLogLevel(logType string) models.LogLevel {
switch logType {
case "progress":
return LogLevelProgress
return models.LogLevelProgress
case "trace":
return LogLevelTrace
return models.LogLevelTrace
case "debug":
return LogLevelDebug
return models.LogLevelDebug
case "info":
return LogLevelInfo
return models.LogLevelInfo
case "warn":
return LogLevelWarning
return models.LogLevelWarning
case "error":
return LogLevelError
return models.LogLevelError
default:
return LogLevelDebug
return models.LogLevelDebug
}
}

func logEntriesFromLogItems(logItems []log.LogItem) []*LogEntry {
ret := make([]*LogEntry, len(logItems))
func logEntriesFromLogItems(logItems []log.LogItem, minLevel *models.LogLevel) []*LogEntry {
ret := make([]*LogEntry, 0, len(logItems))

for i, entry := range logItems {
ret[i] = &LogEntry{
for _, entry := range logItems {
level := getLogLevel(entry.Type)
if minLevel != nil && level < *minLevel {
continue
}
ret = append(ret, &LogEntry{
Time: entry.Time,
Level: getLogLevel(entry.Type),
Level: level,
Message: entry.Message,
}
})
}

return ret
}

func (r *subscriptionResolver) LoggingSubscribe(ctx context.Context) (<-chan []*LogEntry, error) {
func (r *subscriptionResolver) LoggingSubscribe(ctx context.Context, minLevel *models.LogLevel) (<-chan []*LogEntry, error) {
ret := make(chan []*LogEntry, 100)
stop := make(chan int, 1)
logger := manager.GetInstance().Logger
Expand All @@ -50,7 +55,7 @@ func (r *subscriptionResolver) LoggingSubscribe(ctx context.Context) (<-chan []*
for {
select {
case logEntries := <-logSub:
ret <- logEntriesFromLogItems(logEntries)
ret <- logEntriesFromLogItems(logEntries, minLevel)
case <-ctx.Done():
stop <- 0
close(ret)
Expand Down
Loading

0 comments on commit 57a4978

Please sign in to comment.