Edit some logging behaviour

This commit is contained in:
Rubikscraft 2022-12-26 15:34:04 +01:00
parent 844d11b9ca
commit 4566fa947d
10 changed files with 65 additions and 55 deletions

View File

@ -14,7 +14,7 @@ export class InfoConfigService {
SysPreference.HostOverride,
);
if (HasFailed(hostname)) {
this.logger.warn(hostname.print());
hostname.print(this.logger);
return undefined;
}

View File

@ -26,7 +26,7 @@ export class MultiPartPipe implements PipeTransform {
) {
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({
limits: this.multipartConfigService.getLimits(filesLimit),

View File

@ -6,7 +6,7 @@ import {
Logger,
MethodNotAllowedException,
NotFoundException,
UnauthorizedException,
UnauthorizedException
} from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';
import { ApiErrorResponse } from 'picsur-shared/dist/dto/api/api.dto';
@ -14,7 +14,7 @@ import {
Fail,
Failure,
FT,
IsFailure,
IsFailure
} from 'picsur-shared/dist/types/failable';
// 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 type = exception.getType();
const message = exception.getReason();
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}`,
);
}
exception.print(MainExceptionFilter.logger, { prefix: traceString });
const toSend: ApiErrorResponse = {
success: false,
@ -65,7 +49,7 @@ export class MainExceptionFilter implements ExceptionFilter {
data: {
type,
message,
message: exception.getReason(),
},
};

View File

@ -4,7 +4,7 @@ import fastifyReplyFrom from '@fastify/reply-from';
import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
NestFastifyApplication
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { HostConfigService } from './config/early/host.config.service';
@ -39,14 +39,17 @@ async function bootstrap() {
fastifyAdapter,
{
bufferLogs: isProduction,
autoFlushLogs: true,
},
);
// Configure logger
app.useLogger(app.get(PicsurLoggerService));
const logger = app.get(PicsurLoggerService)
app.useLogger(logger);
app.flushLogs();
console.log(logger);
app.useGlobalFilters(app.get(MainExceptionFilter));
app.useGlobalInterceptors(app.get(SuccessInterceptor));
app.useGlobalPipes(app.get(ZodValidationPipe));

View File

@ -3,7 +3,7 @@ import ms from 'ms';
import { ImageRequestParams } from 'picsur-shared/dist/dto/api/image.dto';
import {
FileType,
SupportedFileTypeCategory,
SupportedFileTypeCategory
} from 'picsur-shared/dist/dto/mimes.dto';
import { SysPreference } from 'picsur-shared/dist/dto/sys-preferences.enum';
import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types';
@ -122,16 +122,4 @@ export class ImageConverterService {
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,
};
}
}

View File

@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import {
FileType,
ImageFileType,
SupportedFileTypeCategory,
SupportedFileTypeCategory
} from 'picsur-shared/dist/dto/mimes.dto';
import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types';

View File

@ -19,7 +19,7 @@ import { ImageManagerService } from './image.service';
ImageProcessorService,
ImageConverterService,
],
exports: [ImageManagerService],
exports: [ImageManagerService, ImageConverterService],
})
export class ImageManagerModule implements OnModuleInit {
private readonly logger = new Logger(ImageManagerModule.name);
@ -31,7 +31,7 @@ export class ImageManagerModule implements OnModuleInit {
) {}
async onModuleInit() {
await this.imageManagerCron()
await this.imageManagerCron();
}
@Interval(1000 * 60)
@ -57,7 +57,7 @@ export class ImageManagerModule implements OnModuleInit {
const result = await this.imageFileDB.cleanupDerivatives(after_ms / 1000);
if (HasFailed(result)) {
this.logger.warn(result.print());
result.print(this.logger);
}
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();
if (HasFailed(cleanedUp)) {
this.logger.warn(cleanedUp.print());
cleanedUp.print(this.logger);
return;
}
if (cleanedUp > 0)

View File

@ -64,7 +64,7 @@ export class SettingsShareXComponent implements OnInit {
const ext = FileType2Ext(this.selectedFormat);
if (HasFailed(ext)) {
this.logger.error(ext.print());
ext.print(this.logger);
}
const sharexConfig = BuildShareX(

View File

@ -15,11 +15,7 @@ export class ErrorService {
) {}
public showFailure(error: Failure, logger: Logger): void {
if (error.isImportant()) {
logger.error(error.print());
} else {
logger.warn(error.print());
}
error.print(logger);
this.snackbar.showSnackBar(
error.getReason(),

View File

@ -21,6 +21,12 @@ export enum FT {
Network = 'network',
}
interface ILogger {
error: (message: string) => void;
warn: (message: string) => void;
debug: (message: string) => void;
}
interface FTProp {
important: boolean;
code: number;
@ -142,10 +148,42 @@ export class Failure {
return FTProps[this.type].important;
}
print(): string {
return `${this.getName()}: ${this.getReason()}\n(${
this.debugMessage
})\n${this.getStack()}`;
print(
logger: ILogger,
options?: {
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 {
@ -251,10 +289,10 @@ export function ThrowIfFailed<V>(failable: Failable<V>): V {
export function FallbackIfFailed<V>(
failable: Failable<V>,
fallback: V,
logger?: { warn: (...args: any) => any },
logger?: ILogger,
): V {
if (HasFailed(failable)) {
if (logger) logger.warn(failable.print());
if (logger) failable.print(logger, { notImportant: true });
return fallback;
}