org.asciidoctor.log.Severity Java Examples

The following examples show how to use org.asciidoctor.log.Severity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JULLogHandler.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
private static Level mapSeverity(Severity severity) {
    switch (severity) {
        case DEBUG:
            return Level.FINEST;
        case INFO:
            return Level.INFO;
        case WARN:
            return Level.WARNING;
        case ERROR:
            return Level.SEVERE;
        case FATAL:
            return Level.SEVERE;
        case UNKNOWN:
        default:
            return Level.INFO;
    }
}
 
Example #2
Source File: JavaLogger.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
/**
 * @param threadContext
 * @param args
 */
@JRubyMethod(name = "add", required = 1, optional = 2)
public IRubyObject add(final ThreadContext threadContext, final IRubyObject[] args, Block block) {
  final IRubyObject rubyMessage;
  if (args.length >= 2 && !args[1].isNil()) {
    rubyMessage = args[1];
  } else if (block.isGiven()) {
    rubyMessage = block.yield(threadContext, getRuntime().getNil());
  } else {
    rubyMessage = args[2];
  }
  final Cursor cursor = getSourceLocation(rubyMessage);
  final String message = formatMessage(rubyMessage);
  final Severity severity = mapRubyLogLevel(args[0]);

  final LogRecord record = createLogRecord(threadContext, severity, cursor, message);

  rootLogHandler.log(record);
  return getRuntime().getNil();
}
 
Example #3
Source File: SiteLogHandlerDeserializer.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
private Optional<FailIf> deserializeFailIf(Xpp3Dom node) {
    if (node == null)
        return Optional.empty();

    Xpp3Dom severity = node.getChild("severity");
    FailIf failIf = null;
    if (severity != null) {
        String sanitizedSeverity = sanitizeString(severity);
        if (sanitizedSeverity.length() > 0) {
            Severity severityEnumValue = Severity.valueOf(sanitizedSeverity);
            failIf = new FailIf();
            failIf.setSeverity(severityEnumValue);
        }
    }

    Xpp3Dom containsText = node.getChild("containsText");
    if (containsText != null) {
        String sanitizedContainsText = sanitizeString(containsText);
        if (sanitizedContainsText.length() > 0) {
            failIf = failIf == null ? new FailIf() : failIf;
            failIf.setContainsText(sanitizedContainsText);
        }
    }
    return Optional.ofNullable(failIf);
}
 
Example #4
Source File: JavaLogger.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
private Severity mapRubyLogLevel(IRubyObject arg) {
  final int rubyId = arg.convertToInteger().getIntValue();
  switch (rubyId) {
    case 0:
      return Severity.DEBUG;
    case 1:
      return Severity.INFO;
    case 2:
      return Severity.WARN;
    case 3:
      return Severity.ERROR;
    case 4:
      return Severity.FATAL;
    default:
      return Severity.UNKNOWN;
  }
}
 
Example #5
Source File: LogRecordHelperTest.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void should_format_logRecords_with_empty_lineNumber_absolute_path_when_sourceDir_is_not_valid2() throws IOException {
    // given
    final Cursor cursor = new TestCursor(new File("file.adoc").getAbsolutePath(), 0, "path", "dir");
    final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
    final File sourceDir = Mockito.mock(File.class);
    Mockito.when(sourceDir.getCanonicalPath()).thenThrow(new IOException());

    // when
    String formattedLogRecord = LogRecordHelper.format(logRecord, sourceDir);
    // then
    assertThat(normalizePath(formattedLogRecord)).matches("asciidoctor: INFO: .*/asciidoctor-maven-plugin/file.adoc: a message");
}
 
Example #6
Source File: LogRecordHelperTest.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void should_format_full_logRecord_with_file_absolute_path_when_sourceDir_is_not_valid() throws IOException {
    // given
    final Cursor cursor = new TestCursor(new File("file.adoc").getAbsolutePath(), 3, "path", "dir");
    final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
    final File sourceDir = Mockito.mock(File.class);
    Mockito.when(sourceDir.getCanonicalPath()).thenThrow(new IOException());

    // when
    String formattedLogRecord = LogRecordHelper.format(logRecord, sourceDir);
    // then
    assertThat(normalizePath(formattedLogRecord)).matches("asciidoctor: INFO: .*/asciidoctor-maven-plugin/file.adoc: line 3: a message");
}
 
