Skip to content

Instantly share code, notes, and snippets.

@JakubOboza
Created April 13, 2012 21:24
Show Gist options
  • Save JakubOboza/2380272 to your computer and use it in GitHub Desktop.
Save JakubOboza/2380272 to your computer and use it in GitHub Desktop.
import Text.ParserCombinators.Parsec
data RailsLogHeaderLine = RailsLogHeaderLine {
requestMethod :: String,
requestAction :: String,
requestIp :: String,
requestTime :: (String, String)
} deriving (Show, Ord, Eq)
data RailsLogSummaryLine = RailsLogSummaryLine {
requestStatus :: String,
requestTotalTime :: String,
requestViewTime :: String,
requestDbTime :: String
} deriving (Show, Ord, Eq)
data RailsLogParsingResult = Fail String
| Header RailsLogHeaderLine
| Summary RailsLogSummaryLine
plainValue :: Parser String
plainValue = many1 (noneOf " ")
headerLogLine :: Parser RailsLogHeaderLine
headerLogLine = do
plainValue
space
method <- plainValue
space
action <- plainValue
space
plainValue
space
host <- plainValue
space
plainValue
space
date <- plainValue
space
hour <- plainValue
space
hour_offset <- plainValue
return $ RailsLogHeaderLine method action host (date, hour)
--summaryLogLine :: Parser RailsLogSummaryLine
--summaryLogLine = do
-- plainValue
-- space
-- status <- plainValue
-- space
-- plainValue
-- space
-- plainValue
-- space
-- totalTime <- plainValue
-- space
-- return $ RailsLogSummaryLine status totalTime
parseLine inputLine = case parse headerLogLine "(test)" inputLine of
Left err -> Fail $ show err
Right res -> Header res
clearFailedParses [] = []
clearFailedParses (h:hs) =
case h of
Header hdr -> show hdr
Fail err -> clearFailedParses hs
main = do
file <- readFile "production.log"
let logLines = lines file in
let parseResult = map parseLine logLines in
let results = clearFailedParses parseResult in
print results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment