import winston, { Logger, LoggerOptions } from "winston"; /** * Audit information to describe who is accessing a sensitive or confidential resource. */ export interface AuditInformation { /** * Application name that groups different physical systems into a single logical group. * For example, this may be the front end system name so that multiple backend system logs can be grouped together. */ application?: string; /** * The ID of the user who is accessing the resource. */ accessedBy: string; /** * The action taken on the resource. */ action: 'read' | 'update' | 'create' | 'delete'; /** * The ID of the owner of the resource information. */ ownerId: string; /** * The type of resource accessed. For example, "application" or "enrolment". * By convention this value should be singular. */ resourceType: string; /** * The unique resource identifier. For example, the application number. * This is not required if the resource identifier is the same as the owner. */ resourceId?: string; } export class UoaLogger { logger: Logger; constructor(options: LoggerOptions) { this.logger = winston.createLogger(options); } /** * Function to log debug level messages. * @param {string} message - Debug message. */ public debug(message: string) { this.logger.debug(message); } /** * Function to log info level messages. * @param {string} message - Information message. */ public info(message: string) { this.logger.info(message); } /** * Function to log audit level messages. * @param {AuditInformation} auditInformation - Audit Information object containing relevant details. * @param {string=} message - Audit message (optional). */ public audit(auditInformation: AuditInformation, message?: string): void { // Initialize logMessage with mandatory fields let logMessage: string = `application:${ auditInformation.application ? auditInformation.application.replace(/\s/g, "") : "-" } ` + `accessedBy:${auditInformation.accessedBy.replace(/\s/g, "")} ` + `action:${auditInformation.action.toString().replace(/\s/g, "")} ` + `owner:${auditInformation.ownerId.replace(/\s/g, "")} ` + `resourceType:${auditInformation.resourceType.replace(/\s/g, "")} ` + `resourceId:${ auditInformation.resourceId ? auditInformation.resourceId.replace(/\s/g, "") : "-" }`; // Append additional message if provided if (message) { logMessage += ` ${message}`; } this.logger.log("audit", logMessage); } /** * Function to log warn level messages. * @param {string} message - Warn message. */ public warn(message: string) { this.logger.warn(message); } /** * Function to log error level messages. * @param {string} message - Error message. */ public error(message: string) { this.logger.error(message); } }