Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app一直打开 放置一宿 第二天手机开屏 直接打印日志 不会创建新一天的日志 会写入上一天的日志文件里 #69

Open
WangIdea opened this issue Mar 2, 2021 · 5 comments

Comments

@WangIdea
Copy link

WangIdea commented Mar 2, 2021

app一直打开 放置一宿 第二天手机开屏 直接打印日志 不会创建新一天的日志 会写入上一天的日志文件里

@pengwei1024
Copy link
Owner

会有这个问题,要做到准点切换文件代价比较大,而且就日志而言写到前一天影响也不会很大,基本上客户端日志 sdk 都有这个问题,你可以从应用层来解决哈

@WangIdea
Copy link
Author

WangIdea commented Mar 2, 2021

每次打印日志之前,我都把logutils的初始化配置在走一遍 也不会创建新一天的日志 为什么重启app 再走logutils的初始化配置之后 打印日志就可以创建新一天的日志呢, 因为我做的是商用设备 需要用到每一天的日志,有什么可以时间的方法吗?

@WangIdea
Copy link
Author

WangIdea commented Mar 2, 2021

是因为Log2FileConfigImpl是单例模式吗 app重启后 才会重新创建嘛

@pengwei1024
Copy link
Owner

是因为Log2FileConfigImpl是单例模式吗 app重启后 才会重新创建嘛

没有时机去刷新文件哈,要固定到12点准时切换有点麻烦

@ruirui1128
Copy link

只要在凌晨定时重新初始化一下,反射customFormatName参数为空就可以了

    /**
     * 主要作用 再app不重新启动的情况下重新生成日志
     */
    private fun initLog() {
        // 日志同步
        LogUtils.getLog2FileConfig()?.flushAsync()
        delay(100L) {
            // 此处用反射修改Log2FileConfigImpl.customFormatName=null 会重建日志(在不启动的情况下)
            val class1 = Class.forName("com.apkfuns.logutils.Log2FileConfigImpl")
            val customFormatName = class1.getDeclaredField("customFormatName")
            customFormatName.isAccessible = true
            customFormatName.set(LogUtils.getLog2FileConfig(), null)
            LogUtils.getLog2FileConfig()
                .configLog2FilePath(BaseConfig.LOG_UTIL_PATH)
                .configLog2FileEnable(true) // targetSdkVersion >= 23 需要确保有写sdcard权限
                .configLog2FileNameFormat("%d{yyyyMMdd}.txt")
                .configLogFileEngine(LogFactory(context))
                .flushAsync()
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants