Fix: make log api unblocked
This commit is contained in:
parent
6a92c6af4e
commit
aef4dd3fe7
2 changed files with 19 additions and 7 deletions
|
@ -208,16 +208,27 @@ func getLogs(w http.ResponseWriter, r *http.Request) {
|
||||||
render.Status(r, http.StatusOK)
|
render.Status(r, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ch := make(chan log.Event, 1024)
|
||||||
sub := log.Subscribe()
|
sub := log.Subscribe()
|
||||||
defer log.UnSubscribe(sub)
|
defer log.UnSubscribe(sub)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
var err error
|
|
||||||
|
go func() {
|
||||||
for elm := range sub {
|
for elm := range sub {
|
||||||
buf.Reset()
|
log := elm.(log.Event)
|
||||||
log := elm.(*log.Event)
|
select {
|
||||||
|
case ch <- log:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
|
||||||
|
for log := range ch {
|
||||||
if log.LogLevel < level {
|
if log.LogLevel < level {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
if err := json.NewEncoder(buf).Encode(Log{
|
if err := json.NewEncoder(buf).Encode(Log{
|
||||||
Type: log.Type(),
|
Type: log.Type(),
|
||||||
|
@ -226,6 +237,7 @@ func getLogs(w http.ResponseWriter, r *http.Request) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
if wsConn == nil {
|
if wsConn == nil {
|
||||||
_, err = w.Write(buf.Bytes())
|
_, err = w.Write(buf.Bytes())
|
||||||
w.(http.Flusher).Flush()
|
w.(http.Flusher).Flush()
|
||||||
|
|
|
@ -74,7 +74,7 @@ func SetLevel(newLevel LogLevel) {
|
||||||
level = newLevel
|
level = newLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
func print(data *Event) {
|
func print(data Event) {
|
||||||
if data.LogLevel < level {
|
if data.LogLevel < level {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -91,8 +91,8 @@ func print(data *Event) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLog(logLevel LogLevel, format string, v ...any) *Event {
|
func newLog(logLevel LogLevel, format string, v ...any) Event {
|
||||||
return &Event{
|
return Event{
|
||||||
LogLevel: logLevel,
|
LogLevel: logLevel,
|
||||||
Payload: fmt.Sprintf(format, v...),
|
Payload: fmt.Sprintf(format, v...),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue