Logging Function

Wanted to share a logging function I created to help debug UDFs. It’s still pretty fresh so open to feedback.

Since there isn’t a log per se, it uses a collection, log, to store the messages. As a result, you likely won’t want to use this on a db that is heavily utilized or for which you pay but might be helpful for debugging.

It behaves like an identity function in that it returns the value it was provided after logging.

// adds a document to log with {value: 12} and returns 12. Not very useful :)
Call('log', {value: 12})

// adds a document to log with {message: 'help} and returns a Ref to that log
Call('log', {message: 'help})

// adds a document to log with message and value and returns 12.
Call('log', {message: 'current value', value: 12})

Let(
  {
    // creates a local var with a ref to the log created (since no value was provided)
    log: Call('log', {message: '> my function'}),
  },
  Paginate(
     // logs the Match() and links it to the log created above for traceability
    Call('log', {log: Var("log"}, message: "match", value: Match(Index('my_index'))})
  )
)

Here’s the full function body:

Query(
  Lambda(
    "args",
    If(
      Or(
        false, /* set to true to disable */
        And(
          IsNull(Select("value", Var("args"), null)),
          IsNull(Select("message", Var("args"), null))
        )
      ),
      Select("value", Var("args"), null),
      Let(
        {
          log: Create(Collection("log"), {
            data: {
              parent: Select("log", Var("args"), null),
              message: Select("message", Var("args"), null),
              value: Select("value", Var("args"), null)
            }
          })
        },
        Select("value", Var("args"), Select("ref", Var("log"), null))
      )
    )
  )
)

@ryanjduffy, out of interest, is it a feature request? At first, this seems like something that is quite easy to throw in a UDF with the advantage that it’s super customizable. Or is there a reason why you would like to see a more opinionated version integrated in Fauna?

Yes and no. :slight_smile: I created the topic here since it wasn’t really a request for help but it does seem to me like there’s value in “console.log but for UDFs” as a first class API.

1 Like

There should be a built-in fql function for us to log something and there should be a logs section on the dashboard for the functions.

1 Like

We recently announced Fauna Logs. Please check out the details here

1 Like

I know this topic is marked solved, but the Fauna Logs feature is a very different usecase from what was requested here. The OP was requesting a way to see intermediate runtime-FQL state/values, similar to a console.log statement in the browser. Fauna’s logging solution is about which queries are being run, and what their performance characteristics are.

2 Likes

Hi @wallslide, @aprilmintacpineda, and @ryanjduffy. We have been working on a log() function which will allow you to output messages and values from within your query back to the requesting client, inside the query response. This feature is part of a larger set of improvements we are making to our query system, which will be available in beta soon. I will update this ticket when that feature is beta’ed.

The UDF method is very clever!

2 Likes

The log function is now available through the FQL X API, which is currently in beta! :tada:

For example, the function in the original post can now be rewritten like the following:

let fn = args => 
  if(false || (args.value == null && args.message == null)) {
    args.value
  } else {
    let info = {
      parent: args.log,
      message: args.message,
      value: args.value
    }
    log(info)

    args.value ?? info
  }

Which you could use like this

fn({
  log: "hello",
  message: "foo",
  value: 42
})

// result
{
  data: '42',
  summary: 'info at *query*:10: { parent: "hello", message: "foo", value: 42 }',
  txn_ts: 1682625629243145,
  stats: {
    compute_ops: 1,
    read_ops: 0,
    write_ops: 0,
    query_time_ms: 10,
    contention_retries: 0,
    storage_bytes_read: 203,
    storage_bytes_write: 0,
  },
}
1 Like

@wallslide @aprilmintacpineda and @ryanjduffy I am really proud to announce FQL X is now in beta. This new language features both a Log() and dbg() (debug) functions which do what you’ve asked for. Please take a look at this blog for more details on the new language. I am going to close this ticket as addressed. Thanks!

2 Likes