|
| 1 | +import {debug, info, warning, error} from '@actions/core'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Utility functions for standardized logging across the application. |
| 5 | + * These functions help provide consistent, detailed diagnostic information. |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Logs detailed information about an error. |
| 10 | + * |
| 11 | + * @param message A descriptive message about where/when the error occurred |
| 12 | + * @param err The error object |
| 13 | + * @param context Optional additional context information |
| 14 | + */ |
| 15 | +export function logError(message: string, err: unknown, context?: Record<string, unknown>): void { |
| 16 | + error(`ERROR: ${message}`); |
| 17 | + |
| 18 | + // Log the error message |
| 19 | + error(`Error message: ${String(err)}`); |
| 20 | + |
| 21 | + // Log the stack trace if available |
| 22 | + if (err instanceof Error && err.stack) { |
| 23 | + error(`Stack trace: ${err.stack}`); |
| 24 | + } |
| 25 | + |
| 26 | + // Log all properties of the error object |
| 27 | + try { |
| 28 | + error(`Error details: ${JSON.stringify(err, Object.getOwnPropertyNames(err))}`); |
| 29 | + } catch (jsonError) { |
| 30 | + error(`Could not stringify error: ${String(jsonError)}`); |
| 31 | + } |
| 32 | + |
| 33 | + // Log additional context if provided |
| 34 | + if (context) { |
| 35 | + try { |
| 36 | + error(`Error context: ${JSON.stringify(context)}`); |
| 37 | + } catch (jsonError) { |
| 38 | + error(`Could not stringify context: ${String(jsonError)}`); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Logs detailed debug information. |
| 45 | + * |
| 46 | + * @param component The component/module name for context |
| 47 | + * @param message The debug message |
| 48 | + * @param data Optional data to include in the log |
| 49 | + */ |
| 50 | +export function logDebug(component: string, message: string, data?: unknown): void { |
| 51 | + const prefix = component ? `[${component}] ` : ''; |
| 52 | + |
| 53 | + debug(`${prefix}${message}`); |
| 54 | + |
| 55 | + if (data !== undefined) { |
| 56 | + try { |
| 57 | + if (typeof data === 'string') { |
| 58 | + debug(`${prefix}Data: ${data}`); |
| 59 | + } else { |
| 60 | + debug(`${prefix}Data: ${JSON.stringify(data)}`); |
| 61 | + } |
| 62 | + } catch (jsonError) { |
| 63 | + debug(`${prefix}Could not stringify data: ${String(jsonError)}`); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Logs information about a pull request. |
| 70 | + * |
| 71 | + * @param component The component/module name for context |
| 72 | + * @param message A descriptive message |
| 73 | + * @param pullRequest The pull request information |
| 74 | + */ |
| 75 | +export function logPullRequestInfo( |
| 76 | + component: string, |
| 77 | + message: string, |
| 78 | + pullRequest: { |
| 79 | + number: number; |
| 80 | + ownerName: string; |
| 81 | + repoName: string; |
| 82 | + [key: string]: unknown; |
| 83 | + }, |
| 84 | +): void { |
| 85 | + const prefix = component ? `[${component}] ` : ''; |
| 86 | + const prId = `PR #${pullRequest.number} in ${pullRequest.ownerName}/${pullRequest.repoName}`; |
| 87 | + |
| 88 | + info(`${prefix}${message} - ${prId}`); |
| 89 | + |
| 90 | + // Log detailed PR information at debug level |
| 91 | + try { |
| 92 | + debug(`${prefix}Pull request details: ${JSON.stringify(pullRequest)}`); |
| 93 | + } catch (jsonError) { |
| 94 | + debug(`${prefix}Could not stringify pull request: ${String(jsonError)}`); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Logs a warning with context information. |
| 100 | + * |
| 101 | + * @param component The component/module name for context |
| 102 | + * @param message The warning message |
| 103 | + * @param data Optional data to include in the log |
| 104 | + */ |
| 105 | +export function logWarning(component: string, message: string, data?: unknown): void { |
| 106 | + const prefix = component ? `[${component}] ` : ''; |
| 107 | + |
| 108 | + warning(`${prefix}${message}`); |
| 109 | + |
| 110 | + if (data !== undefined) { |
| 111 | + try { |
| 112 | + if (typeof data === 'string') { |
| 113 | + debug(`${prefix}Warning data: ${data}`); |
| 114 | + } else { |
| 115 | + debug(`${prefix}Warning data: ${JSON.stringify(data)}`); |
| 116 | + } |
| 117 | + } catch (jsonError) { |
| 118 | + debug(`${prefix}Could not stringify warning data: ${String(jsonError)}`); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments