Logging is a very important part of a server application - it allows to analyze workload, collect metrics and facilitates finding critical bugs.

RestRserve ships with basic (yet handy) logging utility - Logger. It can be used in the application code, but what is more important, it is integrated into Application class in order to print useful messages during app execution.

Each applications comes with built-in logger member:

logger is an instance of Logger class. We believe it is important to have logs in a machine readable format, so by default logger writes messages to stdout in JSON format.

Amount of logging can be adjusted with set_log_level method:

If you want to customize logger, you can tweak it with logger$set_printer() function or even swap it to your own (see ?Logger for details):

Moreover Logger mimics loggers from fully-featured lgr package - you can use them interchangeably.

application$logger object however is not designed to be called by end user to produce messages. It is used internally in the application to emit status messages along the request-response processing pipeline.

Consider following example. We develop a method which takes a square root of a number:

Now let’s query it:

Surprise! (or not?)

All request query parameters are strings, but we forgot to convert x to a numeric value. This caused R interpreter to throw an error.

Note however how gracefully RestRserve handled this case:

  • it hasn’t crashed and produced HTTP error with 500 code
  • written error with expanded traceback to the log. This is particularly important as R’s traceback does not contain the full stack trace if you catch errors and warnings!

For example following code will just give you last error (but not a traceback):

Which is not very useful when you try to debug server application.

In contrast let’s call it within RestRserve application:

Now we clearly see that error happens inside fun2