feat(script): api.log now supports objects

This commit is contained in:
Elian Doran 2025-10-01 20:22:02 +03:00
parent f412874c73
commit 7cfebbabeb
No known key found for this signature in database
4 changed files with 19 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import dayjs from "dayjs";
import type NoteContext from "../components/note_context.js";
import type NoteDetailWidget from "../widgets/note_detail.js";
import type Component from "../components/component.js";
import { formatLogMessage } from "@triliumnext/commons";
/**
* A whole number
@ -455,7 +456,7 @@ export interface Api {
/**
* 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) => {
const { noteId } = this.startNote;
message = `${utils.now()}: ${message}`;
message = `${utils.now()}: ${formatLogMessage(message)}`;
console.log(`Script ${noteId}: ${message}`);

View File

@ -23,6 +23,7 @@ import exportService from "./export/zip.js";
import syncMutex from "./sync_mutex.js";
import backupService from "./backup.js";
import optionsService from "./options.js";
import { formatLogMessage } from "@triliumnext/commons";
import type BNote from "../becca/entities/bnote.js";
import type AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.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(message: string): void;
log(message: string | object): void;
/**
* Returns root note of the calendar.
@ -556,7 +557,8 @@ function BackendScriptApi(this: Api, currentNote: BNote, apiParams: ApiParams) {
this.logMessages = {};
this.logSpacedUpdates = {};
this.log = (message) => {
this.log = (rawMessage) => {
const message = formatLogMessage(rawMessage);
log.info(message);
if (!this.startNote) {

View File

@ -10,3 +10,4 @@ export * from "./lib/server_api.js";
export * from "./lib/shared_constants.js";
export * from "./lib/ws_api.js";
export * from "./lib/attribute_names.js";
export * from "./lib/utils.js";

View 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;
}