Adding logging to a shiny app with loggit

February 27, 2018
By

(This article was first published on Clean Code, and kindly contributed to R-bloggers)

This is a very short post with example code
Over time when you move your shiny app from your computer to a server, you
want to add some logging. Generally logging is defined in levels : INFO (everything you want to print),
WARNING (it does not stop the application, but it could be a problem), and
ERROR (fatal things).

Shiny server does already log all it’s actions to
a file on the server, but that file can be hard to access.
It would be nice if every app has its own logging, close to the app location.

The package loggit by Ryan J. Price,
overwrites the normal message, warning and stop functions
in R and replaces them with identically functioning functions, but the package
ALSO writes to a file. Thus the loggit packages writes to a json file of your
choosing and has nice timestamped data, which is extremely easy to parse.

Some prerequisites:

  • you need to have the loggit package installed on your shiny-server
  • you need to create the logfile first (empty)

example code

This is a non functioning app.r example.

library(shiny)
library(loggit)
setLogFile("loggit.json")
loggit("INFO", "app has started", app = "start")
ui <- fluidpage(
  #your ui here
  )

server <- function(ui, server, session){
  message("this is an message")
  # it would be nice if you had some server logic here
  session$onSessionEnded(function(){loggit("INFO", "app has stopped", app = "stop")})
}
shinyApp(ui, server)

This example has the ui and server defined and a function shinyApp() that
starts the server.

There are 3 things I’d like to point your attention to:

  • you first set your logfile location (you need to create it first)
  • I’ve used both loggit() and the general function message() They both write to loglevel INFO
  • I also used the session$onSessionEnded thingy, this is activated when you
    close the app

Are all these things necessary? No absolutely not, you generally don’t want to
know if an app has started, and stopped. You might only want to write the
warnings and errors to the log.

I think the package is extremely user friendly and easy to implement, would you use this in production?

Did I miss anything? Did I make a mistake? open an issue on github,
or send me a message on twitter.

Adding logging to a shiny app with loggit was originally published by at Clean Code on February 28, 2018.

To leave a comment for the author, please follow the link and comment on their blog: Clean Code.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)