Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date format to exported logs #71

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import com.infinum.sentinel.ui.logger.models.FlowBuffer
import com.infinum.sentinel.ui.logger.models.Level
import com.infinum.sentinel.ui.shared.LogFileResolver
import java.io.File
import java.util.Calendar
import java.text.SimpleDateFormat
import java.util.Locale
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
Expand All @@ -21,6 +22,8 @@ internal class SentinelFileTree(
private val logFileResolver = LogFileResolver(context)

override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm interpreting the screenshot correctly, this space seems unnecessary.

Suggested change
val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault())
val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())


MainScope().launch {
withContext(Dispatchers.IO) {
val entry = Entry(
Expand All @@ -34,7 +37,7 @@ internal class SentinelFileTree(
buffer.enqueue(entry)

val file: File = logFileResolver.createOrOpenFile()
val line = entry.asLineString()
val line = entry.asLineString(dateTimeFormat)

file.appendText(line)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import com.infinum.sentinel.SentinelFileTree
import com.infinum.sentinel.databinding.SentinelItemLogBinding
import java.text.SimpleDateFormat
import java.util.Locale

internal class LoggerAdapter(
private val onListChanged: (Boolean) -> Unit,
private val onClick: (SentinelFileTree.Entry) -> Unit
) : ListAdapter<SentinelFileTree.Entry, LoggerViewHolder>(LoggerDiffUtil()) {

private val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too, and it also makes me wonder if we could just share this pattern or formatted between the two files πŸ˜„

Suggested change
private val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault())
private val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, of course


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LoggerViewHolder =
LoggerViewHolder(
SentinelItemLogBinding.inflate(
Expand All @@ -21,7 +25,11 @@ internal class LoggerAdapter(
)

override fun onBindViewHolder(holder: LoggerViewHolder, position: Int) {
holder.bind(getItem(position), onClick)
holder.bind(
item = getItem(position),
dateTimeFormat = dateTimeFormat,
onClick = onClick
)
}

override fun onViewRecycled(holder: LoggerViewHolder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ internal class LoggerViewHolder(
private val binding: SentinelItemLogBinding
) : RecyclerView.ViewHolder(binding.root) {

fun bind(item: SentinelFileTree.Entry?, onClick: (SentinelFileTree.Entry) -> Unit) {
fun bind(
item: SentinelFileTree.Entry?,
dateTimeFormat: SimpleDateFormat,
onClick: (SentinelFileTree.Entry) -> Unit
) {
item?.let { entry ->
with(binding) {
levelView.setBackgroundColor(
Expand All @@ -31,7 +35,7 @@ internal class LoggerViewHolder(
}
)
)
timestampView.text = SimpleDateFormat.getDateTimeInstance().format(Date(entry.timestamp))
timestampView.text = dateTimeFormat.format(Date(entry.timestamp))
tagView.text = entry.tag
entry.stackTrace?.let {
stackTraceView.text = it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.infinum.sentinel.ui.logger.models

import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import org.json.JSONObject

internal open class BaseEntry(
Expand All @@ -10,7 +11,6 @@ internal open class BaseEntry(
open val message: String? = null,
open val stackTrace: String? = null
) {

fun asJSONString(): String =
JSONObject()
.put("level", level)
Expand All @@ -20,9 +20,9 @@ internal open class BaseEntry(
.put("stackTrace", stackTrace)
.toString()

fun asLineString(): String =
fun asLineString(dateTimeFormat: SimpleDateFormat): String =
buildString {
append(timestamp)
append(dateTimeFormat.format(Date(timestamp)))
append(" LEVEL: ")
append(level)
append(" TAG: ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package com.infinum.sentinel.ui.logs
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import com.infinum.sentinel.SentinelFileTree
import com.infinum.sentinel.databinding.SentinelItemLogBinding
import com.infinum.sentinel.databinding.SentinelItemLogFileBinding
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale

internal class LogsAdapter(
private val onListChanged: (Boolean) -> Unit,
private val onDelete: (File) -> Unit,
private val onShare: (File) -> Unit
) : ListAdapter<File, LogsViewHolder>(LogsDiffUtil()) {

private val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault())

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LogsViewHolder =
LogsViewHolder(
SentinelItemLogFileBinding.inflate(
Expand All @@ -24,7 +26,12 @@ internal class LogsAdapter(
)

override fun onBindViewHolder(holder: LogsViewHolder, position: Int) {
holder.bind(getItem(position), onDelete, onShare)
holder.bind(
item = getItem(position),
dateTimeFormat = dateTimeFormat,
onDelete = onDelete,
onShare = onShare
)
}

override fun onViewRecycled(holder: LogsViewHolder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.infinum.sentinel.ui.logs

import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.infinum.sentinel.R
import com.infinum.sentinel.SentinelFileTree
import com.infinum.sentinel.databinding.SentinelItemLogFileBinding
import com.infinum.sentinel.ui.logger.models.Level
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
Expand All @@ -15,11 +10,11 @@ internal class LogsViewHolder(
private val binding: SentinelItemLogFileBinding
) : RecyclerView.ViewHolder(binding.root) {

fun bind(item: File?, onDelete: (File) -> Unit, onShare: (File) -> Unit) {
fun bind(item: File?, dateTimeFormat: SimpleDateFormat, onDelete: (File) -> Unit, onShare: (File) -> Unit) {
item?.let { entry ->
with(binding) {
messageView.text = entry.name
timestampView.text = SimpleDateFormat.getDateTimeInstance().format(Date(entry.lastModified()))
timestampView.text = dateTimeFormat.format(Date(entry.lastModified()))
deleteButton.setOnClickListener { onDelete(entry) }
shareButton.setOnClickListener { onShare(entry) }
}
Expand Down
Loading