Last active
December 13, 2016 10:46
-
-
Save soyo42/13ffa80404158b22de0e to your computer and use it in GitHub Desktop.
walks through all java files in given folder and checks if class in log4j logger matches the encapsulating class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this is project name keeper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import os | |
import re | |
if len(sys.argv) != 2: | |
print('usage:: {0} <java project root>'.format(sys.argv[0])) | |
sys.exit(1) | |
classNameRe = re.compile('^(?:@[^)]+\) |@[^(]+ )*(?:public |final |abstract )*\s*(?:class|@?interface|enum)\s+([^ <{]+)[ <]?') | |
loggerAnchorRe = re.compile('LoggerFactory.getLogger\((?:[^\)]+\.)*([^\.]+)\.class(?:\..+)?\)') | |
def filterAndCheck(dirpath, dirnames, names, checker): | |
for sourceFile in names: | |
if sourceFile.endswith('.java'): | |
if sourceFile not in ('package-info.java'): | |
checker(dirpath + '/' + sourceFile) | |
def checkLogger(javaFile): | |
className = None | |
loggerAnchor = None | |
with open(javaFile, 'r') as javaHandle: | |
LIMIT = 150 | |
count = 0 | |
for rawLine in javaHandle: | |
line = rawLine.strip() | |
count += 1 | |
if count >= LIMIT: | |
#break | |
pass | |
if not className: | |
mach = classNameRe.search(line) | |
if mach: | |
className = mach.group(1) | |
continue | |
elif not loggerAnchor: | |
mach = loggerAnchorRe.search(line) | |
if mach: | |
loggerAnchor = mach.group(1) | |
break | |
if not className: | |
sys.stdout.write('\n\033[33;1mWW: class name missed\033[0m -> {0}'.format(javaFile)) | |
elif loggerAnchor: | |
if className == loggerAnchor: | |
sys.stdout.write('\n\033[32;1mOK:\033[0m {0}'.format(javaFile)) | |
else: | |
sys.stdout.write('\n\033[31;1m!!: \033[0mclass \033[37;1m{0}\033[0m -> getLogger(\033[31;1;9m{1}.class\033[0m)'.format(className, loggerAnchor)) | |
else: | |
#print('LOGGER not found [{0}]: {1}'.format(javaFile, className)) | |
sys.stdout.write('.') | |
for (dirpath, dirnames, filenames) in os.walk(sys.argv[1]): | |
filterAndCheck(dirpath, dirnames, filenames, checkLogger) | |
sys.stdout.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment