dev: improve actor logs

This commit adds the concept of a toLogFields method on objects which
will determine how they're turned into log fields in LogService. This
method is now implemented on actor to prevent errors when the object is
passed to log functions without being stringified first.
This commit is contained in:
KernelDeimos
2025-04-22 12:52:09 -04:00
parent b80052f177
commit 7e7e2897b2
2 changed files with 17 additions and 1 deletions
+8 -1
View File
@@ -30,6 +30,7 @@ const winston = require('winston');
const { Context } = require('../../util/context');
const BaseService = require('../../services/BaseService');
const { stringify_log_entry } = require('./lib/log');
const { whatis } = require('../../util/langutil');
require('winston-daily-rotate-file');
const WINSTON_LEVELS = {
@@ -99,12 +100,18 @@ class LogContext {
}
if ( ! fields.actor && x && x.get('actor') ) {
try {
fields.actor = x.get('actor').uid;
fields.actor = x.get('actor');
} catch (e) {
console.log('error logging actor (this is probably fine):', e);
}
}
}
for ( const k in fields ) {
if (
whatis(fields[k]) === 'object' &&
typeof fields[k].toLogFields === 'function'
) fields[k] = fields[k].toLogFields();
}
this.logService.log_(
log_level,
this.crumbs,
+9
View File
@@ -91,6 +91,15 @@ class Actor extends AdvancedBase {
get uid () {
return this.type.uid;
}
toLogFields () {
return {
uid: this.type.uid,
...(this.type.user ? {
username: this.type.user.username,
} : {})
}
}
/**
* Generates a cryptographically-secure deterministic UUID