2022-10-06 14:23:59 +02:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
2023-02-15 02:52:11 +01:00
|
|
|
"context"
|
|
|
|
"time"
|
2022-10-06 14:23:59 +02:00
|
|
|
|
|
|
|
"github.com/dop251/goja"
|
|
|
|
"github.com/dop251/goja_nodejs/console"
|
2023-02-15 02:52:11 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/logstore"
|
2023-09-15 16:58:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/logstore/record"
|
2023-02-15 02:52:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-09-15 16:58:45 +02:00
|
|
|
logstoreService *logstore.Service[*record.ExecutionLog]
|
2023-02-15 02:52:11 +01:00
|
|
|
_ console.Printer = (*logger)(nil)
|
2022-10-06 14:23:59 +02:00
|
|
|
)
|
|
|
|
|
2023-09-15 16:58:45 +02:00
|
|
|
func SetLogstoreService(svc *logstore.Service[*record.ExecutionLog]) {
|
2023-02-15 02:52:11 +01:00
|
|
|
logstoreService = svc
|
|
|
|
}
|
|
|
|
|
|
|
|
type logger struct {
|
|
|
|
ctx context.Context
|
|
|
|
started time.Time
|
|
|
|
instanceID string
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLogger returns a *logger instance that should only be used for a single action run.
|
|
|
|
// The first log call sets the started field for subsequent log calls
|
|
|
|
func newLogger(ctx context.Context, instanceID string) *logger {
|
|
|
|
return &logger{
|
|
|
|
ctx: ctx,
|
|
|
|
started: time.Time{},
|
|
|
|
instanceID: instanceID,
|
|
|
|
}
|
|
|
|
}
|
2022-10-06 14:23:59 +02:00
|
|
|
|
2023-02-15 02:52:11 +01:00
|
|
|
func (l *logger) Log(msg string) {
|
|
|
|
l.log(msg, logrus.InfoLevel, false)
|
|
|
|
}
|
2022-10-06 14:23:59 +02:00
|
|
|
|
2023-02-15 02:52:11 +01:00
|
|
|
func (l *logger) Warn(msg string) {
|
|
|
|
l.log(msg, logrus.WarnLevel, false)
|
2022-10-06 14:23:59 +02:00
|
|
|
}
|
2023-02-15 02:52:11 +01:00
|
|
|
|
|
|
|
func (l *logger) Error(msg string) {
|
|
|
|
l.log(msg, logrus.ErrorLevel, false)
|
2022-10-06 14:23:59 +02:00
|
|
|
}
|
2023-02-15 02:52:11 +01:00
|
|
|
|
|
|
|
func (l *logger) log(msg string, level logrus.Level, last bool) {
|
|
|
|
ts := time.Now()
|
|
|
|
if l.started.IsZero() {
|
|
|
|
l.started = ts
|
|
|
|
}
|
2023-09-15 16:58:45 +02:00
|
|
|
r := &record.ExecutionLog{
|
2023-02-15 02:52:11 +01:00
|
|
|
LogDate: ts,
|
|
|
|
InstanceID: l.instanceID,
|
|
|
|
Message: msg,
|
|
|
|
LogLevel: level,
|
|
|
|
}
|
|
|
|
if last {
|
2023-09-15 16:58:45 +02:00
|
|
|
r.Took = ts.Sub(l.started)
|
2023-02-15 02:52:11 +01:00
|
|
|
}
|
2023-09-15 16:58:45 +02:00
|
|
|
logstoreService.Handle(l.ctx, r)
|
2022-10-06 14:23:59 +02:00
|
|
|
}
|
|
|
|
|
2023-02-15 02:52:11 +01:00
|
|
|
func withLogger(ctx context.Context) Option {
|
|
|
|
instance := authz.GetInstance(ctx)
|
|
|
|
instanceID := instance.InstanceID()
|
2022-10-06 14:23:59 +02:00
|
|
|
return func(c *runConfig) {
|
2023-02-15 02:52:11 +01:00
|
|
|
c.logger = newLogger(ctx, instanceID)
|
|
|
|
c.instanceID = instanceID
|
2022-10-06 14:23:59 +02:00
|
|
|
c.modules["zitadel/log"] = func(runtime *goja.Runtime, module *goja.Object) {
|
2023-02-15 02:52:11 +01:00
|
|
|
console.RequireWithPrinter(c.logger)(runtime, module)
|
2022-10-06 14:23:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|