Edit some logging behaviour
This commit is contained in:
parent
844d11b9ca
commit
4566fa947d
|
@ -14,7 +14,7 @@ export class InfoConfigService {
|
||||||
SysPreference.HostOverride,
|
SysPreference.HostOverride,
|
||||||
);
|
);
|
||||||
if (HasFailed(hostname)) {
|
if (HasFailed(hostname)) {
|
||||||
this.logger.warn(hostname.print());
|
hostname.print(this.logger);
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ export class MultiPartPipe implements PipeTransform {
|
||||||
) {
|
) {
|
||||||
const filesLimit = typeof data === 'number' ? data : undefined;
|
const filesLimit = typeof data === 'number' ? data : undefined;
|
||||||
|
|
||||||
if (!request.isMultipart()) throw Fail(FT.UsrValidation, 'Invalid file');
|
if (!request.isMultipart()) throw Fail(FT.UsrValidation, 'Invalid files');
|
||||||
|
|
||||||
const files = request.files({
|
const files = request.files({
|
||||||
limits: this.multipartConfigService.getLimits(filesLimit),
|
limits: this.multipartConfigService.getLimits(filesLimit),
|
||||||
|
|
|
@ -6,7 +6,7 @@ import {
|
||||||
Logger,
|
Logger,
|
||||||
MethodNotAllowedException,
|
MethodNotAllowedException,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
UnauthorizedException,
|
UnauthorizedException
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||||
import { ApiErrorResponse } from 'picsur-shared/dist/dto/api/api.dto';
|
import { ApiErrorResponse } from 'picsur-shared/dist/dto/api/api.dto';
|
||||||
|
@ -14,7 +14,7 @@ import {
|
||||||
Fail,
|
Fail,
|
||||||
Failure,
|
Failure,
|
||||||
FT,
|
FT,
|
||||||
IsFailure,
|
IsFailure
|
||||||
} from 'picsur-shared/dist/types/failable';
|
} from 'picsur-shared/dist/types/failable';
|
||||||
|
|
||||||
// This will catch any exception that is made in any request
|
// This will catch any exception that is made in any request
|
||||||
|
@ -39,23 +39,7 @@ export class MainExceptionFilter implements ExceptionFilter {
|
||||||
const status = exception.getCode();
|
const status = exception.getCode();
|
||||||
const type = exception.getType();
|
const type = exception.getType();
|
||||||
|
|
||||||
const message = exception.getReason();
|
exception.print(MainExceptionFilter.logger, { prefix: traceString });
|
||||||
const logmessage =
|
|
||||||
message +
|
|
||||||
(exception.getDebugMessage() ? ' - ' + exception.getDebugMessage() : '');
|
|
||||||
|
|
||||||
if (exception.isImportant()) {
|
|
||||||
MainExceptionFilter.logger.error(
|
|
||||||
`${traceString} ${exception.getName()}: ${logmessage}`,
|
|
||||||
);
|
|
||||||
if (exception.getStack()) {
|
|
||||||
MainExceptionFilter.logger.debug(exception.getStack());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
MainExceptionFilter.logger.warn(
|
|
||||||
`${traceString} ${exception.getName()}: ${logmessage}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const toSend: ApiErrorResponse = {
|
const toSend: ApiErrorResponse = {
|
||||||
success: false,
|
success: false,
|
||||||
|
@ -65,7 +49,7 @@ export class MainExceptionFilter implements ExceptionFilter {
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
type,
|
type,
|
||||||
message,
|
message: exception.getReason(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import fastifyReplyFrom from '@fastify/reply-from';
|
||||||
import { NestFactory } from '@nestjs/core';
|
import { NestFactory } from '@nestjs/core';
|
||||||
import {
|
import {
|
||||||
FastifyAdapter,
|
FastifyAdapter,
|
||||||
NestFastifyApplication,
|
NestFastifyApplication
|
||||||
} from '@nestjs/platform-fastify';
|
} from '@nestjs/platform-fastify';
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
import { HostConfigService } from './config/early/host.config.service';
|
import { HostConfigService } from './config/early/host.config.service';
|
||||||
|
@ -39,14 +39,17 @@ async function bootstrap() {
|
||||||
fastifyAdapter,
|
fastifyAdapter,
|
||||||
{
|
{
|
||||||
bufferLogs: isProduction,
|
bufferLogs: isProduction,
|
||||||
|
autoFlushLogs: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// Configure logger
|
// Configure logger
|
||||||
app.useLogger(app.get(PicsurLoggerService));
|
const logger = app.get(PicsurLoggerService)
|
||||||
|
app.useLogger(logger);
|
||||||
app.flushLogs();
|
app.flushLogs();
|
||||||
|
|
||||||
|
console.log(logger);
|
||||||
|
|
||||||
app.useGlobalFilters(app.get(MainExceptionFilter));
|
app.useGlobalFilters(app.get(MainExceptionFilter));
|
||||||
app.useGlobalInterceptors(app.get(SuccessInterceptor));
|
app.useGlobalInterceptors(app.get(SuccessInterceptor));
|
||||||
app.useGlobalPipes(app.get(ZodValidationPipe));
|
app.useGlobalPipes(app.get(ZodValidationPipe));
|
||||||
|
|
|
@ -3,7 +3,7 @@ import ms from 'ms';
|
||||||
import { ImageRequestParams } from 'picsur-shared/dist/dto/api/image.dto';
|
import { ImageRequestParams } from 'picsur-shared/dist/dto/api/image.dto';
|
||||||
import {
|
import {
|
||||||
FileType,
|
FileType,
|
||||||
SupportedFileTypeCategory,
|
SupportedFileTypeCategory
|
||||||
} from 'picsur-shared/dist/dto/mimes.dto';
|
} from 'picsur-shared/dist/dto/mimes.dto';
|
||||||
import { SysPreference } from 'picsur-shared/dist/dto/sys-preferences.enum';
|
import { SysPreference } from 'picsur-shared/dist/dto/sys-preferences.enum';
|
||||||
import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types';
|
import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types';
|
||||||
|
@ -122,16 +122,4 @@ export class ImageConverterService {
|
||||||
filetype: targetFiletype.identifier,
|
filetype: targetFiletype.identifier,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private async convertAnimation(
|
|
||||||
image: Buffer,
|
|
||||||
targetFiletype: FileType,
|
|
||||||
options: ImageRequestParams,
|
|
||||||
): AsyncFailable<ImageResult> {
|
|
||||||
// Apng and gif are stored as is for now
|
|
||||||
return {
|
|
||||||
image: image,
|
|
||||||
filetype: targetFiletype.identifier,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
FileType,
|
FileType,
|
||||||
ImageFileType,
|
ImageFileType,
|
||||||
SupportedFileTypeCategory,
|
SupportedFileTypeCategory
|
||||||
} from 'picsur-shared/dist/dto/mimes.dto';
|
} from 'picsur-shared/dist/dto/mimes.dto';
|
||||||
|
|
||||||
import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types';
|
import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types';
|
||||||
|
|
|
@ -19,7 +19,7 @@ import { ImageManagerService } from './image.service';
|
||||||
ImageProcessorService,
|
ImageProcessorService,
|
||||||
ImageConverterService,
|
ImageConverterService,
|
||||||
],
|
],
|
||||||
exports: [ImageManagerService],
|
exports: [ImageManagerService, ImageConverterService],
|
||||||
})
|
})
|
||||||
export class ImageManagerModule implements OnModuleInit {
|
export class ImageManagerModule implements OnModuleInit {
|
||||||
private readonly logger = new Logger(ImageManagerModule.name);
|
private readonly logger = new Logger(ImageManagerModule.name);
|
||||||
|
@ -31,7 +31,7 @@ export class ImageManagerModule implements OnModuleInit {
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async onModuleInit() {
|
async onModuleInit() {
|
||||||
await this.imageManagerCron()
|
await this.imageManagerCron();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Interval(1000 * 60)
|
@Interval(1000 * 60)
|
||||||
|
@ -57,7 +57,7 @@ export class ImageManagerModule implements OnModuleInit {
|
||||||
|
|
||||||
const result = await this.imageFileDB.cleanupDerivatives(after_ms / 1000);
|
const result = await this.imageFileDB.cleanupDerivatives(after_ms / 1000);
|
||||||
if (HasFailed(result)) {
|
if (HasFailed(result)) {
|
||||||
this.logger.warn(result.print());
|
result.print(this.logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result > 0) this.logger.log(`Cleaned up ${result} derivatives`);
|
if (result > 0) this.logger.log(`Cleaned up ${result} derivatives`);
|
||||||
|
@ -67,7 +67,8 @@ export class ImageManagerModule implements OnModuleInit {
|
||||||
const cleanedUp = await this.imageDB.cleanupExpired();
|
const cleanedUp = await this.imageDB.cleanupExpired();
|
||||||
|
|
||||||
if (HasFailed(cleanedUp)) {
|
if (HasFailed(cleanedUp)) {
|
||||||
this.logger.warn(cleanedUp.print());
|
cleanedUp.print(this.logger);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cleanedUp > 0)
|
if (cleanedUp > 0)
|
||||||
|
|
|
@ -64,7 +64,7 @@ export class SettingsShareXComponent implements OnInit {
|
||||||
|
|
||||||
const ext = FileType2Ext(this.selectedFormat);
|
const ext = FileType2Ext(this.selectedFormat);
|
||||||
if (HasFailed(ext)) {
|
if (HasFailed(ext)) {
|
||||||
this.logger.error(ext.print());
|
ext.print(this.logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
const sharexConfig = BuildShareX(
|
const sharexConfig = BuildShareX(
|
||||||
|
|
|
@ -15,11 +15,7 @@ export class ErrorService {
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public showFailure(error: Failure, logger: Logger): void {
|
public showFailure(error: Failure, logger: Logger): void {
|
||||||
if (error.isImportant()) {
|
error.print(logger);
|
||||||
logger.error(error.print());
|
|
||||||
} else {
|
|
||||||
logger.warn(error.print());
|
|
||||||
}
|
|
||||||
|
|
||||||
this.snackbar.showSnackBar(
|
this.snackbar.showSnackBar(
|
||||||
error.getReason(),
|
error.getReason(),
|
||||||
|
|
|
@ -21,6 +21,12 @@ export enum FT {
|
||||||
Network = 'network',
|
Network = 'network',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ILogger {
|
||||||
|
error: (message: string) => void;
|
||||||
|
warn: (message: string) => void;
|
||||||
|
debug: (message: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
interface FTProp {
|
interface FTProp {
|
||||||
important: boolean;
|
important: boolean;
|
||||||
code: number;
|
code: number;
|
||||||
|
@ -142,10 +148,42 @@ export class Failure {
|
||||||
return FTProps[this.type].important;
|
return FTProps[this.type].important;
|
||||||
}
|
}
|
||||||
|
|
||||||
print(): string {
|
print(
|
||||||
return `${this.getName()}: ${this.getReason()}\n(${
|
logger: ILogger,
|
||||||
this.debugMessage
|
options?: {
|
||||||
})\n${this.getStack()}`;
|
notImportant?: boolean;
|
||||||
|
prefix?: string;
|
||||||
|
},
|
||||||
|
): void {
|
||||||
|
const message = this.getReason();
|
||||||
|
const logmessage =
|
||||||
|
message + (this.getDebugMessage() ? ' - ' + this.getDebugMessage() : '');
|
||||||
|
|
||||||
|
const prefix = options?.prefix ? options.prefix + ' ' : '';
|
||||||
|
const logline = `${prefix}${this.getName()}: ${logmessage}`;
|
||||||
|
|
||||||
|
if (this.isImportant() && options?.notImportant !== true) {
|
||||||
|
logger.error(logline);
|
||||||
|
const stack = this.getStack();
|
||||||
|
if (stack) {
|
||||||
|
logger.debug(stack);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.warn(logline);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toString(): string {
|
||||||
|
return (
|
||||||
|
`${this.getName()}: ${this.getReason()} - (${this.debugMessage})` +
|
||||||
|
(this.isImportant() ? '\n' + this.stack : '')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
toError(): Error {
|
||||||
|
const error = new Error();
|
||||||
|
(error as any).message = this;
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
static deserialize(data: any): Failure {
|
static deserialize(data: any): Failure {
|
||||||
|
@ -251,10 +289,10 @@ export function ThrowIfFailed<V>(failable: Failable<V>): V {
|
||||||
export function FallbackIfFailed<V>(
|
export function FallbackIfFailed<V>(
|
||||||
failable: Failable<V>,
|
failable: Failable<V>,
|
||||||
fallback: V,
|
fallback: V,
|
||||||
logger?: { warn: (...args: any) => any },
|
logger?: ILogger,
|
||||||
): V {
|
): V {
|
||||||
if (HasFailed(failable)) {
|
if (HasFailed(failable)) {
|
||||||
if (logger) logger.warn(failable.print());
|
if (logger) failable.print(logger, { notImportant: true });
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue