Skip to content

Instantly share code, notes, and snippets.

@steve-kasica
Created July 26, 2026 19:32
Show Gist options
  • Select an option

  • Save steve-kasica/6596dd054fb3b658eccd7c3d18fc3926 to your computer and use it in GitHub Desktop.

Select an option

Save steve-kasica/6596dd054fb3b658eccd7c3d18fc3926 to your computer and use it in GitHub Desktop.
Apache Access log parser

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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment