#205 - Define a logging level setting

This commit is contained in:
Elio Struyf
2021-12-15 14:10:58 +01:00
parent b81ef077f6
commit 5a69240178
4 changed files with 69 additions and 16 deletions
+15 -1
View File
@@ -130,7 +130,7 @@
"boolean",
"choice"
],
"description": ""
"description": "Type of the draft field you want to use"
},
"name": {
"type": "string",
@@ -709,6 +709,20 @@
"default": "yyyy-MM-dd",
"markdownDescription": "Specify the prefix you want to add for your new article filenames. [Check in the docs](https://frontmatter.codes/docs/settings#frontmatter.templates.prefix)",
"scope": "Templates"
},
"frontMatter.global.notifications": {
"type": "array",
"items": {
"type": "string",
"enum": [
"info",
"warning",
"error"
]
},
"default": ["info", "warning", "error"],
"markdownDescription": "Specifies the notifications you want to see. By default, all notifications types will be shown. [Check in the docs](https://frontmatter.codes/docs/settings#frontMatter.global.notifications)",
"scope": "Templates"
}
}
},
+2
View File
@@ -2,6 +2,8 @@ export const EXTENSION_NAME = "Front Matter";
export const CONFIG_KEY = "frontMatter";
export const SETTING_GLOBAL_NOTIFICATIONS = "global.notifications";
export const SETTING_TAXONOMY_TAGS = "taxonomy.tags";
export const SETTING_TAXONOMY_CATEGORIES = "taxonomy.categories";
export const SETTING_TAXONOMY_CUSTOM = "taxonomy.customTaxonomy";
+14 -7
View File
@@ -17,6 +17,7 @@ import { ContentType } from '../models';
import { DateHelper } from './DateHelper';
export class ArticleHelper {
private static notifiedFiles: string[] = [];
/**
* Get the contents of the current article
@@ -246,6 +247,8 @@ export class ArticleHelper {
}
}
this.notifiedFiles = this.notifiedFiles.filter(n => n !== fileName);
return article;
}
}
@@ -258,14 +261,18 @@ export class ArticleHelper {
}];
if (!surpressNotification) {
Notifications.error(`There seems to be an issue parsing the content its front matter. FileName: ${basename(fileName)}. ERROR: ${error.message || error}`, ...items).then((result: any) => {
if (result?.title) {
const item = items.find(i => i.title === result.title);
if (item) {
item.action();
if (this.notifiedFiles.indexOf(fileName) === -1) {
Notifications.error(`There seems to be an issue parsing the content its front matter. FileName: ${basename(fileName)}. ERROR: ${error.message || error}`, ...items).then((result: any) => {
if (result?.title) {
const item = items.find(i => i.title === result.title);
if (item) {
item.action();
}
}
}
});
});
this.notifiedFiles.push(fileName);
}
}
}
return null;
+38 -8
View File
@@ -1,22 +1,52 @@
import { window } from "vscode";
import { Logger } from ".";
import { EXTENSION_NAME } from "../constants";
import { EXTENSION_NAME, SETTING_GLOBAL_NOTIFICATIONS } from "../constants";
import { Logger } from "./Logger";
import { Settings } from "./SettingsHelper";
export class Notifications {
public static info(message: string, items?: any): Thenable<string> {
public static info(message: string, items?: any): Thenable<string | undefined> {
Logger.info(`${EXTENSION_NAME}: ${message}`, "INFO");
return window.showInformationMessage(`${EXTENSION_NAME}: ${message}`, items);
if (this.shouldShow("INFO")) {
return window.showInformationMessage(`${EXTENSION_NAME}: ${message}`, items);
}
return Promise.resolve(undefined);
}
public static warning(message: string, items?: any): Thenable<string> {
public static warning(message: string, items?: any): Thenable<string | undefined> {
Logger.info(`${EXTENSION_NAME}: ${message}`, "WARNING");
return window.showWarningMessage(`${EXTENSION_NAME}: ${message}`, items);
if (this.shouldShow("WARNING")) {
return window.showWarningMessage(`${EXTENSION_NAME}: ${message}`, items);
}
return Promise.resolve(undefined);
}
public static error(message: string, items?: any): Thenable<string> {
public static error(message: string, items?: any): Thenable<string | undefined> {
Logger.info(`${EXTENSION_NAME}: ${message}`, "ERROR");
return window.showErrorMessage(`${EXTENSION_NAME}: ${message}`, items);
if (this.shouldShow("ERROR")) {
return window.showErrorMessage(`${EXTENSION_NAME}: ${message}`, items);
}
return Promise.resolve(undefined);
}
private static shouldShow(level: "INFO" | "WARNING" | "ERROR"): boolean {
let levels = Settings.get<string[]>(SETTING_GLOBAL_NOTIFICATIONS);
if (!levels) {
return true;
}
if (levels.map(l => l.toLowerCase()).includes(level.toLowerCase())) {
return true;
}
return false;
}
}