Wednesday, February 17, 2010

Recursive search on the filesystem for the content of a log-file

I had the following problem lately:

On a test server I was looking for a log-file on the installation path and/or one of its sub-folders with some information about a "session timeout".

I didn't know the the log-files name, but new, that it had the extension ".log". I also didn't know which exception was was creating the entry. All I had, was the string "session timeout".

With the following Groovy script I could find all the log-files containing the string "session timeout" and from there it was easy to identify the log-file.


def dir = 'C:/some_installation_folder'
def fileExtension = '.log'
def searchString = 'session timeout'

new File(dir).eachFileRecurse() { f ->
if (f.directory) {
def p = ~/.*\${fileExtension}/
f.eachFileMatch(p) { log ->
def exceptions = []

log.eachLine { ln ->
if (ln =~ '.*' + searchString + '.*') {
exceptions << "${ln}"
}
}

if (!exceptions.empty) {
println log
exceptions.each{println " $it"}
}
}
}
}


Enjoy! :)

1 comment:

computertech111 said...

nice blog, following you.... follow me back please