Creating message for message log now directly in fints4k, no need to translate it

This commit is contained in:
dankito 2021-11-13 02:22:56 +01:00
parent 6aa03bcad2
commit 462917467d
5 changed files with 24 additions and 30 deletions

View File

@ -20,6 +20,8 @@ open class MessageLogCollector {
const val MaxCountStackTraceElements = 15 const val MaxCountStackTraceElements = 15
private const val NewLine = "\r\n"
private val log = LoggerFactory.getLogger(MessageLogCollector::class) private val log = LoggerFactory.getLogger(MessageLogCollector::class)
} }
@ -28,39 +30,42 @@ open class MessageLogCollector {
// in either case remove sensitive data after response is parsed as otherwise some information like account holder name and accounts may is not set yet on BankData // in either case remove sensitive data after response is parsed as otherwise some information like account holder name and accounts may is not set yet on BankData
open val messageLogWithoutSensitiveData: List<MessageLogEntry> open val messageLogWithoutSensitiveData: List<MessageLogEntry>
get() = messageLog.map { MessageLogEntry(it.type, safelyRemoveSensitiveDataFromMessage(it.message, it.context.bank), it.time, it.context) } get() = messageLog.map { MessageLogEntry(it.type, safelyRemoveSensitiveDataFromMessage(it.message, it.context.bank), it.context, it.time) }
protected open val stackTraceHelper = StackTraceHelper() protected open val stackTraceHelper = StackTraceHelper()
open fun addMessageLog(type: MessageLogEntryType, message: String, context: MessageContext) { open fun addMessageLog(type: MessageLogEntryType, message: String, context: MessageContext) {
val timeStamp = Date() val messageToLog = createMessage(type, prettyPrintHbciMessage(message), context, true)
val prettyPrintMessage = prettyPrintHbciMessage(message)
messageLog.add(MessageLogEntry(type, prettyPrintMessage, timeStamp, context)) messageLog.add(MessageLogEntry(type, messageToLog, context))
val messageTrace = createMessageTraceString(type, context) log.debug { messageToLog }
log.debug { "$messageTrace\r\n$prettyPrintMessage" }
} }
open fun logError(loggingClass: KClass<*>, message: String, context: MessageContext, e: Exception? = null) { open fun logError(loggingClass: KClass<*>, message: String, context: MessageContext, e: Exception? = null) {
val type = MessageLogEntryType.Error val type = MessageLogEntryType.Error
val messageTrace = createMessageTraceString(type, context) + " " val messageToLog = createMessage(type, message, context, false)
if (e != null) { if (e != null) {
getLogger(loggingClass).error(e) { messageTrace + message } getLogger(loggingClass).error(e) { messageToLog }
} else { } else {
getLogger(loggingClass).error(messageTrace + message) getLogger(loggingClass).error(messageToLog)
} }
val errorStackTrace = if (e != null) "\r\n" + getStackTrace(e) else "" val errorStackTrace = if (e != null) NewLine + getStackTrace(e) else ""
messageLog.add(MessageLogEntry(type, message + errorStackTrace, Date(), context)) messageLog.add(MessageLogEntry(type, messageToLog + errorStackTrace, context))
} }
protected open fun createMessage(type: MessageLogEntryType, message: String, context: MessageContext, separateWithNewLine: Boolean): String {
val messageTrace = createMessageTraceString(type, context)
return "$messageTrace${ if (separateWithNewLine) NewLine else " " }$message"
}
protected open fun createMessageTraceString(type: MessageLogEntryType, context: MessageContext): String { protected open fun createMessageTraceString(type: MessageLogEntryType, context: MessageContext): String {
return "${twoDigits(context.jobNumber)}_${twoDigits(context.dialogNumber)}_${twoDigits(context.messageNumber)}_" + return "${twoDigits(context.jobNumber)}_${twoDigits(context.dialogNumber)}_${twoDigits(context.messageNumber)}_" +
"${context.bank.bankCode}_${context.bank.customerId}" + "${context.bank.bankCode}_${context.bank.customerId}" +
@ -82,7 +87,7 @@ open class MessageLogCollector {
} }
protected open fun prettyPrintHbciMessage(message: String): String { protected open fun prettyPrintHbciMessage(message: String): String {
return message.replace("'", "'\r\n") return message.replace("'", "'$NewLine")
} }
@ -90,7 +95,7 @@ open class MessageLogCollector {
try { try {
return removeSensitiveDataFromMessage(message, bank) return removeSensitiveDataFromMessage(message, bank)
} catch (e: Exception) { } catch (e: Exception) {
return "! WARNING !\r\nCould not remove sensitive data!\r\n$e\r\n${getStackTrace(e)}\r\n$message" return "! WARNING !${NewLine}Could not remove sensitive data!$NewLine$e$NewLine${getStackTrace(e)}$NewLine$message"
} }
} }

View File

@ -7,8 +7,8 @@ import net.dankito.utils.multiplatform.Date
open class MessageLogEntry( open class MessageLogEntry(
open val type: MessageLogEntryType, open val type: MessageLogEntryType,
open val message: String, open val message: String,
open val time: Date, open val context: MessageContext,
open val context: MessageContext open val time: Date = Date()
) { ) {
override fun toString(): String { override fun toString(): String {

View File

@ -7,8 +7,6 @@ open class MessageLogEntry(
open val message: String, open val message: String,
open val type: MessageLogEntryType, open val type: MessageLogEntryType,
open val time: Date, open val time: Date,
open val jobNumber: Int,
open val dialogNumber: Int,
open val bank: TypedBankData, open val bank: TypedBankData,
open val account: TypedBankAccount? open val account: TypedBankAccount?
) { ) {

View File

@ -824,16 +824,7 @@ open class BankingPresenter(
}) })
return logEntries.map { entry -> return logEntries.map { entry ->
MessageLogEntryDateFormatter.format(entry.time) + " " + entry.bank.bankCode + " " + getMessageLogPrefix(entry) + "\r\n" + entry.message MessageLogEntryDateFormatter.format(entry.time) + " " + entry.message
}
}
protected open fun getMessageLogPrefix(entry: MessageLogEntry): String {
// TODO: translate
return when (entry.type) {
MessageLogEntryType.Sent -> "Sending message"
MessageLogEntryType.Received -> "Received message"
MessageLogEntryType.Error -> "Error"
} }
} }

View File

@ -215,8 +215,8 @@ open class fints4kBankingClient(
protected open fun saveData(response: FinTsClientResponse) { protected open fun saveData(response: FinTsClientResponse) {
try { try {
_messageLogWithoutSensitiveData.addAll(response.messageLogWithoutSensitiveData _messageLogWithoutSensitiveData.addAll(response.messageLogWithoutSensitiveData
.map { MessageLogEntry(it.message, map(it.type), it.time, it.context.jobNumber, .map { MessageLogEntry(it.message, map(it.type), it.time, bank,
it.context.dialogNumber, bank, it.context.account?.let { mapper.findMatchingAccount(bank, it) } ) }) it.context.account?.let { mapper.findMatchingAccount(bank, it) } ) })
// TODO: fix that real (child) class get serialized and re-enable again // TODO: fix that real (child) class get serialized and re-enable again
// val clientDataFile = getFints4kClientDataFile(fintsBank.bankCode, fintsBank.customerId) // val clientDataFile = getFints4kClientDataFile(fintsBank.bankCode, fintsBank.customerId)