Jan. 15, 2025
A parsing function in R for Apache Access logs in R.
| library(tidyverse) | |
| parse_apache_logs <- function(log_file) { | |
| log_data <- read_lines(log_file) | |
| log_pattern <- '^(\\S+) (\\S+) (\\S+) \\[(.*?)\\] "(\\S+) (\\S+) (\\S+)" (\\d{3}) (\\d+|-) "(.*?)" "(.*?)"' | |
| logs_tibble <- log_data %>% | |
| as_tibble() %>% | |
| rename(raw = value) %>% | |
| mutate( | |
| match = str_match(raw, log_pattern), | |
| ip_address = match[, 2], | |
| identity = match[, 3], | |
| user = match[, 4], | |
| timestamp = match[, 5], | |
| method = match[, 6], | |
| resource = match[, 7], | |
| protocol = match[, 8], | |
| status = as.integer(match[, 9]), | |
| bytes = if_else(match[, 10] == "-", NA_integer_, as.integer(match[, 10])), | |
| referrer = match[, 11], | |
| user_agent = match[, 12] | |
| ) %>% | |
| select(-raw, -match) %>% | |
| mutate( | |
| timestamp = lubridate::dmy_hms(timestamp, tz = "UTC") | |
| ) | |
| return(logs_tibble) | |
| } |