Example #7
Source File: LogRecordHelperTest.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void should_apply_simple_format_when_cursor_is_empty() {
    // given
    final Cursor cursor = new TestCursor(null, 0, null, null);
    final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
    // when
    String formattedLogRecord = LogRecordHelper.format(logRecord, null);
    // then
    assertThat(normalizePath(formattedLogRecord)).isEqualTo("asciidoctor: INFO: a message");
}
 
Example #8
Source File: LogRecordHelperTest.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void should_apply_simple_format_when_cursor_is_null() {
    // given
    final LogRecord logRecord = new LogRecord(Severity.INFO, null, "a message");
    // when
    String formattedLogRecord = LogRecordHelper.format(logRecord, null);
    // then
    assertThat(normalizePath(formattedLogRecord)).isEqualTo("asciidoctor: INFO: a message");
}
 
Example #9
Source File: LogRecordHelperTest.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void should_apply_full_format_logRecord_with_all_data() {
    // given
    final Cursor cursor = new TestCursor(new File("file.adoc").getAbsolutePath(), 3, "path", "dir");
    final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
    final File sourceDir = getParentFile();
    // when
    String formattedLogRecord = LogRecordHelper.format(logRecord, sourceDir);
    // then
    assertThat(normalizePath(formattedLogRecord)).isEqualTo("asciidoctor: INFO: asciidoctor-maven-plugin/file.adoc: line 3: a message");
}
 
Example #10
Source File: MemoryLogHandler.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Returns LogRecords that are equal or above the severity level and whose message contains text.
 *
 * @param severity Asciidoctor severity level
 * @param text     text to search for in the LogRecords
 * @return list of filtered logRecords
 */
public List<LogRecord> filter(Severity severity, String text) {
    final List<LogRecord> records = new ArrayList<>();
    for (LogRecord record : this.records) {
        if (record.getSeverity().ordinal() >= severity.ordinal() && record.getMessage().contains(text))
            records.add(record);
    }
    return records;
}
 
Example #11
Source File: MemoryLogHandler.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Returns LogRecords that are equal or above the severity level.
 *
 * @param severity Asciidoctor severity level
 * @return list of filtered logRecords
 */
public List<LogRecord> filter(Severity severity) {
    // FIXME: find better name or replace with stream
    final List<LogRecord> records = new ArrayList<>();
    for (LogRecord record : this.records) {
        if (record.getSeverity().ordinal() >= severity.ordinal())
            records.add(record);
    }
    return records;
}
 
Example #12
Source File: WhenAsciidoctorLogsToConsole.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(StructuralNode parent, Reader reader, Map<String, Object> attributes) {
    log(new LogRecord(Severity.INFO, parent.getSourceLocation(), "Hello Log"));
    final List<String> strings = reader.readLines().stream()
            .map(String::toUpperCase)
            .collect(Collectors.toList());

    return createBlock(parent, "paragraph", strings);
}
 
Example #13
Source File: JavaLogger.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
private LogRecord createLogRecord(final ThreadContext threadContext,
                                  final Severity severity,
                                  final Cursor cursor,
                                  final String message) {
  final Optional<BacktraceElement> elem = threadContext.getBacktrace(0)
          .skip(1)
          .findFirst();

  final String sourceFileName = elem.map(BacktraceElement::getFilename).orElse(null);
  final String sourceMethodName = elem.map(BacktraceElement::getMethod).orElse(null);
  final LogRecord record = new LogRecord(severity, cursor, message, sourceFileName, sourceMethodName);
  return record;
}
 
Example #14
Source File: JavaLogger.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
private IRubyObject log(ThreadContext threadContext, IRubyObject[] args, Block block, Severity severity) {
  final IRubyObject rubyMessage;
  if (block.isGiven()) {
    rubyMessage = block.yield(threadContext, getRuntime().getNil());
  } else {
    rubyMessage = args[0];
  }
  final Cursor cursor = getSourceLocation(rubyMessage);
  final String message = formatMessage(rubyMessage);

  final LogRecord record = createLogRecord(threadContext, severity, cursor, message);

  rootLogHandler.log(record);
  return getRuntime().getNil();
}
 
