Coder Social home page Coder Social logo

javascript-logging's Introduction

JavaScript Logging

Overview

In this lesson we'll be looking at the many ways to log with Javascript

Objectives

  1. Explore the console object
  2. Use different logging methods

Logging

Journals give us a window into the past, helping us discern what happened when and what the outcome was. We keep journals, traditionally, by writing things down with a timestamp (which could just be a date).

In programming, logging is like journaling. It records a history of a running application that we can revisit, giving us insight into what was happening at a given point in time.

Logging lets us revisit our application as if it was running. It's a useful tool for tracking bugs, performance, and generally ensuring that our applications are chugging along.

In this lesson, we're going to look at ways to log with JavaScript. Let's dive in!

dive in

console

Open the console in your browser of choice.

Top Tip: If you already have the console open but you find that it's getting a little cluttered, you can clear it out by clicking the "Clear console" button — it looks like this in Chrome:

clear console

Alternatively, you can press cmd + k" or ctrl + l to make your console look fresh and new.

Now enter console in the console (that's a little funky to say, huh?) and press "enter". In Chrome, you'll see something like

Object {}

This is all probably a bit mystifying at the moment. You can explore that console thing (psst: it's an object, which we'll learn more about later), but feel free to move on to learn about how we use it.

log()

The venerable console.log() is an all-purpose logging method. (A method or a function is a bit of code that does something. We call them when we want them to act.) In programming, logging refers to the process of printing information about the program as it runs. Try it out!

console.log("I'm logging! I'm a regular lumberjack!")

You can pass any number of messages to console.log() by separating them with commas; when printed, they'll be separated by a space:

console.log('one', 'two', 'three')

When you enter the above in your console, you'll see "one two three".

And you don't just have to pass strings to console.log() — try this:

console.log("I must have logged", 1000, "times today.")

You should see "I must have logged 1000 times today."

Note that when you use strings, the comma must come after the end quotation mark — this is because it's not punctuation like in English writing, but a way of telling JavaScript, "Hey, I'm going to give you something else!"

error()

console.error() prints an error and usually includes a stack trace. A "stack trace" is a report of code that was executed at a certain time (in this case, starting from when the error occurred and working backwards). Enter the following in your console:

console.error('Danger, Will Robinson!')

You should see something like

console.error

If you click on the arrow, you can see the stack trace. When you're debugging, you can click on the linked line numbers in the stack trace to jump to the relevant line in the source code. That won't be particularly useful here, since it will just take you to some of Chrome's internal JavaScript — but it can be incredibly useful when you're writing your own code!

You can pass several messages at once, separated by a comma:

console.error("Danger!", "Something bad happened!", "Time to debug!")

When printed, there will be a space after each message: "Danger! Something bad happened! Time to debug!".

You might ask why we'd ever need to use this — isn't the goal of writing good code to avoid errors? Well, sure, but sometimes errors are out of our control: the network could go down, data could change, or a user could enter something invalid. In these cases, it's helpful to report not only what happened (which logging generally is good for) but also what kind of thing happened — in these cases, it was an error, so we use console.error().

warn()

console.warn() does as its name suggests: it prints a warning. We can use console.warn() to, well, warn developers that an action they've taken might not be wise — one common use-case is giving developers a heads-up that some of the code they've written might no longer be supported.

console.warn('Hm, you might not want to do that.')

It might be a while before you find yourself needing to use console.warn() — but you should think of it every time you see those yellow messages in the browser's console!

console.warn()

As with console.error(), we use console.warn() to indicate in our log history that something undesirable happened, but it shouldn't have broken anything (unlike an error, which could break things). Warnings, as mentioned above, could give us insight into things that might break in the future or actions that worked but maybe shouldn't have — just like in real life!

Wrap-up

console has tons (or tonnes for some folks) of useful functionality — be sure to explore! Logging info, errors, warnings, and stack traces is essential to staying on top of your application's functionality.

Resources

View JavaScript Logging on Learn.co and start learning to code for free.

javascript-logging's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

javascript-logging's Issues

Haven't actually used console.log

The lab mentions having used console.log but up to this point in curriculum, we haven't.

"We've already made use of perhaps the most popular of these methods, console.log(); but there are a few more tricks up console's sleeve. Let's dive in!"

Reopen Console Reminder?

Not sure if it's just me, but I was working in one console through multiple lessons before realizing I should reopen a fresh one. A reminder maybe to close out of the console and open before the new lesson might be nice!

Issues with the error() section

  1. Using object creation and variable assignment and they've never seen that before

  2. Show them what a stack trace looks like.

  3. You do string substitution, but they've never seen that before. Is this the best place to introduce it? Also shouldnt' there be a lot more information around that?

intro to js: repetitive section

WHY LOG? Think of logging like journaling. Journaling gives us a way to record things that happened over the course of a day; logging gives our programs a way to record what happened while they were running.

This section seems unnecessary since it's covered in the first section of this lesson under Javascript Logging.

Additional questions

Really like that this is pared down more --

A couple of comments. When you first talk about 'console' you write "What methods does console expose? " Students do not know what methods yet mean.

I get a good grasp of console.log because it's just printing out stuff to the screen.

I understand less well console.warn and console.error -- I think my biggest questions are why/when will they happen. I know the mechanics of typing them to the console but I'm not sure how this actually works. Is there a little bit of context that you can provide? Or maybe an example. For example, having a scenario where this would appear would be helpful "console.error() prints an error and usually includes a stack trace. "Stack traces" is a report of code that was executed at a certain time (in this case, starting from when the error occurred and working backwards). Enter the following in your console:"

Otherwise, this is pretty good.

@jmburges

learn submit

I completed everything and passed the tests. How do I submit pull to move to the next lesson?

Issues with log section

  1. "takes any number of arguments". Students don't know what arguments are.

  2. String Substitution is actually pretty hard for students to understand, you need to go much deeper into this and make sure they are coding along.

Questions

I think the primary issue here for me is that this lesson focuses on methods but we haven't yet defined for them. And in the individual methods that are covered, things like variables and objects and errors and libraries and stack trace and arguments and object properties are mentioned but the student will have no idea what they are.

I think this lesson is good, but might be misplaced in the track.

Issues with the warn section

  1. What's a library?

  2. What's a function. Also function vs. method?

  3. What's deprecation? Most students won't know that word

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.