Using Ember.Logger

Ember.Logger is a robust type of logging in Ember. It goes beyond the capabilities of imports.console. In this recipe, we'll take a look at some examples on how to work with it in your application.

How to do it...

In this project, we'll create a simple program that demonstrates how to use some of the Ember.logging capabilities:

  1. In a new program, add a new index route:
    $ ember g route index
    

    This will create a new index route.

  2. Edit the index.js file in the routes folder. Add some new logging:
    // app/routes/index.js
    import Ember from 'ember';
    const {Logger}= Ember;
    export default Ember.Route.extend({
        model(){
          Logger.log('log');
          Logger.info('info', 'more stuff');
          Logger.error('error');
          Logger.debug('debug');
          Logger.warn('warn');
          Logger.assert(true === false);
          return {};
        }
    });

    Ember.logging gives us five different logging options. All these different types of log methods accept one or more arguments. Each argument will be joined together and separated by a space when written to the browser console window:

    Logger.log('log');
  3. This is the basic form of logging in Ember. It simply logs the values to the browser console:
    Logger.info('info', 'more stuff');

    The info logger logs a message to the console as an info message. In Firefox and Chrome, a small I icon is displayed next to the item:

    Logger.error('error');

    The error log prints to the console with an error icon, red text, and stack trace.

    Logger.debug('debug');

    The debug log prints to the console in blue text.

    Logger.warn('warn');

    The warning log will print to the console with a warning icon.

    Logger.assert(true === false);

    The assert statement will return an error and stack trace if the value returns false.

  4. Fire up the Ember server and open Console. This is a screenshot of how it looks in Chrome:
    How to do it...

How it works...

The Ember.Logger is a more powerful console logger. It's a robust logging tool to make debugging easier. Ember.Logger is built-in in the Ember CLI package.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset