Skip to content

Instantly share code, notes, and snippets.

@hellojinjie
Created December 5, 2013 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellojinjie/7802037 to your computer and use it in GitHub Desktop.
Save hellojinjie/7802037 to your computer and use it in GitHub Desktop.
ErrorLogAnalyze.scala
import scala.io._
import java.io._
import java.util.regex._
object ErrorLogAnalyze {
def main(args: Array[String]) = {
val files = if (args.length == 0) new File(".") else new File(args(0))
val lines = getLogs(files.listFiles.filter(_.getName.contains("error")).toList)
val errorAndHeartbeat = lines.partition(line => line.contains("ERROR") && line.contains("HEARTBEAT"))._1
val errorAndNotHeartbeat = lines.partition(line => line.contains("ERROR") && ! line.contains("HEARTBEAT"))._1
val warn = lines.partition(_ contains("WARN"))._1
println("File analyzed: " + files.listFiles.filter(_.getName.contains("error")).toList)
print("Total " + lines.size + " to analyze, " + errorAndHeartbeat.size + " error and HEARTBEAT messages, ")
println(errorAndNotHeartbeat.size + " error and not HEARTBEAT messages, ")
println("We also have " + warn.size + " warning messages which are not included in this report.\n")
println("HEARTBEAT " + errorAndHeartbeat.size + " lines log to analyze")
analyzeErrorLog(errorAndHeartbeat)
println("not HEARTBEAT " + errorAndNotHeartbeat.size + " lines log to analyze")
analyzeErrorLog(errorAndNotHeartbeat)
}
def analyzeErrorLog(lines: List[String]) = {
val causes = lines.groupBy(line => line.substring(line.lastIndexOf("|") + 1))
causes foreach (cause => {
val (key, messages) = cause
println(key + " (total " + messages.size + ")")
println("appType\tproductID\tstreamURL\tcount")
messages.groupBy(line => Tuple3(getParameter(line, "appType"),
getParameter(line, "productID"), getParameter(line, "streamURL"))) foreach (line => {
val (parameter, messages) = line
println(parameter.productIterator.toList.mkString("\t") + "\t" + messages.size)
})
println()
})
}
def getLogs(files: List[File]): List[String] = {
if (files.isEmpty) return Nil
return Source.fromFile(files.head).getLines.toList ::: getLogs(files.tail)
}
def getParameter(line: String, parameter: String): String = {
val p = Pattern.compile(parameter + "=([^&]*)(&|$)")
val m = p.matcher(line)
return if (m.find()) m.group(1) else ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment