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

can not write to the file that under the android getExternalFilesDir() #366

Open
3 tasks done
whatisjava opened this issue Apr 16, 2024 · 0 comments
Open
3 tasks done
Assignees

Comments

@whatisjava
Copy link

Describe the bug

i want record the log to android's context.getExternalFilesDir("logs")?.absolutePath?: context.filesDir?.absolutePath, the log path is: /storage/emulated/0/Android/data/cn.newup.elecdetonator/files/logs/app-log , the file is created, but not write anything in it, but the logcat has print same log content, why it's this? ,

Reproduction

in application clalss 's onCreate method call LogConfig.configureLogbackDirectly(this)

Logs

10:04:30.771 logdir D /storage/emulated/0/Android/data/cn.newup.elecdetonator/files/logs
10:04:30.912 System.out I 10:04:30,447 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [assets/logback.xml]
10:04:30.912 System.out I 10:04:30,824 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@218310893 - setting totalSizeCap to 5 GB
10:04:30.912 System.out I 10:04:30,827 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@218310893 - Archive files will be limited to [1 MB] each.
10:04:30.912 System.out I 10:04:30,834 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@218310893 - No compression will be used
10:04:30.912 System.out I 10:04:30,840 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@218310893 - Will use the pattern /storage/emulated/0/Android/data/cn.newup.elecdetonator/files/logs/%d{yyyy-MM-dd}/app-%i.log for the active file
10:04:30.912 System.out I 10:04:30,843 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@51ed322 - The date pattern is 'yyyy-MM-dd' from file name pattern '/storage/emulated/0/Android/data/cn.newup.elecdetonator/files/logs/%d{yyyy-MM-dd}/app-%i.log'.
10:04:30.912 System.out I 10:04:30,843 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@51ed322 - Roll-over at midnight.
10:04:30.912 System.out I 10:04:30,878 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@51ed322 - Setting initial period to Tue Apr 16 08:56:06 GMT+08:00 2024
10:04:30.912 System.out I 10:04:30,894 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[null] - Active log file name: /storage/emulated/0/Android/data/cn.newup.elecdetonator/files/logs/app.log
10:04:30.912 System.out I 10:04:30,895 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[null] - File property is set to [/storage/emulated/0/Android/data/cn.newup.elecdetonator/files/logs/app.log]
10:04:30.912 System.out I 10:04:30,899 |-INFO in ch.qos.logback.classic.AsyncAppender[null] - Attaching appender named [null] to AsyncAppender.

logback-android version

logbackAndroid = "3.0.0"

OS Version

Android 9.0

What logback configuration are you using? (logback.xml or Java/Kotlin code)

fun configureLogbackDirectly(context: android.content.Context){
        val loggerContext = LoggerFactory.getILoggerFactory() as LoggerContext
        loggerContext.stop()

        val logDir = context.getExternalFilesDir("logs")?.absolutePath?: context.filesDir?.absolutePath
        Timber.tag("logdir").d(logDir)

        // setup RollingFileAppender
        val rollingFileAppender = RollingFileAppender<ILoggingEvent>()
        rollingFileAppender.context = loggerContext
        rollingFileAppender.isAppend = true

        val patternLayoutEncoder = PatternLayoutEncoder()
        patternLayoutEncoder.context = loggerContext
        patternLayoutEncoder.pattern = """ [%date{yyyy-MM-dd HH:mm:ss.SSS}] %-5level %logger{0}.%M\(%F:%L\): - %msg%n """
        patternLayoutEncoder.charset = Charset.forName("UTF-8")
        patternLayoutEncoder.start()
        rollingFileAppender.encoder = patternLayoutEncoder

        rollingFileAppender.file = "${logDir}/app.log"

        val sizeAndTimeBasedRollingPolicy = SizeAndTimeBasedRollingPolicy<ILoggingEvent>()
        sizeAndTimeBasedRollingPolicy.context = loggerContext
        sizeAndTimeBasedRollingPolicy.fileNamePattern = "${logDir}/%d{yyyy-MM-dd}/app-%i.log"
        sizeAndTimeBasedRollingPolicy.maxHistory = 300
        sizeAndTimeBasedRollingPolicy.setParent(rollingFileAppender)
        sizeAndTimeBasedRollingPolicy.setMaxFileSize(FileSize.valueOf("1 mb"))
        sizeAndTimeBasedRollingPolicy.setTotalSizeCap(FileSize.valueOf("5 gb"))
        sizeAndTimeBasedRollingPolicy.isCleanHistoryOnStart = false
        sizeAndTimeBasedRollingPolicy.start()

        rollingFileAppender.rollingPolicy = sizeAndTimeBasedRollingPolicy

        rollingFileAppender.start()

        val asyncAppender = AsyncAppender()
        asyncAppender.context = loggerContext
        asyncAppender.name = "ASYNC"
        asyncAppender.addAppender(rollingFileAppender)

        // setup LogcatAppender
        val logcatTagEncoder = PatternLayoutEncoder()
        logcatTagEncoder.context = loggerContext
        logcatTagEncoder.pattern = "%logger{0}"
        logcatTagEncoder.start()

        val logcatAppender = LogcatAppender()
        logcatAppender.context = loggerContext
        logcatAppender.encoder = patternLayoutEncoder
        logcatAppender.tagEncoder = logcatTagEncoder
        logcatAppender.start()

        // add the newly created appenders to the root logger;
        // qualify Logger to disambiguate from org.slf4j.Logger
        val rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as Logger
        rootLogger.level = Level.TRACE
        rootLogger.addAppender(asyncAppender)
        rootLogger.addAppender(logcatAppender)

        // print any status messages (warnings, etc) encountered in logback config
        StatusPrinter.print(loggerContext)
    }

Validations

  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants