Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Created October 2, 2014 15:08
Show Gist options
  • Save randyzwitch/53ed0de5910fe42ebed9 to your computer and use it in GitHub Desktop.
Save randyzwitch/53ed0de5910fe42ebed9 to your computer and use it in GitHub Desktop.
JSON logging using jsonlite
#Example
{
"timestamp": 1324830675.076,
"status": "404",
"short_message": "File does not exist: /var/www/no-such-file",
"host": "ord1.product.api0",
"facility": "httpd",
"errno": "ENOENT",
"remote_host": "50.57.61.4",
"remote_port": "40100",
"path": "/var/www/no-such-file",
"uri": "/no-such-file",
"level": 4,
"headers": {
<strong>"user-agent": "BadAgent/1.0",</strong>
"connection": "close",
"accept": "*/*"
},
"method": "GET",
"unique_id": ".rh-g2Tm.h-ord1.product.api0.r-axAIO3bO.c-9210.ts-1324830675.v-24e946e"
}
#Approx output using jsonlite
library(jsonlite)
errors <- list()
errors$timestamp <- 1324830675.076
errors$status <- "404"
errors$short_message <- "File does not exist: /var/www/no-such-file"
errors$host <- "ord1.product.api0"
errors$facility <- "httpd"
errors$errno <- "ENOENT"
errors$remote_host <- "50.57.61.4"
errors$remote_port <- "40100"
errors$path <- "/var/www/no-such-file"
errors$uri <- "/no-such-file"
errors$level <- "4"
errors <- lapply(errors, unbox) #Remove all the brackets around singletons
errors$headers <- list(user_agent="BadAgent/1.0", connection = "close", accept = "*/*")
prettify(toJSON(errors))
> prettify(toJSON(errors))
{
"timestamp": 1324830675.076,
"status": "404",
"short_message": "File does not exist: /var/www/no-such-file",
"host": "ord1.product.api0",
"facility": "httpd",
"errno": "ENOENT",
"remote_host": "50.57.61.4",
"remote_port": "40100",
"path": "/var/www/no-such-file",
"uri": "/no-such-file",
"level": "4",
"headers": {
"user_agent": [
"BadAgent/1.0"
],
"connection": [
"close"
],
"accept": [
"*/*"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment