Skip to content
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

#115: Add custom JavaScript console logger that forwards log levels to the Go logger #144

Merged
merged 2 commits into from
Sep 13, 2023
Merged
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
9 changes: 8 additions & 1 deletion doc/changes/changes_0.5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ Code name:

## Summary

This release updates the upload process for the extension registry to verify that the extension URLs are valid. It also adds design, requirements and user guide for the integration testing framework.
This release contains the following notable changes:

* It updates the upload process for the extension registry to verify that the extension URLs are valid.
* It verifies that no instances exist before uninstalling an extension.
* It adds design, requirements and user guide for the integration testing framework.
* It adds a custom JavaScript console logger that forwards the log levels of `console.log()`, `console.warn()` and `console.error()` to the Go `logrus` functions `Print()`, `Warn()` and `Error()`. Before, all log messages were output using `Print()`.
* It speeds up tests that use a mock extension by avoiding compiling TypeScript to JavaScript.

The release deprecates function `extensionController.CreateWithConfig()` in favor of `extensionController.CreateWithValidatedConfig()`. This new function validates the given configuration and returns an error in case it finds an issue.

Expand All @@ -13,6 +19,7 @@ The release deprecates function `extensionController.CreateWithConfig()` in favo
* #129: Added verification for extension URLs before uploading to registry
* #142: Added validation of configuration when creating a new controller
* #130: Added verification that no instance exists before uninstalling an extension
* #115: Added custom JavaScript console logger that forwards log levels to the Go logger

## Documentation

Expand Down
2 changes: 1 addition & 1 deletion pkg/extensionAPI/extensionApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func newJavaScriptVm(logPrefix string) *goja.Runtime {
}

func configureLogging(registry *require.Registry, vm *goja.Runtime, logPrefix string) {
var printer console.Printer = console.PrinterFunc(func(s string) { log.Print(logPrefix + s) })
printer := createJavaScriptLogger(logPrefix)
registry.RegisterNativeModule(console.ModuleName, console.RequireWithPrinter(printer))
console.Enable(vm)
}
Expand Down
41 changes: 16 additions & 25 deletions pkg/extensionAPI/extensionApi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,22 @@ func (suite *ExtensionApiSuite) TestInstallResolveBucketFsPathFails() {
suite.EqualError(err, `failed to install extension "ext-id": failed to find absolute path for file "my-adapter-extVersion.jar": mock error`)
}

func (suite *ExtensionApiSuite) TestJavaScriptConsoleLog() {
extensionContent := integrationTesting.CreateTestExtensionBuilder(suite.T()).
WithInstallFunc("console.log('test log message')").
Build().AsString()
extension := suite.loadExtension(extensionContent)
err := extension.Install(suite.mockContext(), "extensionVersion")
suite.NoError(err)
}

func (suite *ExtensionApiSuite) TestJavaScriptConsoleWarn() {
extensionContent := integrationTesting.CreateTestExtensionBuilder(suite.T()).
WithInstallFunc("console.warn('test warning message')").
Build().AsString()
extension := suite.loadExtension(extensionContent)
err := extension.Install(suite.mockContext(), "extensionVersion")
suite.NoError(err)
}

func (suite *ExtensionApiSuite) TestJavaScriptConsoleError() {
extensionContent := integrationTesting.CreateTestExtensionBuilder(suite.T()).
WithInstallFunc("console.error('test error message')").
Build().AsString()
extension := suite.loadExtension(extensionContent)
err := extension.Install(suite.mockContext(), "extensionVersion")
suite.NoError(err)
func (suite *ExtensionApiSuite) TestJavaScriptConsoleLogging() {
var tests = []struct{ jsLoggingCode string }{
{jsLoggingCode: "console.log('test log message')"},
{jsLoggingCode: "console.warn('test warning message')"},
{jsLoggingCode: "console.error('test error message')"},
}
for _, test := range tests {
suite.Run(test.jsLoggingCode, func() {
extensionContent := integrationTesting.CreateTestExtensionBuilder(suite.T()).
WithInstallFunc(test.jsLoggingCode).
Build().AsString()
extension := suite.loadExtension(extensionContent)
err := extension.Install(suite.mockContext(), "extensionVersion")
suite.NoError(err)
})
}
}

func (suite *ExtensionApiSuite) TestUninstall() {
Expand Down
28 changes: 28 additions & 0 deletions pkg/extensionAPI/jsLogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package extensionAPI

import (
"github.com/dop251/goja_nodejs/console"
log "github.com/sirupsen/logrus"
)

// createJavaScriptLogger creates a new [console.Printer] for handling `console.log()`, `console.warn()` and `console.error()` calls in JavaScript.
// This implementation forwards all log messages to logrus using the appropriate log method `Print()`, `Warn()` and `Error()`.
func createJavaScriptLogger(logPrefix string) console.Printer {
return &jsLogger{logPrefix: logPrefix}
}

type jsLogger struct {
logPrefix string
}

func (l jsLogger) Log(s string) {
log.Print(l.logPrefix + s)
}

func (l jsLogger) Warn(s string) {
log.Warn(l.logPrefix + s)
}

func (l jsLogger) Error(s string) {
log.Error(l.logPrefix + s)
}
Loading