Skip to content

👻 improve logging #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .trunk/configs/custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ webmvc
xvzf
yourpassword
shaofstring
klog
40 changes: 22 additions & 18 deletions kai_analyzer_rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"log/slog"
"net/rpc"
"os"
Expand All @@ -14,6 +15,7 @@ import (

"github.com/go-logr/logr"
"github.com/konveyor/kai-analyzer/pkg/codec"
klog "github.com/konveyor/kai-analyzer/pkg/log"
"github.com/konveyor/kai-analyzer/pkg/service"
"github.com/konveyor/kai-analyzer/pkg/tracing"
)
Expand All @@ -25,13 +27,15 @@ func main() {
lspServerPath := flag.String("lspServerPath", "", "this will be the path to the lsp")
bundles := flag.String("bundles", "", "Comma separated list of path to java analyzer bundles")
depOpenSourceLabelsFile := flag.String("depOpenSourceLabelsFile", "", "Path to the dep open source labels file")

// TODO(djzager): We should do verbosity type argument(s)
logLevel := slog.LevelDebug
inputLogLevel := flag.String("log-level", "DEBUG-1", "Log level")

flag.Parse()
// In the future add cobra for flags maybe
// create log file in working directory for now.

var logLevel slog.Level
err := logLevel.UnmarshalText([]byte(*inputLogLevel))
if err != nil {
panic(fmt.Errorf("invalid log level"))
}

if sourceDirectory == nil || *sourceDirectory == "" {
panic(fmt.Errorf("source directory must be valid"))
Expand All @@ -49,26 +53,27 @@ func main() {
panic(fmt.Errorf("bundles must be set"))
}

// TODO(djzager): Handle log level/location more like reputable LSP servers
// ChatGPT told me gopls and jdtls default to stderr but I couldn't confirm that
// for now I think it's enough to make it configurable to be one or the other.
// The editor extension(s) can tee stderr to a temp file.
var logger *slog.JSONHandler
// fileLogger is for user facing logs that contains
// detailed analysis logs
fileLogger := slog.NewJSONHandler(io.Discard, nil)
if logFile != nil && *logFile != "" {
file, err := os.Create(*logFile)
if err != nil {
panic(err)
}
logger = slog.NewJSONHandler(file, &slog.HandlerOptions{
Level: slog.Level(logLevel),
})
} else {
logger = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.Level(logLevel),
fileLogger = slog.NewJSONHandler(file, &slog.HandlerOptions{
Level: logLevel,
})
}

l := logr.FromSlogHandler(logger)
// stderrLogger is for internal logs discovered by the extension
stderrLogger := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelInfo,
})

l := logr.FromSlogHandler(klog.NewCombinedHandler(stderrLogger, fileLogger))

l.Info("using logLevel", "logLevel", logLevel)

// Check if Java exists on the PATH
if err := exec.Command("java", "-version").Run(); err != nil {
Expand Down Expand Up @@ -130,5 +135,4 @@ func main() {
// When we get here, call stop on the analyzer server
l.Info("stopping server", "signal", sig)
analyzerService.Stop()

}
57 changes: 57 additions & 0 deletions kai_analyzer_rpc/pkg/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package log

import (
"context"
"errors"
"log/slog"
)

type compositeHandler struct {
handlers []slog.Handler
}

func (c *compositeHandler) Enabled(ctx context.Context, lvl slog.Level) bool {
for _, h := range c.handlers {
if h.Enabled(ctx, lvl) {
return true
}
}
return false
}

func (c *compositeHandler) Handle(ctx context.Context, record slog.Record) error {
errs := []error{}
for _, h := range c.handlers {
if h.Enabled(ctx, record.Level) {
if err := h.Handle(ctx, record); err != nil {
errs = append(errs, err)
}
}
}
if len(errs) > 0 {
return errors.Join(errs...)
}
return nil
}

func (c *compositeHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
handlers := []slog.Handler{}
for _, h := range c.handlers {
handlers = append(handlers, h.WithAttrs(attrs))
}
return &compositeHandler{handlers: handlers}
}

func (c *compositeHandler) WithGroup(name string) slog.Handler {
handlers := []slog.Handler{}
for _, h := range c.handlers {
handlers = append(handlers, h.WithGroup(name))
}
return &compositeHandler{handlers: handlers}
}

func NewCombinedHandler(hs ...slog.Handler) slog.Handler {
return &compositeHandler{
handlers: hs,
}
}
13 changes: 12 additions & 1 deletion kai_analyzer_rpc/pkg/service/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ type Analyzer struct {
cacheMutex sync.RWMutex
}

func NewAnalyzer(limitIncidents, limitCodeSnips, contextLines int, location, incidentSelector, lspServerPath, bundles, depOpenSourceLabelsFile, rules string, log logr.Logger) (*Analyzer, error) {
func NewAnalyzer(
limitIncidents,
limitCodeSnips,
contextLines int,
location,
incidentSelector,
lspServerPath,
bundles,
depOpenSourceLabelsFile,
rules string,
log logr.Logger,
) (*Analyzer, error) {
prefix, err := filepath.Abs(location)
if err != nil {
return nil, err
Expand Down
Loading