Example #15
Source File: JavaLogger.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@JRubyMethod(name = "debug", required = 0, optional = 2)
public IRubyObject debug(final ThreadContext threadContext, final IRubyObject[] args, Block block) {
  return log(threadContext, args, block, Severity.DEBUG);
}
 
Example #16
Source File: FailIf.java    From asciidoctor-maven-plugin with Apache License 2.0 4 votes vote down vote up
public Severity getSeverity() {
    return severity;
}
 
Example #17
Source File: FailIf.java    From asciidoctor-maven-plugin with Apache License 2.0 4 votes vote down vote up
public void setSeverity(Severity severity) {
    this.severity = severity;
}
 
Example #18
Source File: JavaLogger.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@JRubyMethod(name = "info", required = 0, optional = 2)
public IRubyObject info(final ThreadContext threadContext, final IRubyObject[] args, Block block) {
  return log(threadContext, args, block, Severity.INFO);
}
 
Example #19
Source File: JavaLogger.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@JRubyMethod(name = "warn", required = 0, optional = 2)
public IRubyObject warn(final ThreadContext threadContext, final IRubyObject[] args, Block block) {
  return log(threadContext, args, block, Severity.WARN);
}
 
Example #20
Source File: JavaLogger.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@JRubyMethod(name = "error", required = 0, optional = 2)
public IRubyObject error(final ThreadContext threadContext, final IRubyObject[] args, Block block) {
  return log(threadContext, args, block, Severity.ERROR);
}
 
Example #21
Source File: JavaLogger.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@JRubyMethod(name = "fatal", required = 0, optional = 2)
public IRubyObject fatal(final ThreadContext threadContext, final IRubyObject[] args, Block block) {
  return log(threadContext, args, block, Severity.FATAL);
}
 
Example #22
Source File: AsciidoctorPreprocessTask.java    From aeron with Apache License 2.0 4 votes vote down vote up
@TaskAction
public void preprocess() throws Exception
{
    if (!target.exists() && !target.mkdirs())
    {
        throw new IOException("unable to create build directory");
    }

    final File[] asciidocFiles = AsciidocUtil.filterAsciidocFiles(source);

    System.out.println("Transforming from: " + source);
    System.out.println("Found files: " + Arrays.stream(asciidocFiles).map(File::getName).collect(joining(", ")));

    final Map<File, Integer> errors = new HashMap<>();

    for (final File asciidocFile : asciidocFiles)
    {
        final File outputFile = new File(target, asciidocFile.getName());

        final Asciidoctor asciidoctor = Asciidoctor.Factory.create();

        final int[] errorCount = { 0 };

        asciidoctor.registerLogHandler(
            (logRecord) ->
            {
                if (logRecord.getSeverity() == Severity.ERROR || logRecord.getSeverity() == Severity.FATAL)
                {
                    errorCount[0]++;
                }
            });

        final HashMap<String, Object> attributes = new HashMap<>();
        attributes.put("sampleBaseDir", requireNonNull(sampleBaseDir, "Must specify sampleBaseDir"));
        attributes.put("sampleSourceDir", requireNonNull(sampleSourceDir, "Must specify sampleSourceDir"));

        final HashMap<String, Object> options = new HashMap<>();
        options.put("attributes", attributes);
        options.put("safe", org.asciidoctor.SafeMode.UNSAFE.getLevel());

        try (PrintStream output = new PrintStream(outputFile))
        {
            asciidoctor.javaExtensionRegistry().preprocessor(
                new org.asciidoctor.extension.Preprocessor()
                {
                    public void process(final Document document, final PreprocessorReader reader)
                    {
                        String line;
                        while (null != (line = reader.readLine()))
                        {
                            if (line.startsWith(":aeronVersion:"))
                            {
                                output.println(":aeronVersion: " + versionText);
                            }
                            else
                            {
                                output.println(line);
                            }
                        }
                    }
                });

            asciidoctor.loadFile(asciidocFile, options);

            if (0 < errorCount[0])
            {
                errors.put(asciidocFile, errorCount[0]);
            }
        }
    }

    errors.forEach((key, value) -> System.out.println("file: " + key + ", error count: " + value));

    if (0 < errors.size())
    {
        throw new Exception("failed due to errors in parsing");
    }
}