Skip to content

Commit 72e9a7b

Browse files
hjlarrygemini-code-assist[bot]Yeuoly
authored
fix: plugin log not display after refactor (#537)
* fix: plugin log not display after refactor * Update internal/core/local_runtime/notifier_logger.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * feat: add logging support for local runtime instance events --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Yeuoly <admin@srmxy.cn>
1 parent 11e00ea commit 72e9a7b

15 files changed

Lines changed: 95 additions & 81 deletions

File tree

internal/core/control_panel/launcher_local.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ func (c *ControlPanel) LaunchLocalPlugin(
132132
notifier.OnLocalRuntimeStop(runtime)
133133
})
134134
},
135+
OnInstanceLogImpl: func(pi *local_runtime.PluginInstance, ple plugin_entities.PluginLogEvent) {
136+
c.WalkNotifiers(func(notifier ControlPanelNotifier) {
137+
notifier.OnLocalRuntimeInstanceLog(runtime, pi, ple)
138+
})
139+
},
135140
}
136141
runtime.AddNotifier(lifetime)
137142

internal/core/control_panel/logger.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package controlpanel
22

33
import (
4+
"strings"
5+
46
"github.com/langgenius/dify-plugin-daemon/internal/core/debugging_runtime"
57
"github.com/langgenius/dify-plugin-daemon/internal/core/local_runtime"
68
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
@@ -32,6 +34,33 @@ func (l *StandardLogger) OnLocalRuntimeStopped(runtime *local_runtime.LocalPlugi
3234
log.Info("local runtime stopped: %s", identity)
3335
}
3436

37+
var (
38+
loggers = map[string]func(string, ...interface{}){
39+
"debug": log.Debug,
40+
"warn": log.Warn,
41+
"warning": log.Warn,
42+
"error": log.Error,
43+
"fatal": log.Error,
44+
"critical": log.Error,
45+
}
46+
)
47+
48+
func (l *StandardLogger) OnLocalRuntimeInstanceLog(
49+
runtime *local_runtime.LocalPluginRuntime,
50+
instance *local_runtime.PluginInstance,
51+
event plugin_entities.PluginLogEvent,
52+
) {
53+
// notify terminal
54+
instanceID := instance.ID()[:8]
55+
loggerFunc, ok := loggers[strings.ToLower(event.Level)]
56+
if !ok {
57+
loggerFunc = log.Info
58+
}
59+
identity, _ := runtime.Identity()
60+
61+
loggerFunc("plugin %s: instance %s log: %s", identity, instanceID, event.Message)
62+
}
63+
3564
func (l *StandardLogger) OnDebuggingRuntimeConnected(runtime *debugging_runtime.RemotePluginRuntime) {
3665
identity, _ := runtime.Identity()
3766
log.Info("debugging runtime connected: %s", identity)

internal/core/control_panel/signals.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ type ControlPanelNotifier interface {
2121
OnLocalRuntimeScaleUp(runtime *local_runtime.LocalPluginRuntime, instanceNums int32)
2222
// on local runtime scale down
2323
OnLocalRuntimeScaleDown(runtime *local_runtime.LocalPluginRuntime, instanceNums int32)
24+
// on local runtime instance log
25+
OnLocalRuntimeInstanceLog(
26+
runtime *local_runtime.LocalPluginRuntime,
27+
instance *local_runtime.PluginInstance,
28+
event plugin_entities.PluginLogEvent,
29+
)
2430

2531
// on remote runtime connected
2632
OnDebuggingRuntimeConnected(runtime *debugging_runtime.RemotePluginRuntime)

internal/core/debugging_runtime/lifetime.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ func (r *RemotePluginRuntime) SpawnCore() error {
4747
func(err string) {
4848
log.Error("plugin %s: %s", r.Configuration().Identity(), err)
4949
},
50-
func(message string) {
51-
log.Info("plugin %s: %s", r.Configuration().Identity(), message)
52-
},
50+
func(plugin_entities.PluginLogEvent) {}, // remote debugging logs stay on client side
5351
)
5452
})
5553

internal/core/io_tunnel/backwards_invocation/transaction/aws_event_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (h *ServerlessTransactionHandler) Handle(
116116
func(err string) {
117117
log.Warn("invoke dify failed, received errors: %s", err)
118118
},
119-
func(message string) {}, //log
119+
func(plugin_entities.PluginLogEvent) {}, //log
120120
)
121121

122122
select {

internal/core/local_runtime/instance.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ func (s *PluginInstance) handleStdout(data []byte, once *sync.Once) {
201201
notifier.OnInstanceErrorLog(s, errors.New(err))
202202
})
203203
},
204-
func(message string) {
204+
func(logEvent plugin_entities.PluginLogEvent) {
205205
// plain text log
206206
s.WalkNotifiers(func(notifier PluginInstanceNotifier) {
207-
notifier.OnInstanceLog(s, message)
207+
notifier.OnInstanceLog(s, logEvent)
208208
})
209209
},
210210
)
@@ -330,3 +330,7 @@ func (s *PluginInstance) GracefulStop(maxWaitTime time.Duration) {
330330
// all listeners are closed, stop the instance
331331
s.Stop()
332332
}
333+
334+
func (s *PluginInstance) ID() string {
335+
return s.instanceId
336+
}

internal/core/local_runtime/notifier_lifecycle.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package local_runtime
22

33
import (
44
"time"
5+
6+
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
57
)
68

79
type NotifierHeartbeat struct {
@@ -41,7 +43,7 @@ func (n *NotifierHeartbeat) OnInstanceHeartbeat(instance *PluginInstance) {
4143
instance.lastActiveAt = time.Now()
4244
}
4345

44-
func (n *NotifierHeartbeat) OnInstanceLog(instance *PluginInstance, message string) {
46+
func (n *NotifierHeartbeat) OnInstanceLog(instance *PluginInstance, event plugin_entities.PluginLogEvent) {
4547

4648
}
4749

internal/core/local_runtime/notifier_logger.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

internal/core/local_runtime/signals_instance.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package local_runtime
22

3+
import "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
4+
35
type InstanceSignal string
46

57
const (
@@ -47,7 +49,7 @@ type PluginInstanceNotifier interface {
4749
OnInstanceHeartbeat(*PluginInstance)
4850

4951
// on instance log
50-
OnInstanceLog(*PluginInstance, string)
52+
OnInstanceLog(*PluginInstance, plugin_entities.PluginLogEvent)
5153

5254
// on instance error
5355
OnInstanceErrorLog(*PluginInstance, error)

internal/core/local_runtime/signals_runtime.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package local_runtime
22

3+
import "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
4+
35
type PluginRuntimeNotifier interface {
46
// on instance starting
57
OnInstanceStarting()
@@ -13,6 +15,9 @@ type PluginRuntimeNotifier interface {
1315
// on instance shutdown
1416
OnInstanceShutdown(*PluginInstance)
1517

18+
// on instance log
19+
OnInstanceLog(*PluginInstance, plugin_entities.PluginLogEvent)
20+
1621
// on instance scale up
1722
OnInstanceScaleUp(int32)
1823

0 commit comments

Comments
 (0)