# NOT RUN {
# Template string with two fields
template <- '{{ip ipAddress}} - [{{date accessDate}}] {{int status }}'
# Two simple log records
logs <- c(
'192.168.1.10 - [26/Jul/2019:11:41:10 -0500] 200',
'192.168.1.11 - [26/Jul/2019:11:41:21 -0500] 404'
)
# A formatter for the date field
myFormatters <- list(date = function(x) lubridate::as_datetime(x, format = '%d/%b/%Y:%H:%M:%S %z'))
# A parser class for the date field
date_parser <- parser(
'[0-3][0-9]\\/[A-Z][a-z]{2}\\/[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2}[ ][\\+|\\-][0-9]{4}',
myFormatters$date,
'date'
)
# Parse the logs from raw data
parse_logs(logs, template, list(date=date_parser))
# Write the logs and to file and parse
logfile <- tempfile()
templatefile <- tempfile()
writeLines(logs, logfile)
yaml::write_yaml(list(template=template, classes=list(date=date_parser)), templatefile)
parse_logs_file(logfile, templatefile, myFormatters)
file.remove(logfile)
file.remove(templatefile)
# }
Run the code above in your browser using DataLab