2

I've written a very simple function to ensure ensure my applications do not break if I forget to remove a console.log().

Simply put:

myNamespace.log = function( msg ){
   if( window.console ){
      console.log(obj);
   }
}

It works like a charm. Alas, I very much liked the ability to see from which line of code and file my console messages came from. Is there a (chrome/FF friendly) way to get the file/line of code a function call?

1

1 Answer 1

2

If it were my site, I'd use:

myNamespace.log = function( msg ){
  if( window.console && window.console.log && typeof window.console.log === "function" ){
    console.log(obj);
  }
}

I seriously doubt there's a browser-agnostic way to get source file line number.

Sign up to request clarification or add additional context in comments.

2 Comments

well I just realized it's for my personal use so chrome/FF would suffice for this particular functionality. What do the additional conditions in the if statement do?
Check to make sure that "window.console" actually has a "log" property, and that the property is really a function you can call. Probably overly paranoid, but if you want to really make sure that stray "console.log" calls definitely won't cause an exception that'd be more insurance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.