2024-03-09 12:33:05 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
|
|
|
|
export enum ItemActionType {
|
|
|
|
Delete = 'DeleteAction',
|
|
|
|
}
|
|
|
|
|
|
|
|
const actionTypeToLogger = {
|
|
|
|
[ItemActionType.Delete]: Logger.create(ItemActionType.Delete),
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class ActionLogger {
|
2024-03-14 20:34:11 +02:00
|
|
|
private descriptions_: string[] = [];
|
2024-03-09 12:33:05 +02:00
|
|
|
|
|
|
|
private constructor(private source: string) { }
|
|
|
|
|
|
|
|
public clone() {
|
|
|
|
const clone = new ActionLogger(this.source);
|
2024-03-14 20:34:11 +02:00
|
|
|
clone.descriptions_ = [...this.descriptions_];
|
2024-03-09 12:33:05 +02:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
// addDescription is used to add labels with information that may not be available
|
|
|
|
// when .log is called. For example, to include the title of a deleted note.
|
|
|
|
public addDescription(description: string) {
|
2024-03-14 20:34:11 +02:00
|
|
|
this.descriptions_.push(description);
|
2024-03-09 12:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public log(action: ItemActionType, itemIds: string|string[]) {
|
2024-03-14 20:34:11 +02:00
|
|
|
if (!ActionLogger.enabled_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-09 12:33:05 +02:00
|
|
|
const logger = actionTypeToLogger[action];
|
2024-03-14 20:34:11 +02:00
|
|
|
logger.info(`${this.source}: ${this.descriptions_.join(',')}; Item IDs: ${JSON.stringify(itemIds)}`);
|
2024-03-09 12:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static from(source: ActionLogger|string|undefined) {
|
|
|
|
if (!source) {
|
|
|
|
source = 'Unknown source';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof source === 'string') {
|
|
|
|
return new ActionLogger(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
return source;
|
|
|
|
}
|
2024-03-14 20:34:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Disabling the action logger globally can be useful on Joplin Server/Cloud
|
|
|
|
// when many deletions are expected (e.g. for email-to-note).
|
|
|
|
private static enabled_ = true;
|
|
|
|
|
|
|
|
public static set enabled(v: boolean) {
|
|
|
|
this.enabled_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static get enabled() {
|
|
|
|
return this.enabled_;
|
|
|
|
}
|
2024-03-09 12:33:05 +02:00
|
|
|
}
|