|
| 1 | +import { Logging } from "@google-cloud/logging"; |
| 2 | +import { getProjectId } from "../metadataService"; |
| 3 | + |
| 4 | +type FunctionType = "action" | "connector" | "derivative"; |
| 5 | + |
| 6 | +interface RowyLogging { |
| 7 | + log: (payload: any) => void; |
| 8 | + warn: (payload: any) => void; |
| 9 | + error: (payload: any) => void; |
| 10 | +} |
| 11 | + |
| 12 | +class LoggingFactory { |
| 13 | + public static async createActionLogging(fieldName: string, rowId: string) { |
| 14 | + const projectId = await getProjectId(); |
| 15 | + return new LoggingFieldAndRow(projectId, fieldName, rowId, "action"); |
| 16 | + } |
| 17 | + |
| 18 | + public static async createConnectorLogging(fieldName: string, rowId: string) { |
| 19 | + const projectId = await getProjectId(); |
| 20 | + return new LoggingFieldAndRow(projectId, fieldName, rowId, "connector"); |
| 21 | + } |
| 22 | + |
| 23 | + public static async createDerivativeLogging( |
| 24 | + fieldName: string, |
| 25 | + rowId: string |
| 26 | + ) { |
| 27 | + const projectId = await getProjectId(); |
| 28 | + return new LoggingFieldAndRow(projectId, fieldName, rowId, "derivative"); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class LoggingAbstract implements RowyLogging { |
| 33 | + protected readonly functionType; |
| 34 | + protected readonly logging: Logging; |
| 35 | + |
| 36 | + constructor(projectId: string, functionType: FunctionType) { |
| 37 | + this.functionType = functionType; |
| 38 | + this.logging = new Logging({ projectId }); |
| 39 | + } |
| 40 | + |
| 41 | + protected async logWithSeverity(payload: any, severity: string) { |
| 42 | + throw new Error("logWithSeverity must be implemented"); |
| 43 | + } |
| 44 | + |
| 45 | + async log(payload: any) { |
| 46 | + await this.logWithSeverity(payload, "DEFAULT"); |
| 47 | + } |
| 48 | + |
| 49 | + async warn(payload: any) { |
| 50 | + await this.logWithSeverity(payload, "WARNING"); |
| 51 | + } |
| 52 | + |
| 53 | + async error(payload: any) { |
| 54 | + await this.logWithSeverity(payload, "ERROR"); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class LoggingFieldAndRow extends LoggingAbstract implements RowyLogging { |
| 59 | + private readonly fieldName: string; |
| 60 | + private readonly rowId: string; |
| 61 | + |
| 62 | + constructor( |
| 63 | + projectId: string, |
| 64 | + fieldName: string, |
| 65 | + rowId: string, |
| 66 | + functionType: FunctionType |
| 67 | + ) { |
| 68 | + super(projectId, functionType); |
| 69 | + this.fieldName = fieldName; |
| 70 | + this.rowId = rowId; |
| 71 | + } |
| 72 | + |
| 73 | + async logWithSeverity(payload: any, severity: string) { |
| 74 | + const log = this.logging.log(`rowy-logging`); |
| 75 | + const metadata = { |
| 76 | + severity, |
| 77 | + }; |
| 78 | + const payloadSize = JSON.stringify(payload).length; |
| 79 | + const entry = log.entry(metadata, { |
| 80 | + loggingSource: "backend-scripts", |
| 81 | + functionType: this.functionType, |
| 82 | + fieldName: this.fieldName, |
| 83 | + rowId: this.rowId, |
| 84 | + payload: payloadSize > 250000 ? { v: "payload too large" } : payload, |
| 85 | + }); |
| 86 | + await log.write(entry); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export { LoggingFactory, RowyLogging }; |
0 commit comments