Read on to learn how to empower Lambda functions with NodeJS console objects. We provide a step-by-step guide about lambda functions. Read Now!

Empower Lambda Functions with NodeJS Console Object | Personal Blog

  • Last Modified: 11 Nov, 2022

In NodeJS, console.log() is the method we use most often. As the number of printed information increases, the information becomes unintuitive. We do not know what it means, so we can’t make sense of it! If you want to use the full power of NodeJS without getting lost in poor debugging techniques, you must learn more about the console object.

The console object is a global that provides functions for logging information to the browser console.

In this blog post, we will be discussing how you can use the NodeJS console object to empower your AWS Lambda functions. We will go over some of the most commonly used methods such as console.log(), console.info(), console.warn(), and console.error(). We will also touch on some of the less commonly used methods such as console.assert() and console.dir().

Basic

console.log()

As the name implies, console.log() is a method that logs a message to the server’s web console. The message may either be a string (with optional substitution values) or maybe a collection of JavaScript files or objects.

Code:

exports.handler = async (event) => {
    console.log('Simple Log, nothing fun', event)

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Output

Our example has printed a simple sentence and an object in the same method, so is there a way to see if it is an object? Yes, we need the object inside the JSON.stringify method.

Code

exports.handler = async (event) => {
    console.log('Simple Log, nothing fun', event)
    console.log(JSON.stringify(event, null, 2))
    
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Output


When the console prints out a lot of information, it is difficult to find exactly what we want. Don’t worry, console can help us because it has a special flags to show the info, warnings and errors.

console.info():

Same as console.log()

console.warn()

The console.warn() method outputs a warning message to the Web console.

console.error()

The console.error() method outputs an error message to the Web console.

Code

exports.handler = async (event) => {
    console.log('Simple Log, nothing fun', event)
    console.info('Info', event)
    console.error('Error', event)
    console.warn('Warning', event)

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Output


Read More: Advantages of golang for AWS Lambda.

Count and CountReset

Would you like to know how many times a particular call has been placed? You can find out by using count and countReset.

console.count

The console.count() method logs the number of times this particular call to count() has been called.

In the case that you supply the label to the count() function, it will output the number of times it has been called with the label that you provided. In the absence of a label, count() behaves as though it were called with the default label.

exports.handler = async (event) => {
    // Default one
    console.count()
    
    // With labels
    console.count(13)
    console.count('Karandeep Singh')
    console.count('Karandeep Singh')
    console.count(13)
    console.count('Karandeep Singh')
    console.count('Karandeep Singh')

    console.count()
    console.count(13)
    console.count('Karandeep Singh')

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

CountReset

The console.countReset() method is used to reset the counter that is used with the console.count().

When the countReset() function is supplied with a label, it will output the number of times it has been called with the label you provide. In the absence of a label, countReset() behaves as if it had been called with a default label rather than the one you supply.

exports.handler = async (event) => {
    // Default one
    console.count()
    
    // With labels
    console.count(13)
    console.count('Karandeep Singh')
    console.count('Karandeep Singh')
    console.count(13)
    
    console.count('Karandeep Singh')
    console.count('Karandeep Singh')

    console.countReset()      // Resetting the default
    console.countReset('13')  // Resetting the one from the labelled
    console.log('After resetting')

    console.count()
    console.count(13)
    console.count('Karandeep Singh')

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};




See Also

comments powered by Disqus