Skip to content

Instantly share code, notes, and snippets.

@tgsoverly
Created June 26, 2014 20:09
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 tgsoverly/c0fd7df23666cd460098 to your computer and use it in GitHub Desktop.
Save tgsoverly/c0fd7df23666cd460098 to your computer and use it in GitHub Desktop.
A script to find a lines in files that contains a regex, and allow you to surround that line with something else.
import java.nio.file.DirectoryStream
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
class LogWrapper {
static exclusions = ["/deployments/", "grails-app/conf/", "/test/", "/cas/", "/migrations/", "/target/"]
static void listFiles(List files, Path path) throws IOException {
DirectoryStream<Path> stream = Files.newDirectoryStream(path);
for (Path entry : stream) {
if (Files.isDirectory(entry)) {
listFiles(files, entry);
}
files.add(entry);
}
}
static void main(String[] args) {
def logExpression = /[^{][^{]log.(info|debug)/
def console = System.console()
List paths = []
Path startPath = Paths.get("/Users/toverly/Code/min");
listFiles(paths, startPath)
for (path in paths) {
def containsExcluded = exclusions.find { path.toString().contains(it) }
if (path.toString().endsWith(".groovy") && !containsExcluded) {
def file = new File(path.toString())
def lines = file.readLines()
boolean atLeastOneFound = lines.find { (it =~ logExpression).size() > 0 }
def linesReturn = []
linesReturn.addAll(lines)
boolean atLeastOneReplaced = false
if (atLeastOneFound) {
println "----------------------------------------------------------------------------------"
def response = console.readLine(file.absolutePath + ": ")
println "----------------------------------------------------------------------------------"
if (response == "") {
lines.eachWithIndex { line, index ->
def matcher = (line =~ logExpression)
if (matcher.size() > 0) {
response = console.readLine(line + ": ")
if (response == "") {
if (line.contains('log.debug')) {
linesReturn.set(index, "\t\t\tif(log.isDebugEnabled()){ " + line.trim() + " }")
} else {
linesReturn.set(index, "\t\t\tif(log.isInfoEnabled()){ " + line.trim() + " }")
}
atLeastOneReplaced = true
}
}
}
if (atLeastOneReplaced) {
file.text = ""
linesReturn.each { line ->
file.append(line + "\n")
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment