繁体   English   中英

File Appender无法正常登录-Android

[英]File Appender not working properly logback-android

我在SLF4J中使用logback-android 我正在以编程方式设置日志记录配置:

object LoggingConfiguration {
    private const val LOGCAT_PATTERN = "[%thread] %logger{32} | %msg%n"
    private const val FILE_PATTERN =
            "%date{ISO8601} %-5level [%thread] %logger{32} | %msg%n"
    private const val LOG_DIR = "logs"
    private const val FILE_SIZE = "1MB"
    private const val NUM_FILES = 5

    private val logger = LoggerFactory.getLogger(javaClass)

    @JvmStatic var logDirPath: String? = null
        private set
    var logFilePath: String? = null
        private set

    /**
     * Configure logging to append to logcat and create rolling log files.

     * @param context Application context. It is used to construct the path
     *                where the log files will be stored.
     *
     * @param tag A top level label for your log messages. It is used both as
     *            the logcat tag and as the log file name. If null, then a
     *            tag is derived from the package name of the context.
     *
     * @param logLevel String representation of a log level, as understood
     *                 by logback. Commonly used values are "WARN", "INFO",
     *                 "DEBUG" and "VERBOSE". For the full list, see
     *                 [ch.qos.logback.classic.Level]. If null, then
     *                 "DEBUG" is used.
     */
    @JvmStatic @JvmOverloads
    fun configure(context: Context, tag: String? = null, logLevel: String? = null) {
        val label = tag ?: ClassNameOnlyAbbreviator().abbreviate(context.packageName)

        val logDirPath = File(context.filesDir, LOG_DIR).absolutePath
        val logFilePath = String.format("%s/%s.log", logDirPath, label)
        val logFilePathPattern =
                String.format("%s/%s.%%i.log", logDirPath, label)

        val logcatTag = label
        val logbackLevel = Level.toLevel(logLevel)

        configureLogback(logbackLevel, logcatTag, logFilePath, logFilePathPattern)
        this.logDirPath = logDirPath
        this.logFilePath = logFilePath

        logger.info("Logging configured to append to logcat and file")
        logger.info("Log level: {} (parsed from string '{}')", logbackLevel, logLevel)
        logger.info("Logcat tag: {} ", logcatTag)
        logger.info("Log file path: {}", logFilePath)
    }

    private fun configureLogback(logLevel: Level, logcatTag: String,
                                 logFilePath: String, logFilePathPattern: String) {
        val loggerContext = LoggerFactory.getILoggerFactory() as LoggerContext
        loggerContext.reset()

        val rollingFileAppender = makeRollingLogFileAppender(logFilePath,
                logFilePathPattern, loggerContext)
        val logcatAppender = makeLogcatAppender(logcatTag, loggerContext)

        val rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as LogbackLogger

        StatusPrinter.print(loggerContext)
        rootLogger.apply {
            level = logLevel
            addAppender(rollingFileAppender)
            addAppender(logcatAppender)
        }
    }

    private fun makeRollingLogFileAppender(
            filePath: String, filePathPattern: String, context: LoggerContext):
            RollingFileAppender<ILoggingEvent> {
        val appender = RollingFileAppender<ILoggingEvent>().apply {
            this.context = context
            file = filePath
        }

        appender.encoder = PatternLayoutEncoder().apply {
            this.context = context
            pattern = FILE_PATTERN
            start()
        }

        appender.triggeringPolicy = SizeBasedTriggeringPolicy<ILoggingEvent>().apply {
            maxFileSize = FILE_SIZE
            start()
        }

        appender.rollingPolicy = FixedWindowRollingPolicy().apply {
            this.context = context
            minIndex = 1
            maxIndex = NUM_FILES
            fileNamePattern = filePathPattern
            setParent(appender)
            start()
        }

        return appender.apply {
            start()
        }
    }

    private fun makeLogcatAppender(tag: String, context: LoggerContext): LogcatAppender {
        val encoder = PatternLayoutEncoder().apply {
            this.context = context
            pattern = LOGCAT_PATTERN
            start()
        }

        val tagEncoder = PatternLayoutEncoder().apply {
            this.context = context
            pattern = tag
            start()
        }

        return LogcatAppender().apply {
            this.context = context
            this.encoder = encoder
            this.tagEncoder = tagEncoder
            start()
        }
    }
}

我从Application.onCreate()调用configure(context: Context, tag: String? = null, logLevel: String? = null)方法。

我正在LogCat中获取所有日志,但仅来自文件中特定类的日志。 我不确定自己在做什么错。 我制作了另一个具有相同配置的项目,但是那里没有发生相同的问题。

我尝试了各种配置和logback.xml方法,但无法将其写入文件。

我得到了错误。 这不是logback-android中的错误。

我正在使用组织的一个jar,该jar具有一个静态块,用于重置loggerContext并追加LogcatAppender 我删除了配置,并从jar中使用了配置,并添加了附加文件。 一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM