| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  | import * as core from '@actions/core'; | 
					
						
							| 
									
										
										
										
											2022-01-01 18:36:17 +00:00
										 |  |  | import { Input } from '../..'; | 
					
						
							|  |  |  | const { Logging } = Input.cloudRunnerTests ? require('@google-cloud/logging') : Input.cloudRunnerTests; | 
					
						
							| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  | class CloudRunnerLogger { | 
					
						
							| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  |   private static timestamp: number; | 
					
						
							|  |  |  |   private static globalTimestamp: number; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public static setup() { | 
					
						
							|  |  |  |     this.timestamp = this.createTimestamp(); | 
					
						
							|  |  |  |     this.globalTimestamp = this.timestamp; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  |   public static log(message: string) { | 
					
						
							| 
									
										
										
										
											2022-01-01 18:36:17 +00:00
										 |  |  |     this.logToGoogle(message); | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  |     core.info(message); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-31 20:12:47 +00:00
										 |  |  |   public static logWarning(message: string) { | 
					
						
							| 
									
										
										
										
											2022-01-01 18:36:17 +00:00
										 |  |  |     this.logToGoogle(message); | 
					
						
							| 
									
										
										
										
											2021-12-31 20:12:47 +00:00
										 |  |  |     core.warning(message); | 
					
						
							| 
									
										
										
										
											2021-12-29 14:35:13 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 01:45:55 +00:00
										 |  |  |   public static logLine(message: string) { | 
					
						
							| 
									
										
										
										
											2022-01-01 18:36:17 +00:00
										 |  |  |     this.logToGoogle(message); | 
					
						
							| 
									
										
										
										
											2021-12-19 01:45:55 +00:00
										 |  |  |     core.info(`${message}\n`); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  |   public static error(message: string) { | 
					
						
							| 
									
										
										
										
											2022-01-01 18:36:17 +00:00
										 |  |  |     this.logToGoogle(message); | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  |     core.error(message); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  |   public static logWithTime(message: string) { | 
					
						
							|  |  |  |     const newTimestamp = this.createTimestamp(); | 
					
						
							|  |  |  |     core.info( | 
					
						
							|  |  |  |       `${message} (Since previous: ${this.calculateTimeDiff( | 
					
						
							|  |  |  |         newTimestamp, | 
					
						
							|  |  |  |         this.timestamp, | 
					
						
							|  |  |  |       )}, Total time: ${this.calculateTimeDiff(newTimestamp, this.globalTimestamp)})`,
 | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     this.timestamp = newTimestamp; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static calculateTimeDiff(x: number, y: number) { | 
					
						
							|  |  |  |     return Math.floor((x - y) / 1000); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static createTimestamp() { | 
					
						
							|  |  |  |     return Date.now(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-01-01 18:36:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private static logToGoogle(message: string) { | 
					
						
							|  |  |  |     const projectId = process.env.GCP_PROJECT; | 
					
						
							|  |  |  |     const logName = process.env.GCP_LOG_NAME; | 
					
						
							|  |  |  |     // GCP only setup as dev dependency
 | 
					
						
							|  |  |  |     if (!Input.cloudRunnerTests) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Creates a client
 | 
					
						
							|  |  |  |     const logging = new Logging({ projectId }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Selects the log to write to
 | 
					
						
							|  |  |  |     const log = logging.log(logName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The data to write to the log
 | 
					
						
							|  |  |  |     const text = message; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The metadata associated with the entry
 | 
					
						
							|  |  |  |     const metadata = { | 
					
						
							|  |  |  |       resource: { type: 'global' }, | 
					
						
							|  |  |  |       // See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity
 | 
					
						
							|  |  |  |       severity: 'INFO', | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Prepares a log entry
 | 
					
						
							|  |  |  |     const entry = log.entry(metadata, text); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function writeLog() { | 
					
						
							|  |  |  |       // Writes the log entry
 | 
					
						
							|  |  |  |       await log.write(entry); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     writeLog(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  | export default CloudRunnerLogger; |