Issue #67: Added support of logging severity for all audit events
This commit is contained in:
parent
4687b0b801
commit
a29f01a95b
|
|
@ -11,7 +11,7 @@ class BriefLogger extends DefaultLogger
|
|||
{
|
||||
BriefLogger(OutputStream out) throws UnsupportedEncodingException
|
||||
{
|
||||
super(out, true);
|
||||
super(out, true, out, false, false);
|
||||
}
|
||||
@Override
|
||||
public void auditStarted(AuditEvent evt) {}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,9 @@ public class DefaultLogger
|
|||
/** Close error stream after use. */
|
||||
private final boolean closeError;
|
||||
|
||||
/** Print severity level. */
|
||||
private boolean printSeverity = true;
|
||||
|
||||
/**
|
||||
* Creates a new {@code DefaultLogger} instance.
|
||||
* @param os where to log infos and errors
|
||||
|
|
@ -71,6 +74,24 @@ public class DefaultLogger
|
|||
this(os, closeStreamsAfterUse, os, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>DefaultLogger</code> instance.
|
||||
* @param infoStream the {@code OutputStream} for info messages.
|
||||
* @param closeInfoAfterUse auditFinished should close infoStream.
|
||||
* @param errorStream the {@code OutputStream} for error messages.
|
||||
* @param closeErrorAfterUse auditFinished should close errorStream
|
||||
* @param printSeverity if severity level should be printed.
|
||||
* @exception UnsupportedEncodingException if there is a problem to use UTF-8 encoding.
|
||||
*/
|
||||
public DefaultLogger(OutputStream infoStream,
|
||||
boolean closeInfoAfterUse,
|
||||
OutputStream errorStream,
|
||||
boolean closeErrorAfterUse,
|
||||
boolean printSeverity) throws UnsupportedEncodingException {
|
||||
this(infoStream, closeInfoAfterUse, errorStream, closeErrorAfterUse);
|
||||
this.printSeverity = printSeverity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code DefaultLogger} instance.
|
||||
*
|
||||
|
|
@ -120,10 +141,11 @@ public class DefaultLogger
|
|||
if (evt.getColumn() > 0) {
|
||||
sb.append(':').append(evt.getColumn());
|
||||
}
|
||||
if (severityLevel == SeverityLevel.WARNING) {
|
||||
sb.append(": warning");
|
||||
final String errorMessageSeparator = ": ";
|
||||
if (printSeverity) {
|
||||
sb.append(errorMessageSeparator).append(severityLevel.getName());
|
||||
}
|
||||
sb.append(": ").append(message);
|
||||
sb.append(errorMessageSeparator).append(message);
|
||||
errorWriter.println(sb);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -338,7 +338,7 @@ public final class Main {
|
|||
|
||||
}
|
||||
else if (PLAIN_FORMAT_NAME.equals(format)) {
|
||||
listener = new DefaultLogger(out, closeOutputStream);
|
||||
listener = new DefaultLogger(out, closeOutputStream, out, false, true);
|
||||
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -462,7 +462,7 @@ public class CheckstyleAntTask extends Task {
|
|||
if (formatters.isEmpty()) {
|
||||
final OutputStream debug = new LogOutputStream(this, Project.MSG_DEBUG);
|
||||
final OutputStream err = new LogOutputStream(this, Project.MSG_ERR);
|
||||
listeners[0] = new DefaultLogger(debug, true, err, true);
|
||||
listeners[0] = new DefaultLogger(debug, true, err, true, true);
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < formatterCount; i++) {
|
||||
|
|
@ -591,7 +591,8 @@ public class CheckstyleAntTask extends Task {
|
|||
new LogOutputStream(task, Project.MSG_DEBUG),
|
||||
true, new LogOutputStream(task, Project.MSG_ERR), true);
|
||||
}
|
||||
return new DefaultLogger(new FileOutputStream(toFile), true);
|
||||
final FileOutputStream infoStream = new FileOutputStream(toFile);
|
||||
return new DefaultLogger(infoStream, true, infoStream, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -28,7 +28,11 @@ public abstract class BaseCheckTestSupport {
|
|||
protected static class BriefLogger
|
||||
extends DefaultLogger {
|
||||
public BriefLogger(OutputStream out) throws UnsupportedEncodingException {
|
||||
super(out, true);
|
||||
super(out, true, out, false, false);
|
||||
}
|
||||
|
||||
public BriefLogger(OutputStream out, boolean printSeverity) throws UnsupportedEncodingException {
|
||||
super(out, true, out, false, printSeverity);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -58,6 +62,22 @@ public abstract class BaseCheckTestSupport {
|
|||
return checker;
|
||||
}
|
||||
|
||||
protected Checker createChecker(Configuration checkConfig, boolean printSeverity)
|
||||
throws Exception {
|
||||
|
||||
final DefaultConfiguration dc = createCheckerConfig(checkConfig);
|
||||
final Checker checker = new Checker();
|
||||
// make sure the tests always run with english error messages
|
||||
// so the tests don't fail in supported locales like german
|
||||
final Locale locale = Locale.ENGLISH;
|
||||
checker.setLocaleCountry(locale.getCountry());
|
||||
checker.setLocaleLanguage(locale.getLanguage());
|
||||
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
checker.configure(dc);
|
||||
checker.addListener(new BriefLogger(stream, printSeverity));
|
||||
return checker;
|
||||
}
|
||||
|
||||
protected DefaultConfiguration createCheckerConfig(Configuration config) {
|
||||
final DefaultConfiguration dc =
|
||||
new DefaultConfiguration("configuration");
|
||||
|
|
@ -84,6 +104,11 @@ public abstract class BaseCheckTestSupport {
|
|||
verify(createChecker(aConfig), fileName, fileName, expected);
|
||||
}
|
||||
|
||||
protected void verify(Configuration aConfig, boolean printSeveriy,
|
||||
String filename, String... expected) throws Exception {
|
||||
verify(createChecker(aConfig, printSeveriy), filename, filename, expected);
|
||||
}
|
||||
|
||||
protected void verify(Checker checker, String fileName, String... expected)
|
||||
throws Exception {
|
||||
verify(checker, fileName, fileName, expected);
|
||||
|
|
|
|||
|
|
@ -37,4 +37,12 @@ public class DefaultLoggerTest {
|
|||
dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
|
||||
dl.auditFinished(new AuditEvent(6000, "myfile"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCtorWithTwoParameters() throws UnsupportedEncodingException {
|
||||
OutputStream infoStream = new ByteArrayOutputStream();
|
||||
DefaultLogger dl = new DefaultLogger(infoStream, true);
|
||||
dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
|
||||
dl.auditFinished(new AuditEvent(6000, "myfile"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -253,9 +253,9 @@ public class MainTest {
|
|||
+ "/src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java"
|
||||
.replace("/", File.separator);
|
||||
assertEquals(String.format("Starting audit...%n"
|
||||
+ "%1$s:3:14: "
|
||||
+ "%1$s:3:14: error: "
|
||||
+ "Name 'InputMain' must match pattern '^[a-z0-9]*$'.%n"
|
||||
+ "%1$s:5:7: "
|
||||
+ "%1$s:5:7: error: "
|
||||
+ "Name 'InputMainInner' must match pattern '^[a-z0-9]*$'.%n"
|
||||
+ "Audit done.%n"
|
||||
+ "Checkstyle ends with 2 errors.%n", expectedPath), systemOut.getLog());
|
||||
|
|
|
|||
|
|
@ -80,14 +80,27 @@ public class WriteTagCheckTest extends BaseCheckTestSupport {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testTagSeverity() throws Exception {
|
||||
public void testTagPrintSeverityTrue() throws Exception {
|
||||
checkConfig.addAttribute("tag", "@incomplete");
|
||||
checkConfig.addAttribute("tagFormat", "\\S");
|
||||
checkConfig.addAttribute("tagSeverity", "warning");
|
||||
final String[] expected = {
|
||||
"11: " + getCheckMessage(WRITE_TAG, "warning: @incomplete", "This class needs more code..."),
|
||||
};
|
||||
verify(checkConfig, getPath("InputWriteTag.java"), expected);
|
||||
final boolean printSeverity = true;
|
||||
verify(checkConfig, printSeverity, getPath("InputWriteTag.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTagPrintSeverityFalse() throws Exception {
|
||||
checkConfig.addAttribute("tag", "@incomplete");
|
||||
checkConfig.addAttribute("tagFormat", "\\S");
|
||||
checkConfig.addAttribute("tagSeverity", "warning");
|
||||
final String[] expected = {
|
||||
"11: " + getCheckMessage(WRITE_TAG, "@incomplete", "This class needs more code..."),
|
||||
};
|
||||
final boolean printSeverity = false;
|
||||
verify(checkConfig, printSeverity, getPath("InputWriteTag.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue