-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1071 from robertpanzer/extension-logging-docs
Add documentation for logging from extensions
- Loading branch information
Showing
13 changed files
with
208 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 file contains 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
28 changes: 28 additions & 0 deletions
28
.../src/test/java/org/asciidoctor/integrationguide/extension/LoggingBlockMacroProcessor.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.asciidoctor.integrationguide.extension; | ||
|
||
import org.asciidoctor.ast.StructuralNode; | ||
import org.asciidoctor.extension.BlockMacroProcessor; | ||
import org.asciidoctor.extension.Name; | ||
|
||
import java.util.Map; | ||
|
||
//tag::include[] | ||
@Name("log") | ||
public class LoggingBlockMacroProcessor extends BlockMacroProcessor { | ||
|
||
@Override | ||
public Object process( | ||
StructuralNode parent, String target, Map<String, Object> attributes) { | ||
|
||
String message = target; | ||
|
||
log(new org.asciidoctor.log.LogRecord( // <1> | ||
org.asciidoctor.log.Severity.INFO, | ||
parent.getSourceLocation(), // <2> | ||
message)); | ||
|
||
return createBlock(parent, "paragraph", "Hello from the logging macro"); | ||
} | ||
|
||
} | ||
//end::include[] |
54 changes: 54 additions & 0 deletions
54
.../test/java/org/asciidoctor/integrationguide/extension/LoggingBlockMacroProcessorTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.asciidoctor.integrationguide.extension; | ||
|
||
import org.asciidoctor.Asciidoctor; | ||
import org.asciidoctor.Options; | ||
import org.asciidoctor.log.LogRecord; | ||
import org.asciidoctor.util.ClasspathResources; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@RunWith(Arquillian.class) | ||
public class LoggingBlockMacroProcessorTest { | ||
|
||
@ArquillianResource | ||
private Asciidoctor asciidoctor; | ||
|
||
@ArquillianResource | ||
private ClasspathResources classpathResources; | ||
|
||
@Test | ||
public void should_log_from_extension() { | ||
|
||
//tag::include[] | ||
File loggingmacro_adoc = // ... | ||
//end::include[] | ||
classpathResources.getResource("logging-macro.adoc"); | ||
//tag::include[] | ||
List<LogRecord> logRecords = new ArrayList<>(); | ||
asciidoctor.registerLogHandler(logRecords::add); | ||
asciidoctor.javaExtensionRegistry().blockMacro(LoggingBlockMacroProcessor.class); // <1> | ||
|
||
asciidoctor.convertFile(loggingmacro_adoc, | ||
Options.builder() | ||
.sourcemap(true) | ||
.toFile(false) | ||
.build()); | ||
|
||
assertEquals(1, logRecords.size()); | ||
assertTrue(logRecords.get(0).getCursor().getFile().endsWith("logging-macro.adoc")); | ||
assertEquals(3, logRecords.get(0).getCursor().getLineNumber()); | ||
assertEquals("HelloWorld", logRecords.get(0).getMessage()); | ||
//end::include[] | ||
} | ||
|
||
|
||
} |
5 changes: 5 additions & 0 deletions
5
asciidoctorj-documentation/src/test/resources/logging-macro.adoc
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
= Log Test | ||
|
||
== A logging macro | ||
|
||
log::HelloWorld[] |
This file contains 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 file contains 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 file contains 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 file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
= Log Test | ||
|
||
== A logging macro | ||
|
||
log::HelloWorld[] |
28 changes: 28 additions & 0 deletions
28
...sions/examples/org/asciidoctor/integrationguide/extension/LoggingBlockMacroProcessor.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.asciidoctor.integrationguide.extension; | ||
|
||
import org.asciidoctor.ast.StructuralNode; | ||
import org.asciidoctor.extension.BlockMacroProcessor; | ||
import org.asciidoctor.extension.Name; | ||
|
||
import java.util.Map; | ||
|
||
//tag::include[] | ||
@Name("log") | ||
public class LoggingBlockMacroProcessor extends BlockMacroProcessor { | ||
|
||
@Override | ||
public Object process( | ||
StructuralNode parent, String target, Map<String, Object> attributes) { | ||
|
||
String message = target; | ||
|
||
log(new org.asciidoctor.log.LogRecord( // <1> | ||
org.asciidoctor.log.Severity.INFO, | ||
parent.getSourceLocation(), // <2> | ||
message)); | ||
|
||
return createBlock(parent, "paragraph", "Hello from the logging macro"); | ||
} | ||
|
||
} | ||
//end::include[] |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
= Logging | ||
|
||
Extensions are also able to log messages that are handled in the same way as messages logged by Asciidoctor itself as explained in xref:ROOT:logs-handling.adoc[]. | ||
|
||
Logging messages via this API allows build tools like the Asciidoctor Maven plugin to capture them and for example xref:maven-tools:plugin:goals/process-asciidoc.adoc#configuration-logHandler[fail the build] in case an error or warning is logged. | ||
Therefore it might be preferable to use this instead of directly logging messages via slf4j or other APIs. | ||
|
||
Every extension inherits the method `Processor.log()` that allows it to log messages. | ||
The following example shows a Block Macro Processor that logs a message containing the target of the macro: | ||
|
||
[source,java] | ||
---- | ||
include::example$org/asciidoctor/integrationguide/extension/LoggingBlockMacroProcessor.java[tags=include] | ||
---- | ||
<1> The method `log(LogRecord)` is inherited from the Processor class hierarchy. | ||
<2> You can access the source location of the parent node to put the message in relation to the source document. | ||
Note that the source location will be null unless you enable the xref:ROOT:asciidoctor-api-options.adoc#sourcemap[sourcemap] option |