This page will walk you through editing a Binaris function and reading the execution logs after invocation.

If you are new to Binaris it is recommended that you begin with the previous tutorial:

Note: the instructions below assume you are using MacOS, Ubuntu or Debian.

1. Edit your function

The bn create node8 hello command that you used in the previous getting-started tutorial creates a file called function.js locally.

function.js

exports.handler = async (body, context) => {
  const name = context.request.query.name || body.name || 'World';
  return `Hello ${name}!`;
};

The function receives two arguments. body holds the HTTP request body, and context holds HTTP request and response parameters (such as query strings and headers). Objects returned from a function are automatically JSON-ified and sent as the HTTP response body.

Modify the code

Change the function.js file for the hello function.

- return `Hello ${name}!`;
+ return `Howdy ${name}!`;

After updating the code the function will need to be deployed again.

$ bn deploy hello

Next, invoke the function to test the changes:

$ bn invoke hello
"Howdy World!"
$ bn invoke hello --data '{"name": "Binaris"}'
"Howdy Binaris!"

2. Use Binaris logs

Binaris JavaScript functions support console based logging. This means anything written to stdout (such as console.log) will be accessible via the bn logs command.

As an example, you can log the name argument of your function with the change below.

+ console.log(`${name} has been logged`);

Deploy and invoke the changed code.

$ bn deploy hello
$ bn invoke hello
"Howdy World!"

Now use bn logs to view the log entries.

$ bn logs hello
[2018-10-22T22:03:55.997Z] World has been logged 
[2018-10-22T22:03:55.998Z] Function invocation took 27.437 us

Additionally bn logs supports logging Objects natively.

- console.log(`original name is ${name}`);
+ console.log({ original: name });
$ bn logs hello
[2018-10-22T22:08:07.412Z] { original: 'World' }
[2018-10-22T22:08:07.413Z] Function invocation took 25.628 us

For more information about the Binaris logging capabilities use bn logs --help.

3. Cleanup

$ bn remove hello
Removed function hello

Tip

You can use bn --help to see all the available CLI commands.

What’s next