Skip to content

Commit a03e9aa

Browse files
Artur-claude
andauthored
feat: enable client-side logging in production via localStorage flag (#17935) (#22103)
Allow dynamic control of browser console logging in production mode by checking localStorage for "vaadin.browserLog" flag. Developers can now enable client-side logging in production using: localStorage.setItem('vaadin.browserLog', 'true') This helps with debugging production issues without requiring JAR modifications or rebuilds. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent c630de2 commit a03e9aa

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

flow-client/src/main/java/com/vaadin/client/Console.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @since 1.0
3030
*/
3131
public final class Console {
32-
private static boolean shouldLogToBrowserConsole;
32+
private static boolean isProductionMode = false;
3333

3434
@FunctionalInterface
3535
// Runnable that can throw
@@ -48,9 +48,39 @@ private Console() {
4848
* if an application is in the production mode or not
4949
*/
5050
public static void setProductionMode(boolean isProductionMode) {
51-
shouldLogToBrowserConsole = !isProductionMode;
51+
Console.isProductionMode = isProductionMode;
5252
}
5353

54+
/**
55+
* Checks if logging to browser console should be enabled. Returns true if
56+
* either: - Not in production mode, or - The localStorage flag
57+
* "vaadin.browserLog" is set to "true"
58+
*
59+
* @return true if browser console logging should be enabled
60+
*/
61+
private static boolean shouldLogToBrowserConsole() {
62+
if (!isProductionMode) {
63+
return true;
64+
}
65+
// Check localStorage for override flag in production mode
66+
return GWT.isScript() && isLocalStorageFlagEnabled();
67+
}
68+
69+
/**
70+
* Checks if the localStorage flag "vaadin.browserLog" is set to "true".
71+
*
72+
* @return true if the flag is set to "true", false otherwise
73+
*/
74+
private static native boolean isLocalStorageFlagEnabled()
75+
/*-{
76+
try {
77+
return $wnd.localStorage && $wnd.localStorage.getItem('vaadin.browserLog') === 'true';
78+
} catch (e) {
79+
// localStorage might not be available or accessible
80+
return false;
81+
}
82+
}-*/;
83+
5484
/**
5585
* If not in production mode, logs the given message to the browser console
5686
* using the debug log level.
@@ -63,7 +93,7 @@ public static void setProductionMode(boolean isProductionMode) {
6393
*/
6494
public static void debug(Object message) {
6595
if (GWT.isScript()) {
66-
if (shouldLogToBrowserConsole) {
96+
if (shouldLogToBrowserConsole()) {
6797
Browser.getWindow().getConsole().debug(message);
6898
}
6999
} else {
@@ -83,7 +113,7 @@ public static void debug(Object message) {
83113
*/
84114
public static void log(Object message) {
85115
if (GWT.isScript()) {
86-
if (shouldLogToBrowserConsole) {
116+
if (shouldLogToBrowserConsole()) {
87117
Browser.getWindow().getConsole().log(message);
88118
}
89119
} else {
@@ -103,7 +133,7 @@ public static void log(Object message) {
103133
*/
104134
public static void warn(Object message) {
105135
if (GWT.isScript()) {
106-
if (shouldLogToBrowserConsole) {
136+
if (shouldLogToBrowserConsole()) {
107137
Browser.getWindow().getConsole().warn(message);
108138
}
109139
} else {
@@ -123,7 +153,7 @@ public static void warn(Object message) {
123153
*/
124154
public static void error(Object message) {
125155
if (GWT.isScript()) {
126-
if (shouldLogToBrowserConsole) {
156+
if (shouldLogToBrowserConsole()) {
127157
Browser.getWindow().getConsole().error(message);
128158
}
129159
} else {

0 commit comments

Comments
 (0)