-1

I have read through different posts for turning off the console.log to print in the browser's console, but couldn't find any solution for angular 2. Here is what I'm trying to achieve: a functionality in angular 2 where I can turn off the browser console.log (maybe in the back-end). I'm thinking of a function or maybe a default browser property that can be toggled to turn off the printing of messages in the browser console when I call console.log.

Please suggest me a right direction to achieve this.. thanks!

Aiguo
  • 3,416
  • 7
  • 27
  • 52
  • Maybe overwrite the console.log function and set a boolean var to turn on and off the overwritten function. http://stackoverflow.com/questions/7042611/override-console-log-for-production – Philip Aug 03 '16 at 15:44
  • that can be a solution, but its more of a hacky way. I'm trying to come up with a solution that actually disables the console in the back-end, if that's possible.. – Aiguo Aug 03 '16 at 15:47
  • 1
    This seems like a strange thing to be spending time on. Compilation phases such as babel, webpack, gulp, grunt can all strip these out during any minifcation task. I really don't see why disabling logs has any benefit whatsoever during the development phase. Perhpas by understanding your reasons might help for a better community response. – zilj Aug 03 '16 at 15:50
  • The solutions in the dupe should be enough. Is there security-critical info in the console? – gcampbell Aug 03 '16 at 15:50
  • @zilj its fine to be using it in the development phase, but I have to eventually remove them once my app is ready! My dream scenario is something like a toggle which can turn on/off the console.log, so that I can turn it on/off while testing the whole app – Aiguo Aug 03 '16 at 15:56
  • @Saransh Ahlawat Exactly my point, you can use all the logs you like in development. Then if for example you are using gulp you can rely on a gulp minification plugin to strip all these out for production. This is a very typical of dev to prod environments. – zilj Aug 03 '16 at 15:59
  • @gcampbell, yes thats what my concern is. I have bunch of security-critical things that I don't want to print when my app is ready to launch. Its fine during development/testing phase but after that it won't serve any purpose. Also, its better than removing all console.log(s) manually because I might be needing them to test and maintain the app – Aiguo Aug 03 '16 at 15:59
  • Depending on what your definition of "security critical" is, but removing client-side console.logs won't prevent people from reading the same data by other means if they want to. – JJJ Aug 03 '16 at 16:01
  • @SaranshAhlawat You can't rely on something like `console.log = function () {}` to hide sensitive info. As zilj says, it's better to remove them in your build process. Also remember that people can still open up all your scripts on the client side and see what's in them and make changes. – gcampbell Aug 03 '16 at 16:01
  • 1
    Sorry but this is a standardised process and should really be enforced. Strip any development based code out during a production build process. No hacks. No overriding native global variables or helper methods. Use a task runner/compiler. This is what they exist for! – zilj Aug 03 '16 at 16:04

1 Answers1

2

possible duplicate of this

var logger = function()
{
    var oldConsoleLog = null;
    var pub = {};

    pub.enableLogger =  function enableLogger() 
                        {
                            if(oldConsoleLog == null)
                                return;

                            window['console']['log'] = oldConsoleLog;
                        };

    pub.disableLogger = function disableLogger()
                        {
                            oldConsoleLog = console.log;
                            window['console']['log'] = function() {};
                        };

    return pub;
}();

$(document).ready(
    function()
    {
        console.log('hello');

        logger.disableLogger();
        console.log('hi', 'hiya');
        console.log('this wont show up in console');

        logger.enableLogger();
        console.log('This will show up!');
    }
 );
Community
  • 1
  • 1
rashfmnb
  • 9,959
  • 4
  • 33
  • 44
  • 3
    don't plagiarize answers or give just link only answers to questions, is an exact copy of the code posted here: http://stackoverflow.com/a/1215400/1322693 – konkked Aug 03 '16 at 15:45