add logging.verbose option to enable debug logging in production. (same function as MK_VERBOSE environment variable)

This commit is contained in:
Hazelnoot 2025-05-06 13:34:57 -04:00
parent 7db03f61b1
commit fd5a3eb3f8
10 changed files with 43 additions and 14 deletions

View file

@ -349,6 +349,9 @@ attachLdSignatureForRelays: true
# # Disable query truncation. If set to true, the full text of the query will be output to the log.
# # default: false
# disableQueryTruncation: false
# # Shows debug log messages after instance startup. To capture earlier debug logs, set the MK_VERBOSE environment variable.
# # default: false in production, true otherwise.
# #verbose: false
# Settings for the activity logger, which records inbound activities to the database.
# Disabled by default due to the large volume of data it saves.

View file

@ -295,6 +295,9 @@ allowedPrivateNetworks: [
# # Disable query truncation. If set to true, the full text of the query will be output to the log.
# # default: false
# disableQueryTruncation: false
# # Shows debug log messages after instance startup. To capture earlier debug logs, set the MK_VERBOSE environment variable.
# # default: false in production, true otherwise.
# #verbose: false
# Settings for the activity logger, which records inbound activities to the database.
# Disabled by default due to the large volume of data it saves.

View file

@ -411,6 +411,9 @@ attachLdSignatureForRelays: true
# # Disable query truncation. If set to true, the full text of the query will be output to the log.
# # default: false
# disableQueryTruncation: false
# # Shows debug log messages after instance startup. To capture earlier debug logs, set the MK_VERBOSE environment variable.
# # default: false in production, true otherwise.
# #verbose: false
# Settings for the activity logger, which records inbound activities to the database.
# Disabled by default due to the large volume of data it saves.

View file

@ -417,6 +417,9 @@ attachLdSignatureForRelays: true
# # Disable query truncation. If set to true, the full text of the query will be output to the log.
# # default: false
# disableQueryTruncation: false
# # Shows debug log messages after instance startup. To capture earlier debug logs, set the MK_VERBOSE environment variable.
# # default: false in production, true otherwise.
# #verbose: false
# Settings for the activity logger, which records inbound activities to the database.
# Disabled by default due to the large volume of data it saves.

View file

@ -8,6 +8,7 @@ import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import * as os from 'node:os';
import cluster from 'node:cluster';
import * as net from 'node:net';
import chalk from 'chalk';
import chalkTemplate from 'chalk-template';
import * as Sentry from '@sentry/node';
@ -18,7 +19,6 @@ import type { Config } from '@/config.js';
import { showMachineInfo } from '@/misc/show-machine-info.js';
import { envOption } from '@/env.js';
import { jobQueue, server } from './common.js';
import * as net from 'node:net';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);

View file

@ -135,7 +135,8 @@ type Source = {
sql?: {
disableQueryTruncation?: boolean,
enableQueryParamLogging?: boolean,
}
};
verbose?: boolean;
}
activityLogging?: {
@ -220,7 +221,8 @@ export type Config = {
sql?: {
disableQueryTruncation?: boolean,
enableQueryParamLogging?: boolean,
}
};
verbose?: boolean;
}
version: string;
@ -585,6 +587,7 @@ function applyEnvOverrides(config: Source) {
_apply_top(['import', ['downloadTimeout', 'maxFileSize']]);
_apply_top([['signToActivityPubGet', 'checkActivityPubGetSignature', 'setupPassword', 'disallowExternalApRedirect']]);
_apply_top(['logging', 'sql', ['disableQueryTruncation', 'enableQueryParamLogging']]);
_apply_top(['logging', ['verbose']]);
_apply_top(['activityLogging', ['enabled', 'preSave', 'maxAge']]);
_apply_top(['customHtml', ['head']]);
}

View file

