最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

logback的FileAppender文件追加模式和沖突檢測解讀

 更新時間:2023年10月30日 10:02:11   作者:codecraft  
這篇文章主要為大家介紹了logback的FileAppender文件追加模式和沖突檢測解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

本文主要研究一下logback的FileAppender

FileAppender

ch/qos/logback/core/FileAppender.java

public class FileAppender<E> extends OutputStreamAppender<E> {
    public static final long DEFAULT_BUFFER_SIZE = 8192;
    static protected String COLLISION_WITH_EARLIER_APPENDER_URL = CODES_URL + "#earlier_fa_collision";
    /**
     * Append to or truncate the file? The default value for this variable is
     * <code>true</code>, meaning that by default a <code>FileAppender</code> will
     * append to an existing file and not truncate it.
     */
    protected boolean append = true;
    /**
     * The name of the active log file.
     */
    protected String fileName = null;
    private boolean prudent = false;
    private FileSize bufferSize = new FileSize(DEFAULT_BUFFER_SIZE);
    //......
}
FileAppender繼承了OutputStreamAppender,它定義了append、prudent、bufferSize屬性

start

public void start() {
        int errors = 0;
        if (getFile() != null) {
            addInfo("File property is set to [" + fileName + "]");
            if (prudent) {
                if (!isAppend()) {
                    setAppend(true);
                    addWarn("Setting \"Append\" property to true on account of \"Prudent\" mode");
                }
            }
            if (checkForFileCollisionInPreviousFileAppenders()) {
                addError("Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.");
                addError(MORE_INFO_PREFIX + COLLISION_WITH_EARLIER_APPENDER_URL);
                errors++;
            } else {
                // file should be opened only if collision free
                try {
                    openFile(getFile());
                } catch (java.io.IOException e) {
                    errors++;
                    addError("openFile(" + fileName + "," + append + ") call failed.", e);
                }
            }
        } else {
            errors++;
            addError("\"File\" property not set for appender named [" + name + "].");
        }
        if (errors == 0) {
            super.start();
        }
    }
start方法要求fileName必須有值,在prudent模式下會強制開啟append;另外start的時候會執(zhí)行checkForFileCollisionInPreviousFileAppenders判斷是否有沖突,沒有沖突則執(zhí)行openFile方法

checkForFileCollisionInPreviousFileAppenders

protected boolean checkForFileCollisionInPreviousFileAppenders() {
        boolean collisionsDetected = false;
        if (fileName == null) {
            return false;
        }
        @SuppressWarnings("unchecked")
        Map<String, String> map = (Map<String, String>) context.getObject(CoreConstants.FA_FILENAME_COLLISION_MAP);
        if (map == null) {
            return collisionsDetected;
        }
        for (Entry<String, String> entry : map.entrySet()) {
            if (fileName.equals(entry.getValue())) {
                addErrorForCollision("File", entry.getValue(), entry.getKey());
                collisionsDetected = true;
            }
        }
        if (name != null) {
            map.put(getName(), fileName);
        }
        return collisionsDetected;
    }
checkForFileCollisionInPreviousFileAppenders方法從上下文讀取FA_FILENAME_COLLISION_MAP,判斷有沒有文件名重復(fù)的,有則返回true

openFile

public void openFile(String file_name) throws IOException {
        lock.lock();
        try {
            File file = new File(file_name);
            boolean result = FileUtil.createMissingParentDirectories(file);
            if (!result) {
                addError("Failed to create parent directories for [" + file.getAbsolutePath() + "]");
            }

            ResilientFileOutputStream resilientFos = new ResilientFileOutputStream(file, append, bufferSize.getSize());
            resilientFos.setContext(context);
            setOutputStream(resilientFos);
        } finally {
            lock.unlock();
        }
    }
openFile方法加鎖創(chuàng)建file,然后通過createMissingParentDirectories來創(chuàng)建不存在的父目錄,最后創(chuàng)建根據(jù)file、append參數(shù)、bufferSize來創(chuàng)建ResilientFileOutputStream

stop

public void stop() {
        super.stop();

        Map<String, String> map = ContextUtil.getFilenameCollisionMap(context);
        if (map == null || getName() == null)
            return;

        map.remove(getName());
    }
stop方法會獲取FA_FILENAME_COLLISION_MAP,移除當前文件名

writeOut

protected void writeOut(E event) throws IOException {
        if (prudent) {
            safeWrite(event);
        } else {
            super.writeOut(event);
        }
    }
FileAppender覆蓋了OutputStreamAppender的writeOut方法,在prudent為true時執(zhí)行safeWrite

safeWrite

private void safeWrite(E event) throws IOException {
        ResilientFileOutputStream resilientFOS = (ResilientFileOutputStream) getOutputStream();
        FileChannel fileChannel = resilientFOS.getChannel();
        if (fileChannel == null) {
            return;
        }
        // Clear any current interrupt (see LOGBACK-875)
        boolean interrupted = Thread.interrupted();
        FileLock fileLock = null;
        try {
            fileLock = fileChannel.lock();
            long position = fileChannel.position();
            long size = fileChannel.size();
            if (size != position) {
                fileChannel.position(size);
            }
            super.writeOut(event);
        } catch (IOException e) {
            // Mainly to catch FileLockInterruptionExceptions (see LOGBACK-875)
            resilientFOS.postIOFailure(e);
        } finally {
            if (fileLock != null && fileLock.isValid()) {
                fileLock.release();
            }
            // Re-interrupt if we started in an interrupted state (see LOGBACK-875)
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
    }
safeWrite會通過fileChannel.lock()進行加鎖,然后執(zhí)行fileChannel.position(size),最后通過父類writeOut進行寫入;對于IOException會執(zhí)行resilientFOS.postIOFailure(e)

小結(jié)

logback的FileAppender繼承了OutputStreamAppender,它定義了append、prudent、bufferSize屬性,它使用的是ResilientFileOutputStream,其writeOut方法主要是新增了對prudent模式的支持,在prudent為true時采用的是safeWrite。

以上就是logback的FileAppender文件追加模式和沖突檢測解讀的詳細內(nèi)容,更多關(guān)于logback FileAppender沖突檢測的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java?IO流—異常及捕獲異常處理?try…catch…finally

    Java?IO流—異常及捕獲異常處理?try…catch…finally

    這篇文章主要介紹了Java?IO流—異常及捕獲異常處理?try…catch…finally,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 淺談Java中的atomic包實現(xiàn)原理及應(yīng)用

    淺談Java中的atomic包實現(xiàn)原理及應(yīng)用

    這篇文章主要介紹了淺談Java中的atomic包實現(xiàn)原理及應(yīng)用,涉及Atomic在硬件上的支持,Atomic包簡介及源碼分析等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 解決JDK21中用不了TimeUtild問題

    解決JDK21中用不了TimeUtild問題

    在使用TimeUtil時,可能因為IDE版本不兼容導(dǎo)致問題,升級IDEA到2023.2以上版本可解決此問題,詳細步驟可以通過評論區(qū)索取安裝包或直接從官網(wǎng)下載,分享個人經(jīng)驗,希望對大家有幫助
    2024-10-10
  • Java中正則表達式 .* 的含義講解

    Java中正則表達式 .* 的含義講解

    這篇文章主要介紹了Java中正則表達式 .* 的含義,通過舉例說明了正則表達式*,+,?的區(qū)別,本文給大家講解的非常詳細,需要的朋友可以參考下
    2023-05-05
  • SpringBoot教程_創(chuàng)建第一個SpringBoot項目

    SpringBoot教程_創(chuàng)建第一個SpringBoot項目

    這篇文章主要介紹了SpringBoot教程_創(chuàng)建第一個SpringBoot項目,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringBoot thymeleaf eclipse熱部署方案操作步驟

    SpringBoot thymeleaf eclipse熱部署方案操作步驟

    今天小編就為大家分享一篇關(guān)于SpringBoot thymeleaf eclipse熱部署方案操作步驟,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Lucene實現(xiàn)索引和查詢的實例講解

    Lucene實現(xiàn)索引和查詢的實例講解

    下面小編就為大家分享一篇Lucene實現(xiàn)索引和查詢的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 利用Mybatis向PostgreSQL中插入并查詢JSON字段

    利用Mybatis向PostgreSQL中插入并查詢JSON字段

    這篇文章主要介紹了利用Mybatis向PostgreSQL中插入并查詢JSON字段,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • Spingmvc中的HandlerMapping剖析

    Spingmvc中的HandlerMapping剖析

    這篇文章主要介紹了Spingmvc中的HandlerMapping剖析,Spingmvc中的HandlerMapping負責解析請求URL,對應(yīng)到Handler進行處理,這里的Handler一般為Controller里的一個方法method,也可以為servlet或者Controller等,需要的朋友可以參考下
    2023-09-09
  • java實現(xiàn)打印日歷

    java實現(xiàn)打印日歷

    這篇文章主要為大家詳細介紹了java打印日歷的實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01

最新評論

梁河县| 荃湾区| 鄱阳县| 开鲁县| 库车县| 京山县| 叶城县| 麦盖提县| 吴江市| 温泉县| 冀州市| 鄢陵县| 玉溪市| 满城县| 汝阳县| 梅州市| 兴文县| 崇州市| 察隅县| 比如县| 且末县| 衡东县| 曲水县| 汕尾市| 商河县| 郓城县| 三门县| 邳州市| 阳高县| 阿勒泰市| 莎车县| 茌平县| 昌乐县| 呼和浩特市| 陵水| 镇康县| 饶平县| 铜陵市| 丰顺县| 大埔区| 铁力市|