From 11b1959582c5a6f06a8dcffc599e42ba50e17c58 Mon Sep 17 00:00:00 2001 From: Anatoly Kislov Date: Sat, 21 Oct 2017 19:56:39 +0300 Subject: [PATCH 1/3] feat: add sentry error reporting --- .gitignore | 5 +- build.gradle | 10 +- src/main/kotlin/app/Analytics.kt | 36 +---- src/main/kotlin/app/Logger.kt | 128 ++++++++++++++++-- src/main/kotlin/app/Main.kt | 27 ++-- src/main/kotlin/app/api/ServerApi.kt | 3 - .../kotlin/app/config/FileConfigurator.kt | 31 ++--- src/main/kotlin/app/hashers/CodeLongevity.kt | 14 +- src/main/kotlin/app/hashers/CommitCrawler.kt | 2 +- src/main/kotlin/app/hashers/CommitHasher.kt | 6 +- src/main/kotlin/app/hashers/FactHasher.kt | 2 +- src/main/kotlin/app/hashers/RepoHasher.kt | 11 +- src/main/kotlin/app/ui/AddRepoState.kt | 3 +- src/main/kotlin/app/ui/AuthState.kt | 5 +- src/main/kotlin/app/ui/UpdateRepoState.kt | 13 +- src/main/kotlin/app/utils/RepoHelper.kt | 8 +- 16 files changed, 189 insertions(+), 115 deletions(-) diff --git a/.gitignore b/.gitignore index 928ac4c2..1ab68134 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,9 @@ build/ sourcerer-app.iml sourcerer-app.ipr sourcerer-app.iws +app.iml +app.ipr +app.iws /confluence/target /dependencies /dist @@ -16,4 +19,4 @@ sourcerer-app.iws /ultimate/dependencies /ultimate/ideaSDK /ultimate/out -/ultimate/tmp \ No newline at end of file +/ultimate/tmp diff --git a/build.gradle b/build.gradle index d7c4e381..49dea86f 100644 --- a/build.gradle +++ b/build.gradle @@ -39,21 +39,28 @@ buildConfig { apiBasePath = 'https://sourcerer.io/api/commit' } apiBasePath = project.hasProperty('api') ? api : apiBasePath - buildConfigField 'String', 'API_BASE_PATH', apiBasePath // Common. + buildConfigField 'String', 'ENVIRONMENT', ext.environment buildConfigField 'String', 'PROFILE_URL', 'https://sourcerer.io/' // App version. buildConfigField 'int', 'VERSION_CODE', '1' buildConfigField 'String', 'VERSION', '0.0.1' + // Logging. + buildConfigField 'int', 'LOG_LEVEL', '3' + // Google Analytics. buildConfigField 'String', 'GA_BASE_PATH', 'http://www.google-analytics.com' buildConfigField 'String', 'GA_TRACKING_ID', 'UA-107129190-2' buildConfigField 'boolean', 'IS_GA_ENABLED', 'true' + // Logging. + buildConfigField 'String', 'SENTRY_DSN', 'https://0263d6473bd24a9ba40e25aa5fb0a242:c5451dc815074bff8ce3fb9f0851f2f5@sentry.io/233260' + buildConfigField 'boolean', 'PRINT_STACK_TRACE', 'true' + buildConfig } @@ -89,6 +96,7 @@ dependencies { version: '4.8.0.201706111038-r' compile "org.slf4j:slf4j-nop:1.7.2" compile 'org.jpmml:pmml-evaluator:1.3.9' + compile 'io.sentry:sentry:1.6.0' testCompile 'org.jetbrains.kotlin:kotlin-test' testCompile 'org.jetbrains.spek:spek-api:1.1.4' diff --git a/src/main/kotlin/app/Analytics.kt b/src/main/kotlin/app/Analytics.kt index ecbe740e..96b0da01 100644 --- a/src/main/kotlin/app/Analytics.kt +++ b/src/main/kotlin/app/Analytics.kt @@ -51,7 +51,7 @@ object Analytics { * - t: Hit Type - type of event * - dp: Document Path - virtual url */ - private fun trackEvent(event: String, params: List = listOf()) { + fun trackEvent(event: String, params: List = listOf()) { if (!IS_ENABLED || (username.isEmpty() && uuid.isEmpty())) { return } @@ -75,44 +75,16 @@ object Analytics { post(params + defaultParams.filter { !params.contains(it) } + idParams).responseString() } catch (e: Throwable) { - Logger.error("Error while sending error report", e, logOnly = true) + Logger.error(e, "Error while sending GA report", logOnly = true) } } - fun trackStart() { - trackEvent("start") - } - - fun trackAuth() { - trackEvent("auth") - } - - fun trackConfigSetup() { - trackEvent("config/setup") - } - - fun trackConfigChanged() { - trackEvent("config/changed") - } - - fun trackHashingRepoSuccess() { - trackEvent("hashing/repo/success") - } - - fun trackHashingSuccess() { - trackEvent("hashing/success") - } - - fun trackError(e: Throwable? = null, code: String = "") { - val url = if (e != null) getErrorUrl(e) else code + fun trackError(e: Throwable? = null) { + val url = if (e != null) getErrorUrl(e) else "" val separator = if (url.isNotEmpty()) "/" else "" trackEvent("error" + separator + url, listOf("t" to HIT_EXCEPTION)) } - fun trackExit() { - trackEvent("exit") - } - private fun getErrorUrl(e: Throwable): String { // Mapping for request exceptions. when (e) { diff --git a/src/main/kotlin/app/Logger.kt b/src/main/kotlin/app/Logger.kt index 0b2d913f..a1e7e8ed 100644 --- a/src/main/kotlin/app/Logger.kt +++ b/src/main/kotlin/app/Logger.kt @@ -3,69 +3,133 @@ package app +import io.sentry.Sentry +import io.sentry.context.Context +import io.sentry.event.Breadcrumb +import io.sentry.event.UserBuilder +import io.sentry.event.BreadcrumbBuilder +import java.util.* + + /** * Singleton class that logs events of different levels. */ object Logger { + object Events { + val START = "start" + val AUTH = "auth" + val CONFIG_SETUP = "config/setup" + val CONFIG_CHANGED = "config/changed" + val HASHING_REPO_SUCCES = "hashing/repo/success" + val HASHING_SUCCESS = "hashing/success" + val EXIT = "exit" + } + /** * Current log level. All that higher than this level will not be displayed. */ - const val LEVEL = 3 + private const val LEVEL = BuildConfig.LOG_LEVEL /** * Error level. */ - const val ERROR = 0 + private const val ERROR = 0 /** * Warning level. */ - const val WARN = 1 + private const val WARN = 1 /** * Information level. */ - const val INFO = 2 + private const val INFO = 2 /** * Debug level. */ - const val DEBUG = 3 + private const val DEBUG = 3 + + /** + * Trace level. For extremely detailed and high volume debug logs. + */ + private const val TRACE = 4 + + /** + * Print stack trace on error log. + */ + private const val PRINT_STACK_TRACE = BuildConfig.PRINT_STACK_TRACE + + /** + * Context of Sentry error reporting software for adding info. + */ + private val sentryContext: Context + + /** + * Username used for error reporting. + */ + var username: String? = null + set(value) { + sentryContext.user = UserBuilder().setUsername(value).build() + Analytics.username = value ?: "" + } + + var uuid: String? = null + set(value) { + Analytics.uuid = value ?: "" + } + + init { + Sentry.init(BuildConfig.SENTRY_DSN) + sentryContext = Sentry.getContext() + addTags() + } /** * Log error message with exception info. + * Don't log private information with this method. * - * @property message the message for user and logs. * @property e the exception if presented. - * @property code the code of error if exception is not presented. + * @property message the message for user and logs. + * @property logOnly only log to console, no additional actions. */ - fun error(message: String, e: Throwable? = null, code: String = "", - logOnly: Boolean = false) { + fun error(e: Throwable, message: String = "", logOnly: Boolean = false) { + val finalMessage = if (message.isNotBlank()) { message + ": " } + else { "" } + e.message if (LEVEL >= ERROR) { - println("[e] $message" + if (e != null) ": $e" else "") + println("[e] $finalMessage") + if (PRINT_STACK_TRACE) { + e.printStackTrace() + } } if (!logOnly) { - Analytics.trackError(e = e, code = code) - //TODO(anatoly): Add error tracking software. + Analytics.trackError(e) + Sentry.capture(e) } + addBreadcrumb(finalMessage, Breadcrumb.Level.ERROR) } /** - * Log warning message. + * Log warning message. Don't log private information with this method. */ fun warn(message: String) { if (LEVEL >= WARN) { println("[w] $message.") } + addBreadcrumb(message, Breadcrumb.Level.WARNING) } /** - * Log information message. + * Log information message. Don't log private information with this method. */ - fun info(message: String) { + fun info(message: String, event: String = "") { if (LEVEL >= INFO) { println("[i] $message.") } + if (event.isNotBlank()) { + Analytics.trackEvent(event) + } + addBreadcrumb(message, Breadcrumb.Level.INFO) } /** @@ -76,4 +140,38 @@ object Logger { println("[d] $message.") } } + + /** + * Log trace message. + */ + fun trace(message: String) { + if (LEVEL >= TRACE) { + println("[t] $message.") + } + } + + private fun addBreadcrumb(message: String, level: Breadcrumb.Level) { + sentryContext.recordBreadcrumb(BreadcrumbBuilder() + .setMessage(message) + .setLevel(level) + .setTimestamp(Date()) + .build()) + } + + private fun addTags() { + val default = "unavailable" + val osName = System.getProperty("os.name", default) + val osVersion = System.getProperty("os.version", default) + val javaVendor = System.getProperty("java.vendor", default) + val javaVersion = System.getProperty("java.version", default) + + sentryContext.addTag("environment", BuildConfig.ENVIRONMENT) + sentryContext.addTag("version", BuildConfig.VERSION) + sentryContext.addTag("version-code", BuildConfig.VERSION_CODE + .toString()) + sentryContext.addTag("os-name", osName) + sentryContext.addTag("os-version", osVersion) + sentryContext.addTag("java-vendor", javaVendor) + sentryContext.addTag("java-version", javaVersion) + } } diff --git a/src/main/kotlin/app/Main.kt b/src/main/kotlin/app/Main.kt index 72be7309..3b2ee4d3 100644 --- a/src/main/kotlin/app/Main.kt +++ b/src/main/kotlin/app/Main.kt @@ -19,8 +19,8 @@ import com.beust.jcommander.JCommander import com.beust.jcommander.MissingCommandException fun main(argv : Array) { - Thread.setDefaultUncaughtExceptionHandler { _, e: Throwable? -> - Logger.error("Uncaught exception", e) + Thread.setDefaultUncaughtExceptionHandler { _, e: Throwable -> + Logger.error(e, "Uncaught exception") } Main(argv) } @@ -30,8 +30,8 @@ class Main(argv: Array) { private val api = ServerApi(configurator) init { - Analytics.uuid = configurator.getUuidPersistent() - Analytics.trackStart() + Logger.uuid = configurator.getUuidPersistent() + Logger.info("App started", Logger.Events.START) val options = Options() val commandAdd = CommandAdd() @@ -64,13 +64,10 @@ class Main(argv: Array) { else -> startUi() } } catch (e: MissingCommandException) { - Logger.error( - message = "No such command: ${e.unknownCommand}", - code = "no-command" - ) + Logger.warn("No such command: ${e.unknownCommand}") } - Analytics.trackExit() + Logger.info("App finished", Logger.Events.EXIT) } private fun startUi() { @@ -86,10 +83,9 @@ class Main(argv: Array) { configurator.saveToFile() println("Added git repository at $path.") - Analytics.trackConfigChanged() + Logger.info("Config changed", Logger.Events.CONFIG_CHANGED) } else { - Logger.error(message = "No valid git repository found at $path.", - code = "repo-invalid") + Logger.warn("No valid git repository found at specified path") } } @@ -97,8 +93,7 @@ class Main(argv: Array) { val (key, value) = commandOptions.pair if (!arrayListOf("username", "password").contains(key)) { - Logger.error(message = "No such key $key", - code = "invalid-params") + Logger.warn("No such key $key") return } @@ -109,7 +104,7 @@ class Main(argv: Array) { configurator.saveToFile() - Analytics.trackConfigChanged() + Logger.info("Config changed", Logger.Events.CONFIG_CHANGED) } private fun doList() { @@ -126,7 +121,7 @@ class Main(argv: Array) { configurator.saveToFile() println("Repository removed from tracking list.") - Analytics.trackConfigChanged() + Logger.info("Config changed", Logger.Events.CONFIG_CHANGED) } else { println("Repository not found in tracking list.") } diff --git a/src/main/kotlin/app/api/ServerApi.kt b/src/main/kotlin/app/api/ServerApi.kt index 65ab05a0..0002ba4d 100644 --- a/src/main/kotlin/app/api/ServerApi.kt +++ b/src/main/kotlin/app/api/ServerApi.kt @@ -118,14 +118,11 @@ class ServerApi (private val configurator: Configurator) : Api { Logger.debug("Request $requestName success") return parser(res.data) } else { - Logger.error("Request $requestName error", e) throw RequestException(e) } } catch (e: InvalidProtocolBufferException) { - Logger.error("Request $requestName error while parsing", e) throw RequestException(e) } catch (e: InvalidParameterException) { - Logger.error("Request $requestName error while parsing", e) throw RequestException(e) } } diff --git a/src/main/kotlin/app/config/FileConfigurator.kt b/src/main/kotlin/app/config/FileConfigurator.kt index 117e9b22..f1990d8d 100644 --- a/src/main/kotlin/app/config/FileConfigurator.kt +++ b/src/main/kotlin/app/config/FileConfigurator.kt @@ -16,7 +16,6 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.dataformat.yaml.YAMLFactory import com.fasterxml.jackson.module.kotlin.KotlinModule import java.io.IOException -import java.lang.IllegalStateException import java.nio.file.Files import java.nio.file.InvalidPathException import java.nio.file.NoSuchFileException @@ -72,7 +71,7 @@ class FileConfigurator : Configurator { System.getProperty("user.home") } catch (e: SecurityException) { - Logger.error("Cannot access user directory", e) + Logger.error(e, "Cannot access user directory") null } @@ -232,21 +231,21 @@ class FileConfigurator : Configurator { mapper.readValue(it, Config::class.java) } } catch (e: IOException) { - if(e is NoSuchFileException){ - Logger.info("No config file found") + if (e is NoSuchFileException){ + Logger.warn("No config file found") } else { - Logger.error("Cannot access config file", e) + Logger.error(e, "Cannot access config file") } } catch (e: SecurityException) { - Logger.error("Cannot access config file", e) + Logger.error(e, "Cannot access config file") } catch (e: InvalidPathException) { - Logger.error("Cannot access config file", e) + Logger.error(e, "Cannot access config file") } catch (e: JsonParseException) { - Logger.error("Cannot parse config file", e) + Logger.error(e, "Cannot parse config file") } catch (e: JsonMappingException) { - Logger.error("Cannot parse config file", e) + Logger.error(e, "Cannot parse config file") } catch (e: IllegalStateException) { - Logger.error("Cannot parse config file", e) + Logger.error(e, "Cannot parse config file") } persistent = loadConfig @@ -261,17 +260,17 @@ class FileConfigurator : Configurator { mapper.writeValue(it, persistent) } } catch (e: IOException) { - Logger.error("Cannot save config file", e) + Logger.error(e, "Cannot save config file") } catch (e: SecurityException) { - Logger.error("Cannot save config file", e) + Logger.error(e, "Cannot save config file") } catch (e: InvalidPathException) { - Logger.error("Cannot save config file", e) + Logger.error(e, "Cannot save config file") } catch (e: JsonParseException) { - Logger.error("Cannot parse config file", e) + Logger.error(e, "Cannot parse config file") } catch (e: JsonMappingException) { - Logger.error("Cannot parse config file", e) + Logger.error(e, "Cannot parse config file") } catch (e: IllegalStateException) { - Logger.error("Cannot parse config file", e) + Logger.error(e, "Cannot parse config file") } } diff --git a/src/main/kotlin/app/hashers/CodeLongevity.kt b/src/main/kotlin/app/hashers/CodeLongevity.kt index 6553dab8..3c117257 100644 --- a/src/main/kotlin/app/hashers/CodeLongevity.kt +++ b/src/main/kotlin/app/hashers/CodeLongevity.kt @@ -207,7 +207,7 @@ class CodeLongevity(private val serverRepo: Repo, if (stats.size > 0) { api.postFacts(stats) - Logger.debug("Sent ${stats.size} stats to server") + Logger.info("Sent ${stats.size} facts to server") } } @@ -227,7 +227,7 @@ class CodeLongevity(private val serverRepo: Repo, // Update ages. getLinesObservable(storedHead).blockingSubscribe { line -> - Logger.debug("Scanning: ${line}") + Logger.trace("Scanning: ${line}") if (line.to.isDeleted) { var age = line.age if (ageData.lastingLines.contains(line.oldId)) { @@ -313,7 +313,7 @@ class CodeLongevity(private val serverRepo: Repo, val oldId = diff.getOldId().toObjectId() val newPath = diff.getNewPath() val newId = diff.getNewId().toObjectId() - Logger.debug("old: '$oldPath', new: '$newPath'") + Logger.trace("old: '$oldPath', new: '$newPath'") // Skip binary files. val fileId = if (newPath != DiffEntry.DEV_NULL) newId else oldId @@ -351,14 +351,14 @@ class CodeLongevity(private val serverRepo: Repo, if (insCount > 0) { val insStart = edit.getBeginB() val insEnd = edit.getEndB() - Logger.debug("ins ($insStart, $insEnd)") + Logger.trace("ins ($insStart, $insEnd)") for (idx in insStart .. insEnd - 1) { val from = RevCommitLine(commit, newId, newPath, idx, false) val to = lines.get(idx) val cl = CodeLine(repo, from, to) - Logger.debug("Collected: ${cl}") + Logger.trace("Collected: ${cl}") subscriber.onNext(cl) } lines.subList(insStart, insEnd).clear() @@ -373,7 +373,7 @@ class CodeLongevity(private val serverRepo: Repo, if (delCount > 0) { val delStart = edit.getBeginA() val delEnd = edit.getEndA() - Logger.debug("del ($delStart, $delEnd)") + Logger.trace("del ($delStart, $delEnd)") val tmpLines = ArrayList(delCount) for (idx in delStart .. delEnd - 1) { @@ -409,7 +409,7 @@ class CodeLongevity(private val serverRepo: Repo, val from = RevCommitLine(tail, fileId, filePath, idx, false) val cl = CodeLine(repo, from, lines[idx]) - Logger.debug("Collected (tail): $cl") + Logger.trace("Collected (tail): $cl") subscriber.onNext(cl) } } diff --git a/src/main/kotlin/app/hashers/CommitCrawler.kt b/src/main/kotlin/app/hashers/CommitCrawler.kt index db738c67..2b781d53 100644 --- a/src/main/kotlin/app/hashers/CommitCrawler.kt +++ b/src/main/kotlin/app/hashers/CommitCrawler.kt @@ -35,7 +35,7 @@ object CommitCrawler { // to calculate the diff of the initial commit. subscriber.onNext(Commit()) } catch (e: Exception) { - Logger.error("Commit producing error", e) + Logger.error(e, "Commit producing error") subscriber.onError(e) } subscriber.onComplete() diff --git a/src/main/kotlin/app/hashers/CommitHasher.kt b/src/main/kotlin/app/hashers/CommitHasher.kt index a478c069..90b1c279 100644 --- a/src/main/kotlin/app/hashers/CommitHasher.kt +++ b/src/main/kotlin/app/hashers/CommitHasher.kt @@ -46,7 +46,7 @@ class CommitHasher(private val serverRepo: Repo = Repo(), .map { commit -> // Mapping and stats extraction. commit.stats = Extractor().extract(commit.diffs) - Logger.debug("Stats: ${commit.stats.size} entries") + Logger.info("Stats: ${commit.stats.size} entries") // Count lines on all non-binary files. This is additional // statistics to CommitStats because not all file extensions @@ -77,14 +77,14 @@ class CommitHasher(private val serverRepo: Repo = Repo(), private fun postCommitsToServer(commits: List) { if (commits.isNotEmpty()) { api.postCommits(commits) - Logger.debug("Sent ${commits.size} added commits to server") + Logger.info("Sent ${commits.size} added commits to server") } } private fun deleteCommitsOnServer(commits: List) { if (commits.isNotEmpty()) { api.deleteCommits(commits) - Logger.debug("Sent ${commits.size} deleted commits to server") + Logger.info("Sent ${commits.size} deleted commits to server") } } } diff --git a/src/main/kotlin/app/hashers/FactHasher.kt b/src/main/kotlin/app/hashers/FactHasher.kt index 430bb66d..5234f77e 100644 --- a/src/main/kotlin/app/hashers/FactHasher.kt +++ b/src/main/kotlin/app/hashers/FactHasher.kt @@ -102,7 +102,7 @@ class FactHasher(private val serverRepo: Repo = Repo(), private fun postFactsToServer(facts: List) { if (facts.isNotEmpty()) { api.postFacts(facts) - Logger.debug("Sent ${facts.size} facts to server") + Logger.info("Sent ${facts.size} facts to server") } } } diff --git a/src/main/kotlin/app/hashers/RepoHasher.kt b/src/main/kotlin/app/hashers/RepoHasher.kt index e21bf94f..9585f30d 100644 --- a/src/main/kotlin/app/hashers/RepoHasher.kt +++ b/src/main/kotlin/app/hashers/RepoHasher.kt @@ -3,7 +3,6 @@ package app.hashers -import app.Analytics import app.Logger import app.api.Api import app.config.Configurator @@ -32,6 +31,7 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, fun update() { println("Hashing $localRepo...") + Logger.info("Hashing of repo started") val git = loadGit(localRepo.path) try { val (rehashes, emails) = fetchRehashesAndEmails(git) @@ -57,12 +57,12 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, val errors = mutableListOf() val onError: (Throwable) -> Unit = { e -> errors.add(e) - Logger.error("Hashing error", e) + Logger.error(e, "Hashing error") } // Hash by all plugins. val observable = CommitCrawler.getObservable(git, serverRepo) - .publish() + .publish() CommitHasher(serverRepo, api, rehashes, filteredEmails) .updateFromObservable(observable, onError) FactHasher(serverRepo, api, filteredEmails) @@ -87,7 +87,8 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, } println("Hashing $localRepo successfully finished.") - Analytics.trackHashingRepoSuccess() + Logger.info("Hashing repo succesfully", + Logger.Events.HASHING_REPO_SUCCES) } finally { closeGit(git) @@ -115,7 +116,7 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, private fun getRepoFromServer() { val repo = api.getRepo(serverRepo.rehash) serverRepo.commits = repo.commits - Logger.debug("Received repo from server with " + + Logger.info("Received repo from server with " + serverRepo.commits.size + " commits") Logger.debug(serverRepo.toString()) } diff --git a/src/main/kotlin/app/ui/AddRepoState.kt b/src/main/kotlin/app/ui/AddRepoState.kt index e36156db..0e469577 100644 --- a/src/main/kotlin/app/ui/AddRepoState.kt +++ b/src/main/kotlin/app/ui/AddRepoState.kt @@ -4,6 +4,7 @@ package app.ui import app.Analytics +import app.Logger import app.api.Api import app.config.Configurator import app.model.LocalRepo @@ -46,7 +47,7 @@ class AddRepoState constructor(private val context: Context, } } - Analytics.trackConfigSetup() + Logger.info("Config setup", Logger.Events.CONFIG_SETUP) } override fun next() { diff --git a/src/main/kotlin/app/ui/AuthState.kt b/src/main/kotlin/app/ui/AuthState.kt index 627b63e1..ff802b2f 100644 --- a/src/main/kotlin/app/ui/AuthState.kt +++ b/src/main/kotlin/app/ui/AuthState.kt @@ -5,6 +5,7 @@ package app.ui import app.Analytics import app.BuildConfig +import app.Logger import app.api.Api import app.config.Configurator import app.utils.PasswordHelper @@ -76,8 +77,8 @@ class AuthState constructor(private val context: Context, + BuildConfig.PROFILE_URL + configurator.getUsername()) saveCredentialsIfChanged() - Analytics.username = configurator.getUsername() - Analytics.trackAuth() + Logger.username = configurator.getUsername() + Logger.info("Auth success", Logger.Events.AUTH) return true } catch (e: RequestException) { diff --git a/src/main/kotlin/app/ui/UpdateRepoState.kt b/src/main/kotlin/app/ui/UpdateRepoState.kt index 925a4c23..43c7fc0f 100644 --- a/src/main/kotlin/app/ui/UpdateRepoState.kt +++ b/src/main/kotlin/app/ui/UpdateRepoState.kt @@ -3,13 +3,11 @@ package app.ui -import app.Analytics import app.hashers.RepoHasher import app.Logger import app.api.Api import app.config.Configurator import app.utils.HashingException -import app.utils.RequestException /** * Update repositories console UI state. @@ -20,22 +18,23 @@ class UpdateRepoState constructor(private val context: Context, : ConsoleState { override fun doAction() { println("Hashing your git repositories.") + Logger.info("Hashing started") + for (repo in configurator.getLocalRepos()) { try { RepoHasher(repo, api, configurator).update() } catch (e: HashingException) { - Logger.error("During hashing ${e.errors.size} errors occurred:") e.errors.forEach { error -> - Logger.error("", error) + Logger.error(error, "Error while hashing") } } catch (e: Exception) { - Logger.error("Error while hashing $repo", e) + Logger.error(e, "Error while hashing") } } + println("The repositories have been hashed. See result online on your " + "Sourcerer profile.") - - Analytics.trackHashingSuccess() + Logger.info("Hashing success", Logger.Events.HASHING_SUCCESS) } override fun next() { diff --git a/src/main/kotlin/app/utils/RepoHelper.kt b/src/main/kotlin/app/utils/RepoHelper.kt index 2cb5f3de..f04651ac 100644 --- a/src/main/kotlin/app/utils/RepoHelper.kt +++ b/src/main/kotlin/app/utils/RepoHelper.kt @@ -32,7 +32,7 @@ object RepoHelper { repository = git.repository commitId = repository.resolve(MASTER_BRANCH) } catch (e: Exception) { - Logger.error("Cannot access repository at path $path", e) + Logger.error(e, "Cannot access repository at specified path") return false } finally { repository?.close() @@ -49,13 +49,13 @@ object RepoHelper { return try { Paths.get(path).toFile().isDirectory } catch (e: InvalidPathException) { - Logger.error("Invalid path $path", e) + Logger.error(e, "Invalid path") false } catch (e: UnsupportedOperationException) { - Logger.error("Invalid path $path", e) + Logger.error(e, "Invalid path") false } catch (e: SecurityException) { - Logger.error("Cannot access repository at path $path", e) + Logger.error(e, "Cannot access repository at specified path") false } } From 1d10bea503d693ed5c33df730b6326d722e11a91 Mon Sep 17 00:00:00 2001 From: Anatoly Kislov Date: Mon, 23 Oct 2017 14:44:14 +0300 Subject: [PATCH 2/3] chore: add rehashes loggging to RepoHasher --- src/main/kotlin/app/hashers/RepoHasher.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/kotlin/app/hashers/RepoHasher.kt b/src/main/kotlin/app/hashers/RepoHasher.kt index 9585f30d..ef3d1394 100644 --- a/src/main/kotlin/app/hashers/RepoHasher.kt +++ b/src/main/kotlin/app/hashers/RepoHasher.kt @@ -44,6 +44,8 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, val filteredEmails = filterEmails(emails) initServerRepo(rehashes.last) + Logger.debug("Local repo path: ${localRepo.path}") + Logger.debug("Repo rehash: ${serverRepo.rehash}") if (!isKnownRepo()) { // Notify server about new contributor and his email. From ea31f9fa81a12cdb9d836caf8347d27f5fb360d7 Mon Sep 17 00:00:00 2001 From: Anatoly Kislov Date: Mon, 23 Oct 2017 15:11:17 +0300 Subject: [PATCH 3/3] chore: fix const name --- src/main/kotlin/app/Logger.kt | 2 +- src/main/kotlin/app/hashers/RepoHasher.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/app/Logger.kt b/src/main/kotlin/app/Logger.kt index a1e7e8ed..3771097d 100644 --- a/src/main/kotlin/app/Logger.kt +++ b/src/main/kotlin/app/Logger.kt @@ -20,7 +20,7 @@ object Logger { val AUTH = "auth" val CONFIG_SETUP = "config/setup" val CONFIG_CHANGED = "config/changed" - val HASHING_REPO_SUCCES = "hashing/repo/success" + val HASHING_REPO_SUCCESS = "hashing/repo/success" val HASHING_SUCCESS = "hashing/success" val EXIT = "exit" } diff --git a/src/main/kotlin/app/hashers/RepoHasher.kt b/src/main/kotlin/app/hashers/RepoHasher.kt index ef3d1394..2f77a262 100644 --- a/src/main/kotlin/app/hashers/RepoHasher.kt +++ b/src/main/kotlin/app/hashers/RepoHasher.kt @@ -90,7 +90,7 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, println("Hashing $localRepo successfully finished.") Logger.info("Hashing repo succesfully", - Logger.Events.HASHING_REPO_SUCCES) + Logger.Events.HASHING_REPO_SUCCESS) } finally { closeGit(git)