Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger doesn't work in IE 8 #29

Closed
orangecoding opened this issue Oct 9, 2013 · 7 comments
Closed

logger doesn't work in IE 8 #29

orangecoding opened this issue Oct 9, 2013 · 7 comments

Comments

@orangecoding
Copy link

Hey,

we wanted to use your implementation to log error messages in our webapps. Unfortunaltelly IE 8 doesn't log any messages. If I open IE Dev Tools, it adds the console to the main scope.

None the less it seems like your lib thinks there is no console object available.

I tried to open Dev Tools and reload the page, but no luck.

Here's a little snippet:

<html>

<head>
    <script src="loglevel.js"></script>
    <script>
        (function(w){
            log.setLevel(0);
            log.error(w,"too easy");
        }(this));
    </script>
</head>
</html>
@pimterry
Copy link
Owner

pimterry commented Oct 9, 2013

That's because there is in fact no console object available! IE doesn't create a console object until the first time you open the developer console in that tab, and then its should be available forevermore, in that tab.

This is crazy behaviour and I haven't yet found a nice way of dealing with it, although I'd be very happy to take suggestions. The current best solution is that if you want to log things in IE, you open up the dev console, and then run log.setLevel(whatever), and you should get logging from that point onwards. I'm not totally clear, maybe you're already doing that? If you're doing that and it doesn't work, that's definitely a bug.

Otherwise though it's just IE being typically unhelpful, and while I'd be keen to try any ideas people have, I haven't been able to come up with a nicer solution myself; I've tried looking for some way to listen for events, so I can automatically enable logging once the console is open, but that's apparently impossible. Alternatively I could plausibly continuously polling until the console is available with some setTimeout, but that seems a bit of a messy thing for a library to do... You can do that yourself as a reasonable workaround if you do want though.

@orangecoding
Copy link
Author

Wouldn't it be a solution to check whether console is available next time one calls log.debug. If not, do something...

@pimterry
Copy link
Owner

pimterry commented Oct 9, 2013

So normally we can't do this because log.debug isn't actually a function itself, it's just a bound version of console.log, and adding any behaviour would bork stacktraces in all normal logging.

There's probably milage in squeezing into the special case of this here though, since we know that console.log isn't around, and actually adding an explicit function to manage this there... It'll end up with a messy stack the first time, but that's probably fine, and actually I think IE may not let you jump to console.log calls anyway.

I'll take a look at that shortly and see if I can get something up and running with it in the next few days. Good thinking, and thanks for the prod to get me actually looking at this more thoroughly!

@pimterry
Copy link
Owner

Have a solution, and I think this is now working on master; give it a go if you get a minute.

I'm going to do some thorough cross-browser browser testing and put it out as part of 0.5.0 fairly soon as long as I don't find any issues.

@orangecoding
Copy link
Author

Works like a char in IE8/9/10, Chrome 28/29/30, Safari (Mac OS) newest version, FF 22/23/24.

If I understood your code correctly, you now bind a function, that checks whether the console is available each time I call log.debug/error... correct?

Well thank you. One last feature we'll need is an ajax-appender. We need to have the possibility to send log messages to the server. As soon as I get to this, I'll fork your repo, try to find a good solution and push it back as a pullreq.

@pimterry
Copy link
Owner

If I understood your code correctly, you now bind a function, that checks whether the console is available each time I call log.debug/error... correct?

Yes, pretty much (not actually 'bind', which we do actively do with console methods elsewhere, but we do make one and use it, yes)

One last feature we'll need is an ajax-appender. We need to have the possibility to send log messages to the server. As soon as I get to this, I'll fork your repo, try to find a good solution and push it back as a pullreq.

Hmm. I'm quite keen to not pull in too many extra features here, and building in AJAX functionality definitely comes under that umbrella; I'd much prefer to keep this simple. You can currently do this manually by just setting your own console object, which exposes trace/debug/info/warn/error methods, and having those hook into whatever you want to do, and then using loglevel on top. This then requires you to do some fudging to make it cross-platform though, which is exactly what you don't want.

If you wanted to add this I think there might be a plausible approach available though: take a look at #30 (which I've just created for this). Essentially loglevel currently uses internal log method factories which return either a logging method or noop depending on the level. If you just add an API call to hook into this and provide a custom factory method, and tweak the current code to use it, that should allow people to add this for themselves easily if they want it. Does that make sense? Want to have a go?

@pimterry
Copy link
Owner

I've now done a 0.5.0 release, which includes all this. I'm going to leave #30 to get looked at separately, so I'll close this ticket off. Thanks for filing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants