Logging: Allow logging structured fields

Display this in the Debug Screen app. Additionally, add a reason why a
file was not loaded.
This commit is contained in:
Vishesh Handa
2020-05-17 19:47:11 +02:00
parent 2339f50d89
commit a324d0f755
3 changed files with 60 additions and 15 deletions

View File

@ -186,12 +186,15 @@ class NotesFolderFS with NotesFolderNotifier implements NotesFolder {
}
if (fsEntity is Directory) {
Log.v("Found directory ${fsEntity.path}");
var subFolder = NotesFolderFS(this, fsEntity.path);
if (subFolder.name.startsWith('.')) {
Log.v("Ignoring Folder", props: {
"path": fsEntity.path,
"reason": "Hidden folder",
});
continue;
}
Log.v("Found folder ${fsEntity.path}");
Log.v("Found Folder", props: {"path": fsEntity.path});
_addFolderListeners(subFolder);
_folders.add(subFolder);
@ -203,11 +206,21 @@ class NotesFolderFS with NotesFolderNotifier implements NotesFolder {
}
var note = Note(this, fsEntity.path);
if (!note.filePath.toLowerCase().endsWith('.md')) {
Log.v("Ignoring file ${fsEntity.path}");
if (note.fileName.startsWith('.')) {
Log.v("Ignoring file", props: {
"path": fsEntity.path,
"reason": "Starts with a .",
});
continue;
}
Log.v("Found file ${fsEntity.path}");
if (!note.filePath.toLowerCase().endsWith('.md')) {
Log.v("Ignoring file", props: {
"path": fsEntity.path,
"reason": "Doesn't end with a .md",
});
continue;
}
Log.v("Found file", props: {"path": fsEntity.path});
_addNoteListeners(note);
_notes.add(note);

View File

@ -79,12 +79,33 @@ class _DebugScreenState extends State<DebugScreen> {
if (msg.stack != null) {
str += ' ' + msg.stack;
}
var props = <TextSpan>[];
msg.props?.forEach((key, value) {
var emptySpace = TextSpan(
text: '\n ',
style: textStyle.copyWith(fontWeight: FontWeight.bold));
props.add(emptySpace);
var keySpan = TextSpan(
text: '$key: ',
style: textStyle.copyWith(
fontWeight: FontWeight.bold,
),
);
var valueSpan = TextSpan(text: value.toString());
props.add(keySpan);
props.add(valueSpan);
});
return RichText(
text: TextSpan(children: [
TextSpan(
text: timeStr,
style: textStyle.copyWith(fontWeight: FontWeight.bold)),
TextSpan(text: str),
...props,
], style: textStyle),
);
}

View File

@ -29,29 +29,34 @@ class Log {
await setLogCapture(true);
}
static void v(String msg, {dynamic ex, StackTrace stacktrace}) {
static void v(String msg,
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
Fimber.v(msg, ex: ex, stacktrace: stacktrace);
_write('v', msg, ex, stacktrace);
_write('v', msg, ex, stacktrace, props);
}
static void d(String msg, {dynamic ex, StackTrace stacktrace}) {
static void d(String msg,
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
Fimber.d(msg, ex: ex, stacktrace: stacktrace);
_write('d', msg, ex, stacktrace);
_write('d', msg, ex, stacktrace, props);
}
static void i(String msg, {dynamic ex, StackTrace stacktrace}) {
static void i(String msg,
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
Fimber.i(msg, ex: ex, stacktrace: stacktrace);
_write('i', msg, ex, stacktrace);
_write('i', msg, ex, stacktrace, props);
}
static void e(String msg, {dynamic ex, StackTrace stacktrace}) {
static void e(String msg,
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
Fimber.e(msg, ex: ex, stacktrace: stacktrace);
_write('e', msg, ex, stacktrace);
_write('e', msg, ex, stacktrace, props);
}
static void w(String msg, {dynamic ex, StackTrace stacktrace}) {
static void w(String msg,
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
Fimber.w(msg, ex: ex, stacktrace: stacktrace);
_write('w', msg, ex, stacktrace);
_write('w', msg, ex, stacktrace, props);
}
static void _write(
@ -59,6 +64,7 @@ class Log {
String msg,
dynamic ex,
StackTrace stackTrace,
Map<String, dynamic> props,
) {
if (logFile == null) {
return;
@ -72,6 +78,7 @@ class Log {
stack: stackTrace != null
? stackTrace.toString().replaceAll('\n', ' ')
: null,
props: props,
);
var str = json.encode(logMsg.toMap());
@ -118,6 +125,7 @@ class LogMessage {
String msg;
String ex;
String stack;
Map<String, dynamic> props;
LogMessage({
@required this.t,
@ -125,6 +133,7 @@ class LogMessage {
@required this.msg,
this.ex,
this.stack,
this.props,
});
Map<String, dynamic> toMap() {
@ -134,6 +143,7 @@ class LogMessage {
'msg': msg,
if (ex != null && ex.isNotEmpty) 'ex': ex,
if (stack != null && stack.isNotEmpty) 'stack': stack,
if (props != null && props.isNotEmpty) 'p': props,
};
}
@ -143,5 +153,6 @@ class LogMessage {
msg = map['msg'];
ex = map['ex'];
stack = map['stack'];
props = map['p'];
}
}