-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Logger's name in stdlib (#11519)
* Fix Logger's name in stdlib Somehow SLF4J is able to recognize correctly the provided Logger's name and print it to the user. Java's Logger is not. In addition, we setup SLF4J's configuration, meaning that log-levels are correctly respected. For a simple project: ``` from Standard.Base import all from Standard.Base.Logging import all type Foo main = IO.println "Hello World!" Foo.log_message level=..Warning "I should warn you about something..." Foo.log_message level=..Info "Should be seen? By default we only show up-to warnings level" Foo.log_message level=..Severe "Something went really bad!" ``` This change demonstrates the fix. Before: ``` > enso --run simple-logging.enso Hello World! Nov 08, 2024 6:08:07 PM com.oracle.truffle.host.HostMethodDesc$SingleMethod$MHBase invokeHandle WARNING: I should warn you about something... Nov 08, 2024 6:08:07 PM com.oracle.truffle.host.HostMethodDesc$SingleMethod$MHBase invokeHandle INFO: Should be seen? By default we only show up-to warnings level Nov 08, 2024 6:08:07 PM com.oracle.truffle.host.HostMethodDesc$SingleMethod$MHBase invokeHandle SEVERE: Something went really bad! Foo ``` After: ``` > enso --run simple-logging.enso Hello World! [WARN] [2024-11-08T18:03:37+01:00] [simple-logging.Foo] I should warn you about something... [ERROR] [2024-11-08T18:03:37+01:00] [simple-logging.Foo] Something went really bad! Foo ``` * Update distribution/lib/Standard/Base/0.0.0-dev/src/Logging.enso Co-authored-by: Radosław Waśko <radoslaw.wasko@enso.org> * Test stdlib logs by using MemoryAppender Added `getEvents` member to `MemoryAppender` so that it is possible to retrieve individual log messages from tests and test their presence. Required opening up to some modules to retrieve internals of loggers. * nit * small tweaks to eliminate module warnings --------- Co-authored-by: Radosław Waśko <radoslaw.wasko@enso.org>
- Loading branch information
Showing
7 changed files
with
110 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/StdLibLogsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.enso.interpreter.test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import java.util.List; | ||
import org.enso.common.MethodNames; | ||
import org.enso.interpreter.runtime.EnsoContext; | ||
import org.enso.logging.service.logback.MemoryAppender; | ||
import org.enso.test.utils.ContextUtils; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Value; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class StdLibLogsTest { | ||
|
||
private static Context ctx; | ||
private static Value mod; | ||
private static EnsoContext ensoContext; | ||
|
||
@BeforeClass | ||
public static void initEnsoContext() { | ||
ctx = ContextUtils.createDefaultContext(); | ||
ensoContext = ContextUtils.leakContext(ctx); | ||
mod = | ||
ctx.eval( | ||
"enso", | ||
""" | ||
from Standard.Base import IO | ||
from Standard.Base.Logging import all | ||
type Foo | ||
test = | ||
Foo.log_message level=..Warning "I should warn you about something..." | ||
Foo.log_message level=..Info "Should be seen? By default we only show up-to warnings level" | ||
Foo.log_message level=..Severe "Something went really bad!" | ||
"""); | ||
} | ||
|
||
@AfterClass | ||
public static void disposeContext() { | ||
ctx.close(); | ||
ctx = null; | ||
ensoContext.shutdown(); | ||
ensoContext = null; | ||
} | ||
|
||
@Test | ||
public void reportLogsInStdLib() { | ||
mod.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "test"); | ||
var context = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
var logger = context.getLogger(Logger.ROOT_LOGGER_NAME); | ||
var appender = (MemoryAppender) logger.getAppender("memory"); | ||
var events = appender.getEvents().stream().map(ILoggingEvent::getMessage).toList(); | ||
|
||
assertTrue( | ||
events.containsAll( | ||
List.of("I should warn you about something...", "Something went really bad!"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters