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