Forked from begrossi/LazyInitRollingFileAppender.java
Created
October 10, 2024 10:00
-
-
Save MattAlp/440b859b541d9c51653e6cdd86bc7769 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* RollingFileAppender with Lazy Initialization on GraalVM native image. | |
*/ | |
public class LazyInitRollingFileAppender<E> extends ch.qos.logback.core.rolling.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 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"; | |
/** | |
* 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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment