I need to hide all javascript console logs in production environment.There is a component that i use in every lightning component(i.e Base).What is the most efficient way to hide the logs.I am thinking of some kind of toggle in Base to show / hide logs but i am not sure how it will work in salesforce context.


You can use something like what @Jason is suggesting in your base component, and/or include it in a Lightning component that can be added to any page you can edit using Lightning App Builder.

Instead of hard-coding a switch, I'd recommend using a Hierarchy Custom Setting. Not only will that make it simpler to toggle on and off, it will also let you set different values for users with different profiles, or for specific individual users. You can set values in your production and your dev orgs separately.

You can set up the custom setting like this:

  • Create a Hierarchy Custom Setting named ConsoleLogSwitch__c
  • Add a checkbox field named showLog__c
  • Click the "manage" button on the custom setting detail page
  • Set the Default Organization Level showLog value to False
  • Create new records for any users or profiles that should be able to see console log messages, and set their showLog value to True.

Custom Setting screenshot

As for the component, if you want to be able to add it to a page using Lightning App Builder, implement the flexipage:availableForAllPageTypes interface in your component tag.

<aura:component controller="StackExchange_HideConsoleLogsController" implements="flexipage:availableForAllPageTypes">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

The component's controller will call the server to see whether the current user should have access to the log information. Cache the response by adding the setStorable method to your callout action. (After Winter '19 (v.44) is in production, you can set "cachable" to true in your Apex method's AuraEnabled annotation instead.)

Take a look at Caching Data with Storable Actions to see how client-side caching is handled.

Component controller:

    doInit : function(cmp, event, helper) {

        // See whether console logging should be switched on for the current user
        var action = cmp.get("c.checkConsoleLogDisabler");

        action.setStorable(); // As of the Winter '19 (v.44) release, you can remove 
                              // this line and control caching in your Apex controller.

        action.setCallback(this, function(response){
            if(cmp.isValid() && response !== null && response.getState() == 'SUCCESS'){

                let showDebug = response.getReturnValue();

                if(showDebug) {
                    console.log('I'll show you!');

                } else {
                    if(typeof(console) === 'undefined') {
                        console = {}

                    if(typeof(console.log) != 'undefined'){
                        console.log('I won't show you.');

                        console.log =
                        console.error = 
                        console.info = 
                        console.debug = 
                        console.warn = 
                        console.trace = 
                        console.dir = 
                        console.dirxml = 
                        console.group = 
                        console.groupEnd = 
                        console.time = 
                        console.timeEnd = 
                        console.assert = 
                        console.profile = function() {};  

Apex Controller:

public with sharing class StackExchange_HideConsoleLogsController {

    @AuraEnabled // (cacheable=true)  // Available as of Winter '19 (v.44)
    public static Boolean checkConsoleLogDisabler(){

        // get the ConsoleLogSwitch record that applies to the current user
        ConsoleLogSwitch__c cs = ConsoleLogSwitch__c.getInstance();

        return cs.showLog__c;


I should say that I don't think it's the best idea to rely on something like this. Developers shouldn't be logging sensitive information, and assuming that this sort of log-buster will be in effect on every page gives a false sense of security. The best option is to educate your developers, and do code reviews to be sure nobody is putting stuff out there that they shouldn't be!


Your question looks very similar to a previously answered question on stack overflow. You may want to take a look at the link below.


  • I know i can add console.log = function() {} in every component but i am looking for a way to do this without manually having every single component – user2650277 Oct 3 '18 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.