Skip to content

Commit

Permalink
MBS-11239 Add AndroidLogcatLoggingHandler.kt (#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
sboishtyan authored Dec 1, 2021
1 parent 5114cf1 commit 7cabdf2
Show file tree
Hide file tree
Showing 32 changed files with 107 additions and 127 deletions.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ include(":subprojects:logger:gradle-logger")
include(":subprojects:logger:logger")
include(":subprojects:logger:sentry-logger")
include(":subprojects:logger:elastic-logger")
include(":subprojects:logger:android-logger")

include(":subprojects:delivery:artifactory-app-backup")
include(":subprojects:delivery:cd")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ internal class BuildMetricsPluginTest {
),
buildGradleExtra = """
|gradleLogger {
| appendMetadata.set(true)
| printlnHandler(false, com.avito.logger.LogLevel.DEBUG)
|}
""".trimMargin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ internal class BuildCacheMetricsTest : BuildCacheTestFixture() {
}
gradleLogger {
appendMetadata.set(true)
printlnHandler(false, com.avito.logger.LogLevel.DEBUG)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ internal class HttpBuildCacheMetricsTest : HttpBuildCacheTestFixture() {
id("com.avito.android.build-metrics")
}
gradleLogger {
appendMetadata.set(true)
printlnHandler(false, com.avito.logger.LogLevel.DEBUG)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ internal class TasksMetricsTest {
},
buildGradleExtra = """
|gradleLogger {
| appendMetadata.set(true)
| printlnHandler(false, com.avito.logger.LogLevel.DEBUG)
|}
""".trimMargin(),
Expand Down
8 changes: 8 additions & 0 deletions subprojects/logger/android-logger/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
id("convention.kotlin-android-library")
id("convention.publish-android-library")
}

dependencies {
api(projects.subprojects.logger.logger)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.avito.android.logger"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.avito.logger.handler

import android.util.Log
import com.avito.logger.LogLevel

internal class AndroidLogcatLoggingHandler(
private val tag: String,
acceptedLogLevel: LogLevel
) : LogLevelLoggingHandler(acceptedLogLevel) {

override fun handleIfAcceptLogLevel(level: LogLevel, message: String, error: Throwable?) {
when (level) {
LogLevel.DEBUG -> Log.d(tag, message, error)
LogLevel.INFO -> Log.i(tag, message, error)
LogLevel.WARNING -> Log.w(tag, message, error)
LogLevel.CRITICAL -> Log.e(tag, message, error)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.avito.logger.handler

import com.avito.logger.LogLevel
import com.avito.logger.metadata.LoggerMetadata

public class AndroidLogcatLoggingHandlerProvider(private val acceptedLogLevel: LogLevel) : LoggingHandlerProvider {

override fun provide(metadata: LoggerMetadata): LoggingHandler {
return AndroidLogcatLoggingHandler(
tag = metadata.asMessagePrefix,
acceptedLogLevel = acceptedLogLevel
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.avito.logger.destination

import com.avito.android.elastic.ElasticClient
import com.avito.logger.LogLevel
import com.avito.logger.LoggerMetadata
import com.avito.logger.handler.LoggingHandler
import com.avito.logger.handler.LoggingHandlerProvider
import com.avito.logger.metadata.LoggerMetadata

public class ElasticLoggingHandlerProvider(
private val acceptedLogLevel: LogLevel,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.avito.logger

import com.avito.logger.metadata.LoggerMetadata
import java.nio.file.Path

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public abstract class GradleLoggerExtension {
val config: ElasticConfig
) : Serializable

public abstract val appendMetadata: Property<Boolean>
public abstract val fileHandler: Property<LogLevel>
public abstract val fileHandlerRootDir: DirectoryProperty
internal abstract val printlnHandler: Property<PrintlnMode>
Expand All @@ -44,7 +43,6 @@ public abstract class GradleLoggerExtension {
}

internal fun finalizeValues() {
appendMetadata.finalizeValue()
fileHandler.finalizeValue()
fileHandlerRootDir.finalizeValue()
printlnHandler.finalizeValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.avito.logger

import java.nio.file.Path

public data class GradleLoggerMetadata(
public override val tag: String,
public val coordinates: GradleLoggerCoordinates
internal class GradleLoggerMetadata(
private val tag: String,
private val coordinates: GradleLoggerCoordinates
) : FileHandledLoggerMetadata {

private val asString by lazy {
override val asMessagePrefix: String by lazy {
buildString {
append('[')
append(tag)
Expand Down Expand Up @@ -37,8 +37,6 @@ public data class GradleLoggerMetadata(
}
}

override fun asString(): String = asString

override fun asMap(): Map<String, String> {
val result = mutableMapOf(
"tag" to tag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class GradleLoggerPlugin : Plugin<Project> {
checkProjectIsRoot(target)
val extension = target.extensions.create("gradleLogger", GradleLoggerExtension::class.java)
extension.fileHandlerRootDir.set(target.layout.buildDirectory.dir("logs"))
extension.appendMetadata.set(true)
target.afterEvaluate {
/**
* The problem is some plugins could start use LoggerService before build script evaluated
Expand Down Expand Up @@ -102,7 +101,6 @@ public class GradleLoggerPlugin : Plugin<Project> {
.withZone(ZoneId.from(ZoneOffset.UTC))
val instant = Instant.now()
it.parameters {
it.appendMetadata.set(extension.appendMetadata)
it.printlnHandler.set(extension.printlnHandler)
it.fileHandler.set(extension.fileHandler)
it.fileHandlerRootDir.set(extension.fileHandlerRootDir.map { it.dir(formatter.format(instant)) })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.avito.logger

import com.avito.logger.metadata.LoggerMetadata
import com.avito.logger.metadata.LoggerMetadataProvider

internal class GradleMetadataProvider(
private val coordinates: GradleLoggerCoordinates
) : LoggerMetadataProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ internal class LegacyGradleLoggerConfigurator(

fun configure(parameters: LoggerService.Params) {
with(parameters) {
appendMetadata.set(true)
val verbosity = getVerbosity() ?: LogLevel.INFO
printlnHandler.set(GradleLoggerExtension.PrintlnMode(verbosity, doPrintStackTrace()))
fileHandler.set(LogLevel.INFO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.avito.android.elastic.ElasticClientFactory
import com.avito.android.sentry.sentryClient
import com.avito.logger.destination.ElasticLoggingHandlerProvider
import com.avito.logger.destination.SentryLoggingHandlerProvider
import com.avito.logger.formatter.AppendMetadataFormatter
import com.avito.logger.handler.FileLoggingHandlerProvider
import com.avito.logger.handler.LoggingHandlerProvider
import com.avito.logger.handler.PrintlnLoggingHandlerProvider
import org.gradle.api.file.DirectoryProperty
Expand All @@ -17,7 +17,6 @@ import org.gradle.api.services.BuildServiceParameters
public abstract class LoggerService : BuildService<LoggerService.Params> {

public interface Params : BuildServiceParameters {
public val appendMetadata: Property<Boolean>
public val fileHandler: Property<LogLevel>
public val fileHandlerRootDir: DirectoryProperty
public val printlnHandler: Property<GradleLoggerExtension.PrintlnMode>
Expand All @@ -32,8 +31,6 @@ public abstract class LoggerService : BuildService<LoggerService.Params> {
}
}

private val appendMetadata: Boolean = parameters.appendMetadata.getOrElse(false)

private val handlerProviders: List<LoggingHandlerProvider> by lazy {
val providers = mutableListOf<LoggingHandlerProvider>()
with(parameters) {
Expand Down Expand Up @@ -66,9 +63,6 @@ public abstract class LoggerService : BuildService<LoggerService.Params> {
public fun createLoggerFactory(coordinates: GradleLoggerCoordinates): LoggerFactory {
val builder = LoggerFactoryBuilder()
with(builder) {
if (appendMetadata) {
formatter(AppendMetadataFormatter)
}
metadataProvider(GradleMetadataProvider(coordinates))
handlerProviders.forEach { addLoggingHandlerProvider(it) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.avito.logger
package com.avito.logger.handler

import com.avito.logger.handler.LogLevelLoggingHandler
import com.avito.logger.LogLevel
import java.io.PrintWriter
import java.nio.file.Files
import java.nio.file.Path

internal class FileLoggingHandler(
private val messagePrefix: String,
acceptedLogLevel: LogLevel,
logFile: Path
) : LogLevelLoggingHandler(acceptedLogLevel) {
Expand All @@ -17,7 +18,7 @@ internal class FileLoggingHandler(
private val stackTraceWriter by lazy { PrintWriter(fileWriter) }

override fun handleIfAcceptLogLevel(level: LogLevel, message: String, error: Throwable?) {
fileWriter.write("[$level] $message")
fileWriter.write("$messagePrefix [$level] $message")
fileWriter.newLine()
if (error != null) {
val errorMessage = error.message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.avito.logger
package com.avito.logger.handler

import com.avito.logger.handler.LoggingHandler
import com.avito.logger.handler.LoggingHandlerProvider
import com.avito.logger.FileHandledLoggerMetadata
import com.avito.logger.LogLevel
import com.avito.logger.metadata.LoggerMetadata
import java.io.File
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
Expand Down Expand Up @@ -32,6 +33,6 @@ internal class FileLoggingHandlerProvider(
require(absoluteLogDirPath.isDirectory()) {
"Must be dir but was $absoluteLogDirPath"
}
return FileLoggingHandler(acceptedLogLevel, absoluteLogFilePath)
return FileLoggingHandler(metadata.asMessagePrefix, acceptedLogLevel, absoluteLogFilePath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.avito.logger

import com.avito.logger.handler.CompositeLoggingHandler
import com.avito.logger.handler.LoggingHandlerProvider
import com.avito.logger.metadata.LoggerMetadataProvider
import com.avito.logger.metadata.TagLoggerMetadataProvider

internal class DefaultLoggerFactory(
private val formatter: LoggingFormatter?,
private val metadataProvider: LoggerMetadataProvider,
private val handlerProviders: List<LoggingHandlerProvider>
) : LoggerFactory {
Expand All @@ -19,27 +20,15 @@ internal class DefaultLoggerFactory(
val metadata = metadataProvider.provide(tag)
val handlers = handlerProviders.map { it.provide(metadata) }
val handler = CompositeLoggingHandler(handlers)
return LoggerImpl(
handler = if (formatter != null) {
FormatterLoggingHandler(formatter, handler, metadata)
} else {
handler
},
)
return LoggerImpl(handler)
}
}

public class LoggerFactoryBuilder {

private var formatter: LoggingFormatter = LoggingFormatter.NoOpFormatter
private var metadataProvider: LoggerMetadataProvider = TagLoggerMetadataProvider
private val handlerProviders = mutableListOf<LoggingHandlerProvider>()

public fun formatter(formatter: LoggingFormatter): LoggerFactoryBuilder {
this.formatter = formatter
return this
}

public fun metadataProvider(provider: LoggerMetadataProvider): LoggerFactoryBuilder {
this.metadataProvider = provider
return this
Expand All @@ -54,15 +43,13 @@ public class LoggerFactoryBuilder {

public fun newBuilder(): LoggerFactoryBuilder {
return LoggerFactoryBuilder().also { newBuilder ->
newBuilder.formatter = formatter
newBuilder.metadataProvider = metadataProvider
newBuilder.handlerProviders.addAll(handlerProviders)
}
}

public fun build(): LoggerFactory {
return DefaultLoggerFactory(
formatter = formatter,
metadataProvider = metadataProvider,
handlerProviders = handlerProviders.toList()
)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.avito.logger.handler

import com.avito.logger.LoggerMetadata
import com.avito.logger.metadata.LoggerMetadata

public interface LoggingHandlerProvider {
public fun provide(metadata: LoggerMetadata): LoggingHandler
Expand Down
Loading

0 comments on commit 7cabdf2

Please sign in to comment.