Search in sources :

Example 1 with StartEvent

use of ch.qos.logback.core.joran.event.StartEvent in project quarkus-logging-logback by quarkiverse.

the class LoggingLogbackProcessor method init.

@Record(ExecutionTime.STATIC_INIT)
@BuildStep
void init(LogbackRecorder recorder, RecorderContext context, BuildProducer<RunTimeConfigurationDefaultBuildItem> runTimeConfigurationDefaultBuildItemBuildProducer, BuildProducer<GeneratedClassBuildItem> generatedClasses, OutputTargetBuildItem outputTargetBuildItem, CurateOutcomeBuildItem curateOutcomeBuildItem, ShutdownContextBuildItem shutdownContextBuildItem) throws Exception {
    // first check the versions
    doVersionCheck();
    URL url = getUrl();
    if (url == null) {
        return;
    }
    context.registerSubstitution(StartEvent.class, StartSub.class, (Class) EventSubstitution.class);
    context.registerSubstitution(BodyEvent.class, BodySub.class, (Class) EventSubstitution.class);
    context.registerSubstitution(EndEvent.class, EndSub.class, (Class) EventSubstitution.class);
    final AtomicReference<List<SaxEvent>> events = new AtomicReference<>();
    JoranConfigurator configurator = new JoranConfigurator() {

        @Override
        public void doConfigure(List<SaxEvent> eventList) throws JoranException {
            events.set(eventList);
        }
    };
    configurator.setContext(new LoggerContext());
    configurator.doConfigure(url);
    List<String> loggerPath = Arrays.asList("configuration", "logger");
    List<String> rootPath = Arrays.asList("configuration", "root");
    String rootLevel = null;
    Map<String, String> levels = new HashMap<>();
    Set<String> allClasses = new HashSet<>();
    for (SaxEvent i : events.get()) {
        if (i instanceof StartEvent) {
            StartEvent s = ((StartEvent) i);
            if (Objects.equals(loggerPath, s.elementPath.getCopyOfPartList())) {
                String level = s.attributes.getValue("level");
                if (level != null) {
                    levels.put(s.attributes.getValue("name"), level);
                }
            } else if (Objects.equals(rootPath, s.elementPath.getCopyOfPartList())) {
                String level = s.attributes.getValue("level");
                if (level != null) {
                    rootLevel = level;
                }
            }
            int classIndex = s.attributes.getIndex("class");
            if (classIndex != -1) {
                allClasses.add(s.attributes.getValue(classIndex));
            }
        }
    }
    boolean disableConsole = false;
    Set<String> delayedClasses = new HashSet<>();
    for (String i : allClasses) {
        if (i.equals("ch.qos.logback.core.ConsoleAppender")) {
            disableConsole = true;
        }
        try {
            Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(i);
            if (LifeCycle.class.isAssignableFrom(c)) {
                delayedClasses.add(i);
            }
        } catch (ClassNotFoundException exception) {
            throw new RuntimeException(exception);
        }
    }
    if (disableConsole) {
        runTimeConfigurationDefaultBuildItemBuildProducer.produce(new RunTimeConfigurationDefaultBuildItem("quarkus.log.console.enable", "false"));
    }
    for (String i : delayedClasses) {
        try (ClassCreator c = new ClassCreator(new GeneratedClassGizmoAdaptor(generatedClasses, (Function<String, String>) s -> s.substring(s.length() - LogbackRecorder.DELAYED.length())), i + LogbackRecorder.DELAYED, null, i, DelayedStart.class.getName())) {
            MethodCreator start = c.getMethodCreator("start", void.class);
            start.invokeStaticMethod(MethodDescriptor.ofMethod(LogbackRecorder.class, "addDelayed", void.class, DelayedStart.class), start.getThis());
            start.returnValue(null);
            MethodCreator method = c.getMethodCreator("doQuarkusDelayedStart", void.class);
            method.invokeSpecialMethod(MethodDescriptor.ofMethod(i, "start", void.class), method.getThis());
            method.returnValue(null);
        }
    }
    if (rootLevel != null) {
        runTimeConfigurationDefaultBuildItemBuildProducer.produce(new RunTimeConfigurationDefaultBuildItem("quarkus.log.level", rootLevel));
    }
    for (Map.Entry<String, String> e : levels.entrySet()) {
        runTimeConfigurationDefaultBuildItemBuildProducer.produce(new RunTimeConfigurationDefaultBuildItem("quarkus.log.category.\"" + e.getKey() + "\".level", e.getValue()));
    }
    Map<String, String> buildProperties = new HashMap<>(outputTargetBuildItem.getBuildSystemProperties().entrySet().stream().collect(Collectors.toMap(Object::toString, Object::toString)));
    buildProperties.put(PROJECT_VERSION, curateOutcomeBuildItem.getEffectiveModel().getAppArtifact().getVersion());
    recorder.init(events.get(), delayedClasses, shutdownContextBuildItem, buildProperties);
}
Also used : DelayedStart(io.quarkiverse.logback.runtime.DelayedStart) HashMap(java.util.HashMap) ClassCreator(io.quarkus.gizmo.ClassCreator) URL(java.net.URL) LogbackRecorder(io.quarkiverse.logback.runtime.LogbackRecorder) Function(java.util.function.Function) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) StartEvent(ch.qos.logback.core.joran.event.StartEvent) List(java.util.List) EventSubstitution(io.quarkiverse.logback.runtime.events.EventSubstitution) HashSet(java.util.HashSet) AtomicReference(java.util.concurrent.atomic.AtomicReference) LoggerContext(ch.qos.logback.classic.LoggerContext) GeneratedClassGizmoAdaptor(io.quarkus.deployment.GeneratedClassGizmoAdaptor) MethodCreator(io.quarkus.gizmo.MethodCreator) SaxEvent(ch.qos.logback.core.joran.event.SaxEvent) RunTimeConfigurationDefaultBuildItem(io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem) Map(java.util.Map) HashMap(java.util.HashMap) Record(io.quarkus.deployment.annotations.Record) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with StartEvent

use of ch.qos.logback.core.joran.event.StartEvent in project quarkus-logging-logback by quarkiverse.

the class LogbackRecorder method init.

public void init(List<SaxEvent> originalEvents, Set<String> delayedStartClasses, ShutdownContext context, Map<String, String> buildSystemProps) {
    EventSubstitution substitution = new EventSubstitution();
    if (defaultLoggerContext == null) {
        SmallRyeConfig config = (SmallRyeConfig) SmallRyeConfigProviderResolver.instance().getConfig();
        List<SaxEvent> configEvents = new ArrayList<>();
        for (SaxEvent i : originalEvents) {
            if (i instanceof StartEvent) {
                AttributesImpl impl = (AttributesImpl) ((StartEvent) i).attributes;
                int index = impl.getIndex("class");
                if (index > -1) {
                    String val = impl.getValue(index);
                    if (delayedStartClasses.contains(val)) {
                        impl.setValue(index, val + DELAYED);
                    }
                }
                for (int j = 1; j <= impl.getLength(); ++j) {
                    String val = impl.getValue(index);
                    if (val != null && val.contains("${")) {
                        final String expanded = doExpand(config, val, buildSystemProps);
                        impl.setValue(j, expanded);
                    }
                }
                configEvents.add(i);
            } else if (i instanceof BodyEvent) {
                String val = ((BodyEvent) i).getText();
                if (val.contains("${")) {
                    final String expanded = doExpand(config, val, buildSystemProps);
                    configEvents.add(substitution.deserialize(new BodySub(i.getNamespaceURI(), i.getLocalName(), i.getQName(), i.getLocator(), expanded)));
                } else {
                    configEvents.add(i);
                }
            } else {
                configEvents.add(i);
            }
        }
        defaultLoggerContext = new LoggerContext();
        try {
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(defaultLoggerContext);
            configurator.doConfigure(configEvents);
            // logback-292
            if (!StatusUtil.contextHasStatusListener(defaultLoggerContext)) {
                StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext);
            }
        } catch (Exception t) {
            // see LOGBACK-1159
            Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t);
        }
        context.addLastShutdownTask(new Runnable() {

            @Override
            public void run() {
                defaultLoggerContext.stop();
                defaultLoggerContext = null;
                started = false;
            }
        });
    }
}
Also used : BodySub(io.quarkiverse.logback.runtime.events.BodySub) ArrayList(java.util.ArrayList) BodyEvent(ch.qos.logback.core.joran.event.BodyEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) AttributesImpl(org.xml.sax.helpers.AttributesImpl) SmallRyeConfig(io.smallrye.config.SmallRyeConfig) SaxEvent(ch.qos.logback.core.joran.event.SaxEvent) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) StartEvent(ch.qos.logback.core.joran.event.StartEvent) EventSubstitution(io.quarkiverse.logback.runtime.events.EventSubstitution)

Example 3 with StartEvent

use of ch.qos.logback.core.joran.event.StartEvent in project logback-android by tony19.

the class EventPlayer method play.

public void play(List<SaxEvent> aSaxEventList) {
    eventList = aSaxEventList;
    SaxEvent se;
    for (currentIndex = 0; currentIndex < eventList.size(); currentIndex++) {
        se = eventList.get(currentIndex);
        if (se instanceof StartEvent) {
            interpreter.startElement((StartEvent) se);
            // invoke fireInPlay after startElement processing
            interpreter.getInterpretationContext().fireInPlay(se);
        }
        if (se instanceof BodyEvent) {
            // invoke fireInPlay before  characters processing
            interpreter.getInterpretationContext().fireInPlay(se);
            interpreter.characters((BodyEvent) se);
        }
        if (se instanceof EndEvent) {
            // invoke fireInPlay before endElement processing
            interpreter.getInterpretationContext().fireInPlay(se);
            interpreter.endElement((EndEvent) se);
        }
    }
}
Also used : SaxEvent(ch.qos.logback.core.joran.event.SaxEvent) StartEvent(ch.qos.logback.core.joran.event.StartEvent) EndEvent(ch.qos.logback.core.joran.event.EndEvent) BodyEvent(ch.qos.logback.core.joran.event.BodyEvent)

Aggregations

SaxEvent (ch.qos.logback.core.joran.event.SaxEvent)3 StartEvent (ch.qos.logback.core.joran.event.StartEvent)3 LoggerContext (ch.qos.logback.classic.LoggerContext)2 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)2 BodyEvent (ch.qos.logback.core.joran.event.BodyEvent)2 EventSubstitution (io.quarkiverse.logback.runtime.events.EventSubstitution)2 EndEvent (ch.qos.logback.core.joran.event.EndEvent)1 DelayedStart (io.quarkiverse.logback.runtime.DelayedStart)1 LogbackRecorder (io.quarkiverse.logback.runtime.LogbackRecorder)1 BodySub (io.quarkiverse.logback.runtime.events.BodySub)1 GeneratedClassGizmoAdaptor (io.quarkus.deployment.GeneratedClassGizmoAdaptor)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 Record (io.quarkus.deployment.annotations.Record)1 RunTimeConfigurationDefaultBuildItem (io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem)1 ClassCreator (io.quarkus.gizmo.ClassCreator)1 MethodCreator (io.quarkus.gizmo.MethodCreator)1 SmallRyeConfig (io.smallrye.config.SmallRyeConfig)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1