Skip to content

Print console.log and console.error messages in terminal (--consolelogs) #3169

Open

Description

@imhoffd

This has to be re-implemented for use with the Angular CLI. We'd need to inject a client JS file into the Angular dev server and connect to a websocket server running from the Ionic CLI.

Activity

  1. added
    project type: angular
    on Jun 5, 2018
  2. imhoffd commented on Jul 3, 2018

    @imhoffd
    ContributorAuthor

    @GuilhermeBCC Can you explain why these logs are vital for your tests? What sort of testing are you doing?

  3. GuilhermeBCC commented on Jul 3, 2018

    @GuilhermeBCC

    Well, when I'm running on the device I do not even see when there are any errors, the application simply hangs, there is no information to aid the fix. I am even stopped now because there is an error that I can not trace.

  4. imhoffd commented on Jul 3, 2018

    @imhoffd
    ContributorAuthor

    If the webview is loaded, you should be able to attach dev tools to debug issues and see console logs:

    iOS: https://beta.ionicframework.com/docs/building/ios#using-safari-web-inspector
    Android: https://beta.ionicframework.com/docs/building/android#using-chrome-devtools-to-debug

  5. GuilhermeBCC commented on Jul 3, 2018

    @GuilhermeBCC

    it works and it's great \o/ thanks!

  6. changed the title [-]Print console.log and console.error messages in terminal for Angular[/-] [+]Print console.log and console.error messages in terminal[/+] on Aug 7, 2018
  7. Borongaj commented on Aug 8, 2018

    @Borongaj

    Dev tools are nice, but we have to restart manually everytime we build the app. Sometimes the dev tools window closes immediatly after launch.

    Is it normal that Ionic 3 projects don't show logs anymore too ?

  8. imhoffd commented on Aug 8, 2018

    @imhoffd
    ContributorAuthor

    @Borongaj No, it's not normal that Ionic 3 projects stopped logging. If you want, create a new issue.

  9. obkdev commented on Sep 13, 2018

    @obkdev

    How do I see logs for an app that is installed on the device via Ionic Pro? I'm not able to inspect a 'deployed' app in the same way I'm able to inspect an app I run on the device via 'ionic cordova run android' .. Please can you suggest a solution?

    Thank you!

  10. ludufre commented on Jan 31, 2019

    @ludufre

    Just curious: do you plan to implement this in the near future?

  11. sjregan commented on Feb 12, 2019

    @sjregan

    It would great to have this feature back again. The Safari Debugger is very flakey, it often crashes, doesn't detect device unless you restart. It's much nicer to be able to have a dedicated window on screen that always displays the log output.

  12. changed the title [-]Print console.log and console.error messages in terminal[/-] [+]Print console.log and console.error messages in terminal (--consolelogs)[/+] on Feb 26, 2019
  13. ludufre commented on Mar 4, 2019

    @ludufre

    Is there any forecast?

  14. timbru31 commented on Mar 4, 2019

    @timbru31

    Given that it's nearly 1 year without any progress, I believe you can't expect a forecast/ETA/release date

  15. 7 remaining items

  16. abutizi commented on Mar 19, 2019

    @abutizi

    I'm also suffering of the in-ability to show console logs using the -c command. Anyone here found a way that could help a brother out?

  17. joshstrange commented on Mar 22, 2019

    @joshstrange

    @abutizi I hacked in a VERY rudimentary system using the following code in app.component:

    takeOverConsole() {
    		const ws = new WebSocket('ws://10.0.1.33:12345');
    		ws.onopen = () => {
    			// Web Socket is connected, send data using send()
    			ws.send('Connected!');
    			const comn = window.console;
    			if(!comn) {
    				return;
    			}
    
    			const methods = ['log', 'warn', 'error'];
    			for(let i = 0; i < methods.length; i++) {
    				const original = window.console[methods[i]];
    				window.console[methods[i]] = function() {
    					// @ts-ignore
    					for(const argument of arguments) {
    						try {
    							ws.send(typeof argument === 'string' ? argument : JSON.stringify(argument));
    						} catch(error) {
    							// Do Nothing
    						}
    					}
    					original.apply(window.console, arguments);
    				};
    			}
    		};
    
    		ws.onclose = function() {
    
    			// websocket is closed.
    			alert('Connection is closed...');
    		};
    	}
    

    and this tool running:

    $ websocat -s 10.0.1.33:12345
    

    in my terminal. Make sure to change the IP to your own and pick a port at random. This is FAR from a good solution and I make NO promises about the code. I literally wrote that in 5-10 min max, I just wanted a POC. It does work and I can see it being built upon to provide a turnkey solution for this in the future.

    I just attempted to use Safari again for debugging and it crashed the window almost immediately 🙄. I am going to upgrade to Mojave sometime soon to see if that improves anything but I don't anticipate it doing so.

  18. abutizi commented on Mar 27, 2019

    @abutizi

    @joshstrange
    Thanls a lot for sharing mate. It does work. I'm just wondering, if it took you only 10 min to create this work-around, why does the Ionic Team not look into this issue?

  19. sjregan commented on Mar 27, 2019

    @sjregan

    @joshstrange Thanks, great work.

    Here is my modified version that uses the 'flatten' library to prevent circular JSON issues, sends console arguments concatenated, prepends the console method and outputs any caught errors to console.

        takeOverConsole() {
            const ws = new WebSocket('ws://10.0.1.33:12345')
    
            ws.onopen = () => {
                // Web Socket is connected, send data using send()
                ws.send('Connected!')
                
                const comn = window.console
                
                if (!comn) {
                    return
                }
    
                const methods = ['log', 'warn', 'error']
    
                for (let i = 0; i < methods.length; i++) {
                    const original = window.console[methods[i]]
    
                    window.console[methods[i]] = function () {
                        const output = []
    
                        try {
                            // @ts-ignore
                            for (const argument of arguments) {
                                output.push(typeof argument === 'string' ? argument : stringify(argument))
                            }
    
                            ws.send(methods[i].toUpperCase() + ': ' + output.join(', '))
                        } catch (error) {
                            window.console.error('Websocket Console Error', error)
                        }
    
                        original.apply(window.console, arguments)
                    }
                }
            }
    
            ws.onclose = function () {
                // websocket is closed.
                alert('Websocket Console closed.')
            }
        }
    
  20. abutizi commented on Mar 28, 2019

    @abutizi

    thanks @sjregan

    However I keep getting this message: "Only WebSocket connections are welcome here" on my ionic page when I start listening. Am I doing something wrong?

    Insdie websocket console, it prints this:
    websocat: WebSocketError: I/O failure

  21. sjregan commented on Mar 29, 2019

    @sjregan

    @abutizi no idea sorry, this is the first time I've used websocat. Check your websocket URI and make sure you don't have a proxy messing with your connection.

  22. macekCZ commented on Apr 29, 2019

    @macekCZ

    I develop apps on Windows but testing via DevApp on iPhone ,.. any chance to see debug log?

  23. joshstrange commented on Jun 19, 2019

    @joshstrange

    As of the newest ionic cli it looks like --consolelogs works ("-c" is no longer supported since it is used as shorthand for --configuration it looks like). This is GREAT news but I'm still not seeing errors in the console. So I sometimes get a white screen on device and no output in the terminal. I have to open up safari to see the error. The --consolelogs appears to look like it should be working for "console.error" as well. Any idea why it's not getting printed?

  24. imhoffd commented on Jun 19, 2019

    @imhoffd
    ContributorAuthor

    @joshstrange Thanks for the report! Looks like we're not serializing Error objects very well. I made an issue for that: ionic-team/angular-toolkit#152

  25. joshstrange commented on Jun 19, 2019

    @joshstrange

    While we wait on a better fix I wanted to throw out a quick and dirty fix for people like me:

    Add to node_modules/@ionic/angular-toolkit/assets/consolelogs.js:56 <- The line number might be off, just put it at the top of the for() loop before the msg.data.push(arguments[i]) line.

    if (arguments[i] instanceof Error) {
      arguments[i] = {
        name: arguments[i].name,
        message: arguments[i].message
      }
    }
    

    No promises on if this won't cause other issues but it's working for me (and quite helpful since safari likes to crash as soon as I attach to my app 🙄 ). I am normally the first one to recoil in horror at modifying node_modules but that is normally because it messes with our build server (obviously) but I'm ok with this change since it's just for local development purposes. I hope this helps someone else!

    Also HUGE thank you to @dwieeb as without his help and pointing out where the issue was I wouldn't have gone looking for a quick fix. Also thank you for the quick reply and logging that issue!

  26. selected-pixel-jameson commented on Dec 19, 2019

    @selected-pixel-jameson

    You can only use Safari to debug / log values in the console if it's on the same version as your phone. Since I do not, like most people, want to update to Catalina and my phone is on iOS 13.3 I can't see my device to show the logs.

    This leaves only the option for XCode to debug anything that has to deal with native functionality. This is a huge fail IMHO. I can't believe this has been open for this long.

  27. joshstrangetp commented on Dec 19, 2019

    @joshstrangetp

    @selected-pixel-jameson I too am not planning on upgrading to Catalina and I have not yet tried Safari debugging on my phone (Safari debugging is terrible). You can still use the --consolelogs flag which is what I do to get my logs in the terminal.

  28. selected-pixel-jameson commented on Dec 19, 2019

    @selected-pixel-jameson

    I redact my post then. I thought this was something else as I was no longer seeing logs when running ionic cordova ios -lc it appears as if the flag has changed. Would be nice to have that in the --help... maybe I just missed it then.

  29. joshstrangetp commented on Dec 19, 2019

    @joshstrangetp

    @selected-pixel-jameson Console logs didn't work AFAICT when Ionic 4 launched and they added it after this ticket (or other feedback). Also there is STILL a bug when trying to print errors that does not appear to be a high priority. I have a workaround here: #3169 (comment) -- For myself I use patch-package to fix this in my projects.

  30. ludufre commented on Dec 19, 2019

    @ludufre

    @selected-pixel-jameson ionic cordova run ios -l --consolelogs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementproject type: angular

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions