mirror of
https://github.com/zadam/trilium.git
synced 2025-10-31 18:36:30 +01:00
feat(script): api.log now supports objects
This commit is contained in:
@@ -21,6 +21,7 @@ import dayjs from "dayjs";
|
|||||||
import type NoteContext from "../components/note_context.js";
|
import type NoteContext from "../components/note_context.js";
|
||||||
import type NoteDetailWidget from "../widgets/note_detail.js";
|
import type NoteDetailWidget from "../widgets/note_detail.js";
|
||||||
import type Component from "../components/component.js";
|
import type Component from "../components/component.js";
|
||||||
|
import { formatLogMessage } from "@triliumnext/commons";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A whole number
|
* A whole number
|
||||||
@@ -455,7 +456,7 @@ export interface Api {
|
|||||||
/**
|
/**
|
||||||
* Log given message to the log pane in UI
|
* Log given message to the log pane in UI
|
||||||
*/
|
*/
|
||||||
log(message: string): void;
|
log(message: string | object): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -696,7 +697,7 @@ function FrontendScriptApi(this: Api, startNote: FNote, currentNote: FNote, orig
|
|||||||
this.log = (message) => {
|
this.log = (message) => {
|
||||||
const { noteId } = this.startNote;
|
const { noteId } = this.startNote;
|
||||||
|
|
||||||
message = `${utils.now()}: ${message}`;
|
message = `${utils.now()}: ${formatLogMessage(message)}`;
|
||||||
|
|
||||||
console.log(`Script ${noteId}: ${message}`);
|
console.log(`Script ${noteId}: ${message}`);
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import exportService from "./export/zip.js";
|
|||||||
import syncMutex from "./sync_mutex.js";
|
import syncMutex from "./sync_mutex.js";
|
||||||
import backupService from "./backup.js";
|
import backupService from "./backup.js";
|
||||||
import optionsService from "./options.js";
|
import optionsService from "./options.js";
|
||||||
|
import { formatLogMessage } from "@triliumnext/commons";
|
||||||
import type BNote from "../becca/entities/bnote.js";
|
import type BNote from "../becca/entities/bnote.js";
|
||||||
import type AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
|
import type AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
|
||||||
import type BBranch from "../becca/entities/bbranch.js";
|
import type BBranch from "../becca/entities/bbranch.js";
|
||||||
@@ -221,7 +222,7 @@ export interface Api {
|
|||||||
/**
|
/**
|
||||||
* Log given message to trilium logs and log pane in UI
|
* Log given message to trilium logs and log pane in UI
|
||||||
*/
|
*/
|
||||||
log(message: string): void;
|
log(message: string | object): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns root note of the calendar.
|
* Returns root note of the calendar.
|
||||||
@@ -556,7 +557,8 @@ function BackendScriptApi(this: Api, currentNote: BNote, apiParams: ApiParams) {
|
|||||||
this.logMessages = {};
|
this.logMessages = {};
|
||||||
this.logSpacedUpdates = {};
|
this.logSpacedUpdates = {};
|
||||||
|
|
||||||
this.log = (message) => {
|
this.log = (rawMessage) => {
|
||||||
|
const message = formatLogMessage(rawMessage);
|
||||||
log.info(message);
|
log.info(message);
|
||||||
|
|
||||||
if (!this.startNote) {
|
if (!this.startNote) {
|
||||||
|
|||||||
@@ -10,3 +10,4 @@ export * from "./lib/server_api.js";
|
|||||||
export * from "./lib/shared_constants.js";
|
export * from "./lib/shared_constants.js";
|
||||||
export * from "./lib/ws_api.js";
|
export * from "./lib/ws_api.js";
|
||||||
export * from "./lib/attribute_names.js";
|
export * from "./lib/attribute_names.js";
|
||||||
|
export * from "./lib/utils.js";
|
||||||
|
|||||||
11
packages/commons/src/lib/utils.ts
Normal file
11
packages/commons/src/lib/utils.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
export function formatLogMessage(message: string | object) {
|
||||||
|
if (typeof message === "object") {
|
||||||
|
try {
|
||||||
|
return JSON.stringify(message, null, 4);
|
||||||
|
} catch (e) {
|
||||||
|
return message.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user