Skip to content

Instantly share code, notes, and snippets.

@begrossi
Last active October 10, 2024 10:00
Show Gist options
  • Select an option

  • Save begrossi/d807280f54d3378d407e9c9a95e5d905 to your computer and use it in GitHub Desktop.

Select an option

Save begrossi/d807280f54d3378d407e9c9a95e5d905 to your computer and use it in GitHub Desktop.

Revisions

  1. begrossi revised this gist Feb 9, 2021. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions LazyInitRollingFileAppender.java
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    import ch.qos.logback.core.rolling.RollingFileAppender;

    /**
    * RollingFileAppender with Lazy Initialization on GraalVM native image.
    */
    public class LazyInitRollingFileAppender<E> extends RollingFileAppender<E> {
    public class LazyInitRollingFileAppender<E> extends ch.qos.logback.core.rolling.RollingFileAppender<E> {
    private boolean started = false;

    @Override
  2. begrossi revised this gist Feb 9, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LazyInitRollingFileAppender.java
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ public void doAppend(E eventObject) {
    }


    //THE ABOVE CODE CAN BE SUBSTITUTED BY ImageInfo.inImageBuildtimeCode() if you have it on your classpath
    //THE BELOW CODE CAN BE SUBSTITUTED BY ImageInfo.inImageBuildtimeCode() if you have it on your classpath

    private static final String PROPERTY_IMAGE_CODE_VALUE_BUILDTIME = "buildtime";
    private static final String PROPERTY_IMAGE_CODE_KEY = "org.graalvm.nativeimage.imagecode";
  3. begrossi created this gist Feb 9, 2021.
    56 changes: 56 additions & 0 deletions LazyInitRollingFileAppender.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    import ch.qos.logback.core.rolling.RollingFileAppender;

    /**
    * RollingFileAppender with Lazy Initialization on GraalVM native image.
    */
    public class LazyInitRollingFileAppender<E> extends RollingFileAppender<E> {
    private boolean started = false;

    @Override
    public void start() {
    if (!inGraalImageBuildtimeCode()) {
    super.start();
    this.started = true;
    }

    }

    /**
    * This method is synchronized to avoid double start from doAppender().
    */
    protected void maybeStart() {
    lock.lock();
    try {
    if (!started)
    this.start();
    } finally {
    lock.unlock();
    }
    }

    @Override
    public void doAppend(E eventObject) {
    if (!inGraalImageBuildtimeCode()) {
    if (!started)
    maybeStart();

    super.doAppend(eventObject);
    }
    }


    //THE ABOVE CODE CAN BE SUBSTITUTED BY ImageInfo.inImageBuildtimeCode() if you have it on your classpath

    private static final String PROPERTY_IMAGE_CODE_VALUE_BUILDTIME = "buildtime";
    private static final String PROPERTY_IMAGE_CODE_KEY = "org.graalvm.nativeimage.imagecode";
    /**
    * Returns true if (at the time of the call) code is executing in the context of Graal native image building
    * (e.g. in a static initializer of class that will be contained in the image).
    * Copy of graal code in org.graalvm.nativeimage.ImageInfo.inImageBuildtimeCode().
    * https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java
    */
    private static boolean inGraalImageBuildtimeCode() {
    return PROPERTY_IMAGE_CODE_VALUE_BUILDTIME.equals(System.getProperty(PROPERTY_IMAGE_CODE_KEY));
    }

    }