Commit 9309eada authored by zhaopengxiang's avatar zhaopengxiang

添加请求日志

parent 623672f3
package com.miya.fastcashier
import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import androidx.multidex.MultiDexApplication
import com.blankj.utilcode.util.LogUtils
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.LogUtils
import com.elvishew.xlog.XLog
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.ConsolePrinter
......@@ -18,13 +14,14 @@ import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.backup.NeverBackupStrategy
import com.elvishew.xlog.printer.file.naming.DateFileNameGenerator
import com.miya.fastcashier.service.AccountService
import com.miya.fastcashier.ui.LoginActivity
import com.miya.fastcashier.ui.dialog.CommonDialog
import com.miya.fastcashier.utils.ContextUtils
import com.miya.fastcashier.utils.DateUtils
import com.miya.fastcashier.utils.DensityUtils
import com.miya.fastcashier.utils.LogFileUtils
import com.miya.fastcashier.utils.manage.OrderRecordManageKit
import com.miya.print.PrinterManager
import com.sdy.miya.moblie.component.pay.core.net.MiYaPayMobileApiClient
import com.sdy.miya.moblie.component.pay.core.net.MiyaHttpLoggingInterceptor
import com.sdy.miya.moblie.component.pay.core.utils.PayLogFileUtils
import com.tencent.mmkv.MMKV
import java.io.File
......@@ -91,6 +88,26 @@ class BaseApplication : MultiDexApplication() {
.absolutePath
+ File.separator + "payLog"
)
var loggingInterceptor: MiyaHttpLoggingInterceptor? = null
try {
loggingInterceptor = MiyaHttpLoggingInterceptor { message ->
if (!LogFileUtils.isProhibitWrite) {
LogFileUtils.writeLog(
ContextUtils.getContext(),
DateUtils.getDateStringByTimeStamp(System.currentTimeMillis())
.toString() + " " + message + "\n"
)
}
if (BuildConfig.DEBUG) {
LogUtils.d("fastcashier", message)
}
}
loggingInterceptor.level = MiyaHttpLoggingInterceptor.Level.BODY
} catch (ex: Exception) {
ex.printStackTrace()
}
MiYaPayMobileApiClient.init(true, loggingInterceptor)
}
private fun screenAdapt() {
......
......@@ -73,6 +73,10 @@ class ResetAuthorizePasswordActivity : BaseActivity() {
return
}
if (newPassword.equals(originPassword)) {
CenterToasty.error(this, "新密码与原密码不能相同!").show()
return
}
if (!newPassword.equals(newConfirmPassword)) {
CenterToasty.error(this, "新密码两次输入不一致!").show()
......
package com.miya.fastcashier.utils
import android.content.Context
import android.content.pm.PackageInfo
import com.miya.fastcashier.utils.LogFileUtils
import android.content.pm.PackageManager
import com.miya.fastcashier.utils.LogFileUtils.DeleteLogTask
import android.os.AsyncTask
import android.os.Environment
import android.util.Log
import java.io.*
import java.lang.Exception
import java.nio.charset.Charset
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.Executors
object LogFileUtils {
private var logFiles //日志文件夹
: File? = null
private var logFile //单个日志文件
: File? = null
private val sExecutor = Executors.newSingleThreadExecutor()
val CACHE_FILE_PATH = Environment
.getExternalStorageDirectory().path +
File.separator +
"miyaterminal" +
File.separator +
"log" +
File.separator
var isProhibitWrite = false //是否禁止写入文件
private var allFileBeanList //所有的文件列表
: MutableList<FileBean>? = null
private var deleteFilePathList //需要删除的文件列表
: MutableList<File>? = null
private const val logSuffix = ".txt"
private const val logLink = "_"
fun writeLog(c: Context, input: String): Boolean {
sExecutor.execute { writeLogToFile(c, input) }
return false
}
private fun writeLogToFile(context: Context, input: String) {
var fos: FileOutputStream? = null
var writer: OutputStreamWriter? = null
try {
logFiles = File(CACHE_FILE_PATH)
if (!logFiles!!.exists()) {
logFiles!!.mkdirs()
}
val logPath = (CACHE_FILE_PATH + getVersionCode(context)
+ "_" + DateUtils.format8(Date()) + ".txt")
logFile = File(logPath)
fos = FileOutputStream(logFile, true)
writer = OutputStreamWriter(fos, Charset.forName("utf-8"))
writer.write(input)
writer.flush()
} catch (e: IOException) {
e.printStackTrace()
} finally {
try {
writer?.close()
} catch (e: IOException) {
e.printStackTrace()
}
try {
fos?.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
private fun getVersionCode(context: Context): String {
val packageManager = context.packageManager
val packageInfo: PackageInfo
var versionCode = ""
try {
packageInfo = packageManager.getPackageInfo(context.packageName, 0)
versionCode = packageInfo.versionCode.toString() + ""
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
return versionCode
}
/**
* 自动拼装时间戳的日志
*/
fun writeLogFormat(c: Context, input: String): Boolean {
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
return writeLog(
c, """${sdf.format(calendar.time)} $input
"""
)
}
/**
* 写入throwable
*/
fun writeThrowable(context: Context, throwable: Throwable?): Boolean {
return try {
if (throwable == null) {
return false
}
val stringWriter = StringWriter()
throwable.printStackTrace(PrintWriter(stringWriter))
writeLogFormat(context, stringWriter.toString())
} catch (e: Exception) {
writeLogFormat(
context,
"error occur when writing throwable , error content is:" + e.message
)
false
}
}
/**
* 删除指定天数之前的日志,此处的天数不是精确到小时的,只精确到日
*
* @param days
*/
fun deleteLog(days: Int) {
files
if (deleteFilePathList == null) {
deleteFilePathList = ArrayList()
} else {
deleteFilePathList!!.clear()
}
if (allFileBeanList != null && allFileBeanList!!.size > 0) {
for (i in allFileBeanList!!.indices) {
getDeleteFileList(days, allFileBeanList!![i])
}
}
if (deleteFilePathList != null && deleteFilePathList!!.size > 0) {
val deleteFiles = deleteFilePathList!!.toTypedArray()
DeleteLogTask().execute(*deleteFiles)
}
}
/**
* 执行符合条件的删除日志操作
*
* @param days
* @param fileBean
*/
private fun getDeleteFileList(days: Int, fileBean: FileBean) {
val currentLongTimes = System.currentTimeMillis()
if (fileBean.fileName!!.endsWith(logSuffix)) {
if (fileBean.fileName!!.contains(logLink)) {
try {
val fileNameWithOutSuffix =
fileBean.fileName!!.substring(0, fileBean.fileName!!.indexOf("."))
val fileNameDate = fileNameWithOutSuffix.substring(
fileNameWithOutSuffix.lastIndexOf(
logLink
) + 1
)
val fileNameLongTimes =
DateUtils.stringToLong(fileNameDate, DateUtils.DF_YYYYMMDD)
if (currentLongTimes - fileNameLongTimes >= days * 24L * 60L * 60L * 1000L) { //符合要求,加入删除列表
deleteFilePathList!!.add(File(fileBean.filePath))
}
} catch (e: ParseException) {
e.printStackTrace()
}
}
}
}
/**
* 获得日志文件夹下的文件名和文件路径
*/
private val files: Unit
private get() {
if (allFileBeanList == null) {
allFileBeanList = ArrayList()
} else {
allFileBeanList!!.clear()
}
val files = logFiles!!.listFiles()
if (files != null) {
for (i in files.indices) {
val fileBean = FileBean()
fileBean.fileName = files[i].name
fileBean.filePath = files[i].absolutePath
allFileBeanList!!.add(fileBean)
}
}
}
private fun doDeleteFiles(file: File) {
//flie:要删除的文件夹的所在位置
if (file.isDirectory) {
val files = file.listFiles()
for (i in files.indices) {
val f = files[i]
doDeleteFiles(f)
}
file.delete() //如要保留文件夹,只删除文件,请注释这行
} else if (file.exists()) {
file.delete()
}
}
private class DeleteLogTask : AsyncTask<File, Void?, Boolean>() {
override fun doInBackground(vararg files: File): Boolean {
for (file in files) {
doDeleteFiles(file)
}
return true
}
override fun onPostExecute(aBoolean: Boolean) {
super.onPostExecute(aBoolean)
if (aBoolean) {
Log.i("LogFileUtils", "日志删除成功!")
}
}
}
private class FileBean {
var fileName: String? = null
var filePath: String? = null
}
}
\ No newline at end of file
package com.miya.fastcashier.utils.manage
import android.util.Log
import com.blankj.utilcode.util.LogUtils
import com.miya.fastcashier.utils.DateUtils
import com.tencent.mmkv.MMKV
......@@ -21,7 +20,7 @@ class OrderRecordManageKit {
val today = DateUtils.format8(Date())
val no = getOrderMMKV().getInt(today, 0).toString()
var listNo = no
//真是流水单号需满足五位
//真实流水单号五位
for (index in 1..(5 - no.length)) {
listNo = "0$listNo"
}
......
......@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_E5E5E5"
android:background="@color/color_F8F8F8"
tools:context=".ui.SettingActivity">
<View
......
......@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_E5E5E5"
android:background="@color/color_F8F8F8"
tools:context=".ui.SettingActivity">
<View
......
......@@ -16,8 +16,9 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#323233"
android:textColor="@color/color_323233"
android:textSize="28sp"
android:textStyle="bold"
tools:text="是否确认退款"
android:layout_marginTop="61dp"
android:layout_width="wrap_content"
......
......@@ -14,7 +14,7 @@
android:id="@+id/cpEmptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_E5E5E5"
android:background="@color/color_F8F8F8"
app:layout_constraintStart_toStartOf="parent"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent"
......
......@@ -22,4 +22,5 @@
<color name="color_111235">#111235</color>
<color name="color_141C30">#141C30</color>
<color name="color_D1D1DC">#D1D1DC</color>
<color name="color_323233">#323233</color>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment