Add logger that encapsulates console logging

This commit is contained in:
Johannes Loher 2021-06-26 14:32:40 +02:00
parent 1f6d13c49d
commit a987b59b65
12 changed files with 92 additions and 57 deletions

View file

@ -235,7 +235,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
event.preventDefault();
const id = $(event.currentTarget).parents(".item").data("itemId");
const item = this.actor.getOwnedItem(id);
item.roll();
item.roll().catch((e) => notifications.error(e, { log: true }));
}
/**
@ -245,7 +245,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
protected _onRollCheck(event: JQuery.ClickEvent): void {
event.preventDefault();
const check = event.currentTarget.dataset["check"];
this.actor.rollCheck(check);
this.actor.rollCheck(check).catch((e) => notifications.error(e, { log: true }));
}
/** @override */

View file

@ -6,6 +6,7 @@ import registerHandlebarsHelpers from "../handlebars/handlebars-helpers";
import registerHandlebarsPartials from "../handlebars/handlebars-partials";
import { DS4Item } from "../item/item";
import { DS4ItemSheet } from "../item/item-sheet";
import logger from "../logger";
import { macros } from "../macros/macros";
import { migration } from "../migrations";
import { DS4Check } from "../rolls/check";
@ -19,7 +20,7 @@ export default function registerForInitHook(): void {
}
async function init() {
console.log(`DS4 | Initializing the DS4 Game System\n${DS4.ASCII}`);
logger.info(`Initializing the DS4 Game System\n${DS4.ASCII}`);
game.ds4 = {
DS4Actor,

27
src/module/logger.ts Normal file
View file

@ -0,0 +1,27 @@
const loggingContext = "DS4";
const loggingSeparator = "|";
type LogLevel = "debug" | "info" | "warning" | "error";
type LoggingFunction = (...data: unknown[]) => void;
class Logger {
readonly debug: LoggingFunction;
readonly info: LoggingFunction;
readonly warn: LoggingFunction;
readonly error: LoggingFunction;
constructor() {
this.debug = this.getLoggingFunction("debug");
this.info = this.getLoggingFunction("info");
this.warn = this.getLoggingFunction("warning");
this.error = this.getLoggingFunction("error");
}
getLoggingFunction(type: LogLevel = "info") {
const log = { debug: console.debug, info: console.info, warning: console.warn, error: console.error }[type];
return (...data: unknown[]) => log(loggingContext, loggingSeparator, ...data);
}
}
const logger = new Logger();
export default logger;

View file

@ -45,5 +45,5 @@ export async function rollCheck(check: Check): Promise<void> {
return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
}
return actor.rollCheck(check);
return actor.rollCheck(check).catch((e) => notifications.error(e, { log: true }));
}

View file

@ -9,5 +9,5 @@ export async function rollGenericCheck(): Promise<void> {
return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
}
return actor.rollGenericCheck();
return actor.rollGenericCheck().catch((e) => notifications.error(e, { log: true }));
}

View file

@ -53,5 +53,5 @@ export async function rollItem(itemId: string): Promise<void> {
);
}
return item.roll();
return item.roll().catch((e) => notifications.error(e, { log: true }));
}

View file

@ -1,3 +1,4 @@
import logger from "./logger";
import { migrate as migrate001 } from "./migrations/001";
import { migrate as migrate002 } from "./migrations/002";
import { migrate as migrate003 } from "./migrations/003";
@ -40,7 +41,7 @@ async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion
for (const [i, migration] of migrationsToExecute.entries()) {
const currentMigrationVersion = oldMigrationVersion + i + 1;
console.log("executing migration script ", currentMigrationVersion);
logger.info("executing migration script ", currentMigrationVersion);
try {
await migration();
game.settings.set("ds4", "systemMigrationVersion", currentMigrationVersion);
@ -54,7 +55,7 @@ async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion
{ permanent: true },
);
err.message = `Failed ds4 system migration: ${err.message}`;
console.error(err);
logger.error(err);
return;
}
}

View file

@ -1,7 +1,9 @@
import logger from "../logger";
export async function migrate(): Promise<void> {
for (const a of game.actors?.entities ?? []) {
const updateData = getActorUpdateData();
console.log(`Migrating actor ${a.name}`);
logger.info(`Migrating actor ${a.name}`);
await a.update(updateData, { enforceTypes: false });
}
}

View file

@ -1,3 +1,5 @@
import logger from "../logger";
export async function migrate(): Promise<void> {
await migrateItems();
await migrateActors();
@ -10,12 +12,12 @@ async function migrateItems() {
try {
const updateData = getItemUpdateData(item._data);
if (updateData) {
console.log(`Migrating Item entity ${item.name} (${item.id})`);
logger.info(`Migrating Item entity ${item.name} (${item.id})`);
await item.update(updateData), { enforceTypes: false };
}
} catch (err) {
err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -30,12 +32,12 @@ async function migrateActors() {
try {
const updateData = getActorUpdateData(actor._data);
if (updateData) {
console.log(`Migrating Actor entity ${actor.name} (${actor.id})`);
logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
await actor.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -59,12 +61,12 @@ async function migrateScenes() {
try {
const updateData = getSceneUpdateData(scene._data);
if (updateData) {
console.log(`Migrating Scene entity ${scene.name} (${scene.id})`);
logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
await scene.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -123,12 +125,12 @@ async function migrateCompendium(compendium: Compendium) {
};
const updateData = getUpdateData(entity);
if (updateData) {
console.log(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
await compendium.updateEntity({ ...updateData, _id: entity._id });
}
} catch (err) {
err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
console.error(err);
logger.error(err);
}
}

View file

@ -1,3 +1,5 @@
import logger from "../logger";
export async function migrate(): Promise<void> {
await migrateItems();
await migrateActors();
@ -10,12 +12,12 @@ async function migrateItems() {
try {
const updateData = getItemUpdateData(item._data);
if (updateData) {
console.log(`Migrating Item entity ${item.name} (${item.id})`);
logger.info(`Migrating Item entity ${item.name} (${item.id})`);
await item.update(updateData), { enforceTypes: false };
}
} catch (err) {
err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -34,12 +36,12 @@ async function migrateActors() {
try {
const updateData = getActorUpdateData(actor._data);
if (updateData) {
console.log(`Migrating Actor entity ${actor.name} (${actor.id})`);
logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
await actor.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -63,12 +65,12 @@ async function migrateScenes() {
try {
const updateData = getSceneUpdateData(scene._data);
if (updateData) {
console.log(`Migrating Scene entity ${scene.name} (${scene.id})`);
logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
await scene.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -127,12 +129,12 @@ async function migrateCompendium(compendium: Compendium) {
};
const updateData = getUpdateData(entity);
if (updateData) {
console.log(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
await compendium.updateEntity({ ...updateData, _id: entity._id });
}
} catch (err) {
err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
console.error(err);
logger.error(err);
}
}

View file

@ -1,4 +1,5 @@
import { DS4SpellDataData } from "../item/item-data";
import logger from "../logger";
export async function migrate(): Promise<void> {
await migrateItems();
@ -12,12 +13,12 @@ async function migrateItems() {
try {
const updateData = getItemUpdateData(item._data);
if (updateData) {
console.log(`Migrating Item entity ${item.name} (${item.id})`);
logger.info(`Migrating Item entity ${item.name} (${item.id})`);
await item.update(updateData), { enforceTypes: false };
}
} catch (err) {
err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -39,12 +40,12 @@ async function migrateActors() {
try {
const updateData = getActorUpdateData(actor._data);
if (updateData) {
console.log(`Migrating Actor entity ${actor.name} (${actor.id})`);
logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
await actor.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -75,12 +76,12 @@ async function migrateScenes() {
try {
const updateData = getSceneUpdateData(scene._data);
if (updateData) {
console.log(`Migrating Scene entity ${scene.name} (${scene.id})`);
logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
await scene.update(updateData, { enforceTypes: false });
}
} catch (err) {
err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`;
console.error(err);
logger.error(err);
}
}
}
@ -140,12 +141,12 @@ async function migrateCompendium(compendium: Compendium) {
};
const updateData = getUpdateData(entity);
if (updateData) {
console.log(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
await compendium.updateEntity({ ...updateData, _id: entity._id });
}
} catch (err) {
err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
console.error(err);
logger.error(err);
}
}

View file

@ -1,35 +1,34 @@
import logger from "../logger";
function getNotificationFunction(type: "info" | "warn" | "error") {
return (message: string, { permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {}): void => {
if (ui.notifications) {
ui.notifications[type](message, { permanent });
if (log) {
logger[type](message);
}
} else {
logger[type](message);
}
};
}
const notifications = {
info: (message: string, { permanent = false }: { permanent?: boolean } = {}): void => {
if (ui.notifications) {
ui.notifications.info(message, { permanent });
} else {
console.info(message);
}
},
warn: (message: string, { permanent = false }: { permanent?: boolean } = {}): void => {
if (ui.notifications) {
ui.notifications.warn(message, { permanent });
} else {
console.log(message);
}
},
error: (message: string, { permanent = false }: { permanent?: boolean } = {}): void => {
if (ui.notifications) {
ui.notifications.error(message, { permanent });
} else {
console.warn(message);
}
},
info: getNotificationFunction("info"),
warn: getNotificationFunction("warn"),
error: getNotificationFunction("error"),
notify: (
message: string,
type: "info" | "warning" | "error" = "info",
{ permanent = false }: { permanent?: boolean } = {},
{ permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {},
): void => {
if (ui.notifications) {
ui.notifications.notify(message, type, { permanent });
if (log) {
logger.getLoggingFunction(type)(message);
}
} else {
const log = { info: console.info, warning: console.warn, error: console.error }[type];
log(message);
logger.getLoggingFunction(type)(message);
}
},
};