Disabling system messages in LWC component
Hello, I want to use NebulaLogger for errors in public site, but I don't want initialization and error messages in browser's console.
On c/logger/loggerService.js file I can see functions to disable messages:
let areSystemMessagesEnabled = true;
export function enableSystemMessages() {
areSystemMessagesEnabled = true;
}
export function disableSystemMessages() {
areSystemMessagesEnabled = false;
}
But i'm not sure how to use these as I can only import c/logger and not c/logger/loggerService.js. Is it possible to provide it as a setting when initializing logger in LWC?
Hi @dominykasvitkus - if you want to prevent the logs from showing up in the browser's console, there's a settings field you can use to control that (LoggerSettings__c.IsJavaScriptConsoleLoggingEnabled__c). For production orgs, I recommend that this is disabled org-wide, but you can also configure at the profile or user levels.
Hey, I've already disabled this setting, but looking at the code it doesn't apply to initial message which I refer.
c/logger/loggerService.js:58-66
constructor() {
this._loadSettingsFromServer();
if (areSystemMessagesEnabled && !LoggerService.hasInitialized) {
this._logToConsole('INFO', 'logger component initialized\n' + JSON.stringify(new BrowserContext(), null, 2));
LoggerService.hasInitialized = true;
}
}
areSystemMessagesEnabled doesn't read settings from what I see (loggerService.js:29-37):
let areSystemMessagesEnabled = true;
export function enableSystemMessages() {
areSystemMessagesEnabled = true;
}
export function disableSystemMessages() {
areSystemMessagesEnabled = false;
}
@dominykasvitkus thanks for the clarification! I see what you're saying about the initial message not checking the settings first. I'm not planning to expose the functions enableSystemMessages() and disableSystemMessages() (I think those were added primarily to help with testing in Jest tests), but I'll see if I can change the behavior so the settings are checked first before printing the initial message.
@dominykasvitkus I've just published release v4.16.4 to address this. It will now check LoggerSettings__c.IsJavaScriptConsoleLoggingEnabled__c first, before deciding if it should print the initialization and error messages in the browser's console. You won't have to add anything to your code for this (e.g., you won't need to call disableSystemMessages(), etc.), just upgrading your version of Nebula Logger should fix this in your orgs.
Whenever you have a chance to upgrade & try it, let me know if you come across any more issues with this - or have any other suggestions/feedback!
Hey, @jongpie. Seems to work as expected now, thank you!
@dominykasvitkus that's great to hear, thanks for the update!