@ -45,6 +45,7 @@ import { isMimeImage } from '@/misc/is-mime-image.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
import { UtilityService } from '@/core/UtilityService.js';
import { BunnyService } from '@/core/BunnyService.js';
import { LoggerService } from './LoggerService.js';
type AddFileArgs = {
/** User who wish to add file */
@ -133,8 +134,10 @@ export class DriveService {
private perUserDriveChart: PerUserDriveChart,
private instanceChart: InstanceChart,
private utilityService: UtilityService,
loggerService: LoggerService,
) {
const logger = new Logger('drive', 'blue');
const logger = loggerService.getLogger('drive', 'blue');
this.registerLogger = logger.createSubLogger('register', 'yellow');
this.downloaderLogger = logger.createSubLogger('downloader');
this.deleteLogger = logger.createSubLogger('delete');

View file

@ -3,19 +3,25 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import type { KEYWORD } from 'color-convert/conversions.js';
import { envOption } from '@/env.js';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
@Injectable()
export class LoggerService {
constructor(
@Inject(DI.config)
private config: Config,
) {
}
@bindThis
public getLogger(domain: string, color?: KEYWORD | undefined) {
return new Logger(domain, color);
const verbose = this.config.logging?.verbose || envOption.verbose;
return new Logger(domain, color, verbose);
}
}

View file

@ -28,9 +28,8 @@ import type { Config } from '@/config.js';
import { AccountMoveService } from '@/core/AccountMoveService.js';
import { UtilityService } from '@/core/UtilityService.js';
import type { ThinUser } from '@/queue/types.js';
import Logger from '../logger.js';
const logger = new Logger('following/create');
import { LoggerService } from '@/core/LoggerService.js';
import type Logger from '../logger.js';
type Local = MiLocalUser | {
id: MiLocalUser['id'];
@ -48,6 +47,7 @@ type Both = Local | Remote;
@Injectable()
export class UserFollowingService implements OnModuleInit {
private userBlockingService: UserBlockingService;
private readonly logger: Logger;
constructor(
private moduleRef: ModuleRef,
@ -86,7 +86,10 @@ export class UserFollowingService implements OnModuleInit {
private accountMoveService: AccountMoveService,
private perUserFollowingChart: PerUserFollowingChart,
private instanceChart: InstanceChart,
loggerService: LoggerService,
) {
this.logger = loggerService.getLogger('following/create');
}
onModuleInit() {
@ -254,7 +257,7 @@ export class UserFollowingService implements OnModuleInit {
followeeSharedInbox: this.userEntityService.isRemoteUser(followee) ? followee.sharedInbox : null,
}).catch(err => {
if (isDuplicateKeyValueError(err) && this.userEntityService.isRemoteUser(follower) && this.userEntityService.isLocalUser(followee)) {
logger.info(`Insert duplicated ignore. ${follower.id} => ${followee.id}`);
this.logger.info(`Insert duplicated ignore. ${follower.id} => ${followee.id}`);
alreadyFollowed = true;
} else {
throw err;
@ -372,7 +375,7 @@ export class UserFollowingService implements OnModuleInit {
});
if (following === null || !following.follower || !following.followee) {
logger.warn('フォロー解除がリクエストされましたがフォローしていませんでした');
this.logger.warn('フォロー解除がリクエストされましたがフォローしていませんでした');
return;
}

View file

@ -27,17 +27,19 @@ export type DataObject = Record<string, unknown> | (object & { length?: never; }
export default class Logger {
private context: Context;
private parentLogger: Logger | null = null;
private readonly verbose: boolean;
constructor(context: string, color?: KEYWORD) {
constructor(context: string, color?: KEYWORD, verbose?: boolean) {
this.context = {
name: context,
color: color,
};
this.verbose = verbose ?? envOption.verbose;
}
@bindThis
public createSubLogger(context: string, color?: KEYWORD): Logger {
const logger = new Logger(context, color);
const logger = new Logger(context, color, this.verbose);
logger.parentLogger = this;
return logger;
}
@ -110,7 +112,7 @@ export default class Logger {
@bindThis
public debug(message: string, data?: Data, important = false): void { // デバッグ用に使う(開発者に必要だが利用者に不要な情報)
if (process.env.NODE_ENV !== 'production' || envOption.verbose) {
if (process.env.NODE_ENV !== 'production' || this.verbose) {
this.log('debug', message, data, important);
}
}