introduce the logging module
This commit is contained in:
110
src/logging/logging.controller.ts
Normal file
110
src/logging/logging.controller.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Delete,
|
||||
Query,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { ApiQuery, ApiResponse } from '@nestjs/swagger';
|
||||
import { LogEntryDto } from './dto/logentry.dto';
|
||||
import { LoggingService } from './logging.service';
|
||||
import { LogEntry } from './models/logentry.model';
|
||||
|
||||
@Controller('logging')
|
||||
export class LoggingController {
|
||||
constructor(private readonly loggingService: LoggingService) {}
|
||||
|
||||
private static logEntryToDto(entry: LogEntry): LogEntryDto {
|
||||
return {
|
||||
component: entry.component,
|
||||
level: entry.level,
|
||||
timestamp: entry.timestamp,
|
||||
message: entry.message,
|
||||
};
|
||||
}
|
||||
|
||||
@Get('/all')
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: 'All log entries',
|
||||
type: [LogEntryDto],
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 404,
|
||||
description: 'No messages found',
|
||||
})
|
||||
async allMessages(): Promise<LogEntryDto[]> {
|
||||
return this.loggingService.allMessages().then((response) => {
|
||||
if (response === null) {
|
||||
throw new HttpException(
|
||||
'Unable to find the log messages',
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const messages: LogEntryDto[] = [];
|
||||
response.forEach((entry) =>
|
||||
messages.push(LoggingController.logEntryToDto(entry)),
|
||||
);
|
||||
return messages;
|
||||
});
|
||||
}
|
||||
|
||||
@Get('/component')
|
||||
@ApiQuery({
|
||||
name: 'component',
|
||||
description: 'The component filter for the messages',
|
||||
type: String,
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: 'All log entries',
|
||||
type: [LogEntryDto],
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 404,
|
||||
description: 'No messages found',
|
||||
})
|
||||
async componentMessages(
|
||||
@Query('component') component: string,
|
||||
): Promise<LogEntryDto[]> {
|
||||
return this.loggingService.componentMessages(component).then((response) => {
|
||||
if (response === null) {
|
||||
throw new HttpException(
|
||||
'Unable to find the log messages',
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const messages: LogEntryDto[] = [];
|
||||
response.forEach((entry) =>
|
||||
messages.push(LoggingController.logEntryToDto(entry)),
|
||||
);
|
||||
return messages;
|
||||
});
|
||||
}
|
||||
|
||||
@Delete('/deleteAll')
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'All log entries are deleted',
|
||||
})
|
||||
async deleteAll(): Promise<void> {
|
||||
return this.loggingService.deleteAllMessages();
|
||||
}
|
||||
|
||||
@Delete('/deleteComponent')
|
||||
@ApiQuery({
|
||||
name: 'component',
|
||||
description: 'The component filter for the messages',
|
||||
type: String,
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'All log entries are deleted',
|
||||
})
|
||||
async deleteComponent(@Query('component') component: string): Promise<void> {
|
||||
return this.loggingService.deleteComponentMessages(component);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user