Mastering Loglevel for Effective JavaScript Logging

Introduction to loglevel

Loglevel is a lightweight and easy-to-use JavaScript logging library that provides a simple but powerful API for logging messages at various levels of severity. This makes it an essential tool for developers who want to keep track of their application’s behavior through meaningful log messages.

Why Use Loglevel?

Loglevel allows you to categorize log messages by severity, such as trace, debug, info, warn, error, and silent. This helps in filtering out unnecessary logs and focusing only on messages relevant to the severity level you are interested in. Here’s a quick overview of the available API methods:

Basic API Methods

  
  // Importing the library
  const log = require('loglevel');

  // Setting the default log level
  log.setLevel('warn');

  // Logging messages at different levels
  log.trace('This is a trace message');
  log.debug('This is a debug message');
  log.info('This is an info message');
  log.warn('This is a warning message');
  log.error('This is an error message');
  

Setting Log Levels

Loglevel provides the setLevel method for controlling the levels of logs that you want to see. Here’s how you can customize it:

  
  // Set the log level to 'info'
  log.setLevel('info');

  // Only 'info', 'warn', and 'error' messages will be logged
  log.debug('This debug message will not be logged');
  log.info('This info message will be logged');
  log.warn('This warning will be logged');
  log.error('This error will be logged');
  

Get Current Log Level

To retrieve the current log level, use the getLevel method:

  
  const currentLogLevel = log.getLevel();
  console.log('Current log level:', currentLogLevel);
  

Persisting Log Level

Loglevel supports persisting the log level across page reloads using localStorage:

  
  // Enable or disable persist functionality
  log.enableAll();
  log.disableAll();
  

Complete Application Example

Below is a complete application example demonstrating the usage of loglevel:

  
  const log = require('loglevel');

  // Log configuration
  log.setLevel('info');

  function initApp() {
    log.info('Initializing app...');
    
    // Simulate a trace scenario
    log.trace('This trace message will not be shown at info level');
    
    // Function for handling user login
    function login(username, password) {
      log.debug('Login attempt for username:', username);
      
      if (username === 'admin') {
        log.info('Admin logged in');
      } else {
        log.warn('Non-admin login detected');
      }
      
      // Simulate an error scenario
      if (password === '12345') {
        log.error('Weak password detected');
      } else {
        log.info('User logged in successfully');
      }
    }
    
    // Simulate user interaction
    login('admin', '12345');
    login('user', 'password');
  }

  initApp();
  

By using loglevel, you can improve the maintainability and debuggability of your JavaScript applications by categorizing and controlling your log messages effectively.

Leave a Reply

Your email address will not be published. Required fields are marked *