Skip to content

Commit

Permalink
Add plugin mechanism for Loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpanzer committed Jun 19, 2018
1 parent af104d9 commit 1e37588
Show file tree
Hide file tree
Showing 22 changed files with 672 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.asciidoctor.extension.JavaExtensionRegistry;
import org.asciidoctor.extension.RubyExtensionRegistry;
import org.asciidoctor.internal.JRubyAsciidoctor;
import org.asciidoctor.log.LogHandler;

/**
*
Expand Down Expand Up @@ -910,5 +911,8 @@ public static Asciidoctor create(List<String> loadPaths, String gemPath) {
*/
Document loadFile(File file, Map<String, Object> options);

void registerLogHandler(LogHandler logHandler);

void unregisterLogHandler(LogHandler logHandler);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@ public int getLineNumber() {

@Override
public String getPath() {
return getString("path");
final Object result = getProperty("path");
return result == null ? null : result.toString();
}

@Override
public String getDir() {
return getString("dir");
final Object result = getProperty("dir");
return result == null ? null : result.toString();
}

@Override
public String getFile() {
return getString("file");
final Object result = getProperty("file");
return result == null ? null : result.toString();
}

@Override
public String toString() {
return getString("to_s");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* super(blockName);
* }
*
* public Object process(StructuralNode parent, Reader reader, Map<String, Object> attributes) {
* List<String> lines = reader.readLines();
* List<String> newLines = new ArrayList<>();
* public Object process(StructuralNode parent, Reader reader, Map&lt;String, Object&gt; attributes) {
* List&lt;String&gt; lines = reader.readLines();
* List&lt;String&gt; newLines = new ArrayList<>();
* for (String line: lines) {
* newLines.add(line.toUpperCase());
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.asciidoctor.ast.NodeConverter;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.ast.StructuredDocument;
import org.asciidoctor.ast.Title;
import org.asciidoctor.ast.impl.ContentPartImpl;
import org.asciidoctor.ast.impl.DocumentHeaderImpl;
import org.asciidoctor.ast.impl.StructuredDocumentImpl;
Expand All @@ -21,6 +20,11 @@
import org.asciidoctor.extension.JavaExtensionRegistry;
import org.asciidoctor.extension.RubyExtensionRegistry;
import org.asciidoctor.extension.internal.ExtensionRegistryExecutor;
import org.asciidoctor.log.LogHandler;
import org.asciidoctor.log.LogRecord;
import org.asciidoctor.log.internal.JULLogHandler;
import org.asciidoctor.log.internal.JavaLogger;
import org.asciidoctor.log.internal.LogHandlerRegistryExecutor;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyHash;
Expand All @@ -44,9 +48,10 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JRubyAsciidoctor implements Asciidoctor {
public class JRubyAsciidoctor implements Asciidoctor, LogHandler {

private static final Logger logger = Logger.getLogger(JRubyAsciidoctor.class.getName());

Expand All @@ -59,6 +64,10 @@ public class JRubyAsciidoctor implements Asciidoctor {

private RubyClass extensionGroupClass;

private List<LogHandler> logHandlers = new ArrayList<>();

private final static Logger LOGGER = Logger.getLogger("asciidoctorj");

private JRubyAsciidoctor(final Ruby rubyRuntime) {
this.rubyRuntime = rubyRuntime;

Expand All @@ -67,6 +76,7 @@ private JRubyAsciidoctor(final Ruby rubyRuntime) {
this.rubyRuntime.evalScriptlet(script);

this.rubyGemsPreloader = new RubyGemsPreloader(this.rubyRuntime);
this.logHandlers.add(new JULLogHandler());
}

public static JRubyAsciidoctor create() {
Expand Down Expand Up @@ -96,6 +106,7 @@ public static JRubyAsciidoctor create(List<String> loadPaths, String gemPath) {
private static JRubyAsciidoctor processRegistrations(JRubyAsciidoctor asciidoctor) {
registerExtensions(asciidoctor);
registerConverters(asciidoctor);
registerLogHandlers(asciidoctor);
return asciidoctor;
}

Expand All @@ -107,6 +118,10 @@ private static void registerExtensions(Asciidoctor asciidoctor) {
new ExtensionRegistryExecutor(asciidoctor).registerAllExtensions();
}

private static void registerLogHandlers(Asciidoctor asciidoctor) {
new LogHandlerRegistryExecutor(asciidoctor).registerAllLogHandlers();
}

private static JRubyAsciidoctor createJRubyAsciidoctorInstance(Map<String, String> environmentVars, List<String> loadPaths, ClassLoader classloader) {

Map<String, String> env = environmentVars != null ?
Expand All @@ -120,7 +135,11 @@ private static JRubyAsciidoctor createJRubyAsciidoctorInstance(Map<String, Strin

Ruby rubyRuntime = JavaEmbedUtils.initialize(loadPaths, config);

return new JRubyAsciidoctor(rubyRuntime);
JRubyAsciidoctor jrubyAsciidoctor = new JRubyAsciidoctor(rubyRuntime);

JavaLogger.install(rubyRuntime, jrubyAsciidoctor);

return jrubyAsciidoctor;
}

private static void injectEnvironmentVariables(RubyInstanceConfig config, Map<String, String> environmentVars) {
Expand All @@ -132,6 +151,18 @@ private static RubyInstanceConfig createOptimizedConfiguration() {
return new RubyInstanceConfig();
}

@Override
public void registerLogHandler(final LogHandler logHandler) {
if (!this.logHandlers.contains(logHandler)) {
this.logHandlers.add(logHandler);
}
}

@Override
public void unregisterLogHandler(final LogHandler logHandler) {
this.logHandlers.remove(logHandler);
}

public Ruby getRubyRuntime() {
return rubyRuntime;
}
Expand All @@ -140,7 +171,7 @@ private DocumentHeader toDocumentHeader(Document document) {

Document documentImpl = (Document) NodeConverter.createASTNode(document);

return DocumentHeaderImpl.createDocumentHeader((Title) documentImpl.getStructuredDoctitle(), documentImpl.getDoctitle(),
return DocumentHeaderImpl.createDocumentHeader(documentImpl.getStructuredDoctitle(), documentImpl.getDoctitle(),
documentImpl.getAttributes());
}

Expand Down Expand Up @@ -642,4 +673,15 @@ private RubyClass getExtensionGroupClass() {
}
return extensionGroupClass;
}

@Override
public void log(LogRecord logRecord) {
for (LogHandler logHandler: logHandlers) {
try {
logHandler.log(logRecord);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unexpected exception while logging Asciidoctor log entry", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.asciidoctor.log;

public interface LogHandler {

void log(LogRecord logRecord);

}
44 changes: 44 additions & 0 deletions asciidoctorj-core/src/main/java/org/asciidoctor/log/LogRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.asciidoctor.log;

import org.asciidoctor.ast.Cursor;

public class LogRecord {

private final Severity severity;

private final Cursor cursor;

private final String message;

private final String sourceFileName;

private final String sourceMethodName;

public LogRecord(Severity severity, Cursor cursor, String message, String sourceFileName, String sourceMethodName) {
this.severity = severity;
this.cursor = cursor;
this.message = message;
this.sourceFileName = sourceFileName;
this.sourceMethodName = sourceMethodName;
}

public Severity getSeverity() {
return severity;
}

public Cursor getCursor() {
return cursor;
}

public String getMessage() {
return message;
}

public String getSourceFileName() {
return sourceFileName;
}

public String getSourceMethodName() {
return sourceMethodName;
}
}
20 changes: 20 additions & 0 deletions asciidoctorj-core/src/main/java/org/asciidoctor/log/Severity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.asciidoctor.log;

public enum Severity {
DEBUG(0),
INFO(1),
WARN(2),
ERROR(3),
FATAL(4),
UNKNOWN(5);

private int rubyId;

private Severity(final int rubyId) {
this.rubyId = rubyId;
}

public int getRubyId() {
return rubyId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.asciidoctor.log.internal;

import org.asciidoctor.log.LogHandler;
import org.asciidoctor.log.LogRecord;
import org.asciidoctor.log.Severity;

import java.util.logging.Level;
import java.util.logging.Logger;

public class JULLogHandler implements LogHandler {

private final static Logger LOGGER = Logger.getLogger("asciidoctor");

@Override
public void log(LogRecord logRecord) {
final java.util.logging.LogRecord julLogRecord =
new java.util.logging.LogRecord(
mapSeverity(logRecord.getSeverity()),
logRecord.getCursor() != null ? logRecord.getCursor().toString() + ": " + logRecord.getMessage() : logRecord.getMessage());

julLogRecord.setSourceClassName(logRecord.getSourceFileName());
julLogRecord.setSourceMethodName(logRecord.getSourceMethodName());
julLogRecord.setParameters(new Object[] { logRecord.getCursor() });
LOGGER.log(julLogRecord);
}

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;
}
}

}
Loading

0 comments on commit 1e37588

Please sign in to comment.