cloudflare/kumo
Publicmirrored fromhttps://github.com/cloudflare/kumoAvailable
packages/kumo-figma/src/logger.ts
165lines ยท modecode
| 1 | /** |
| 2 | * Structured logging utility for Figma plugin |
| 3 | * |
| 4 | * Provides log levels (DEBUG, INFO, WARN, ERROR) with configurable verbosity. |
| 5 | * In production, only WARN and ERROR are shown by default. |
| 6 | * |
| 7 | * Semantic logging functions with emojis for consistent output: |
| 8 | * - logComplete: โ
Task completed successfully |
| 9 | * - logStart: ๐จ Starting a generation task |
| 10 | * - logProgress: ๐ฆ Progress update during generation |
| 11 | * - logFound: ๐ Found/loaded resources |
| 12 | * |
| 13 | * ES2020 compatible for Figma plugin runtime. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * Log levels in order of severity |
| 18 | */ |
| 19 | export enum LogLevel { |
| 20 | DEBUG = 0, |
| 21 | INFO = 1, |
| 22 | WARN = 2, |
| 23 | ERROR = 3, |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Current log level - only logs at this level or higher will be shown |
| 28 | * Default: INFO (shows INFO, WARN, ERROR but not DEBUG) |
| 29 | * |
| 30 | * To enable DEBUG logs: Set LOG_LEVEL = LogLevel.DEBUG |
| 31 | * For production: Set LOG_LEVEL = LogLevel.WARN |
| 32 | */ |
| 33 | export var LOG_LEVEL: LogLevel = LogLevel.INFO; |
| 34 | |
| 35 | /** |
| 36 | * Set the current log level |
| 37 | */ |
| 38 | export function setLogLevel(level: LogLevel): void { |
| 39 | LOG_LEVEL = level; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Internal logging function (no prefix, just logs the message) |
| 44 | */ |
| 45 | function log(level: LogLevel, ...args: unknown[]): void { |
| 46 | if (level < LOG_LEVEL) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | switch (level) { |
| 51 | case LogLevel.DEBUG: |
| 52 | case LogLevel.INFO: |
| 53 | console.log(...args); |
| 54 | break; |
| 55 | case LogLevel.WARN: |
| 56 | console.warn(...args); |
| 57 | break; |
| 58 | case LogLevel.ERROR: |
| 59 | console.error(...args); |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Log debug message (verbose, development only) |
| 66 | * Hidden by default, enable with setLogLevel(LogLevel.DEBUG) |
| 67 | */ |
| 68 | export function logDebug(...args: unknown[]): void { |
| 69 | log(LogLevel.DEBUG, ...args); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Log informational message |
| 74 | * Shown by default in development |
| 75 | */ |
| 76 | export function logInfo(...args: unknown[]): void { |
| 77 | log(LogLevel.INFO, ...args); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Log warning message |
| 82 | * Always shown (production + development) |
| 83 | */ |
| 84 | export function logWarn(...args: unknown[]): void { |
| 85 | log(LogLevel.WARN, "โ ๏ธ", ...args); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Log error message |
| 90 | * Always shown (production + development) |
| 91 | */ |
| 92 | export function logError(...args: unknown[]): void { |
| 93 | log(LogLevel.ERROR, "โ", ...args); |
| 94 | } |
| 95 | |
| 96 | // ============================================================================ |
| 97 | // SEMANTIC LOGGING FUNCTIONS |
| 98 | // Use these for consistent, meaningful log output across generators |
| 99 | // ============================================================================ |
| 100 | |
| 101 | /** |
| 102 | * Log successful completion of a task |
| 103 | * Use at the end of generator functions |
| 104 | * |
| 105 | * @example logComplete("Generated Badge ComponentSet with 8 variants (light + dark)") |
| 106 | */ |
| 107 | export function logComplete(...args: unknown[]): void { |
| 108 | log(LogLevel.INFO, "โ
", ...args); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Log the start of a generation task |
| 113 | * Use at the beginning of generator functions |
| 114 | * |
| 115 | * @example logStart("Badge", "Y=100") |
| 116 | */ |
| 117 | export function logStart(component: string, context?: string): void { |
| 118 | var msg = component + ": Starting generation"; |
| 119 | if (context) { |
| 120 | msg += " at " + context; |
| 121 | } |
| 122 | log(LogLevel.INFO, "๐จ", msg); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Log progress during generation |
| 127 | * Use for intermediate steps like "Creating variant X" or "Combining as variants" |
| 128 | * |
| 129 | * @example logProgress("Badge", "Creating variant=info") |
| 130 | * @example logProgress("Badge", "Combining as variants...") |
| 131 | */ |
| 132 | export function logProgress(component: string, message: string): void { |
| 133 | log(LogLevel.INFO, "๐ฆ", component + ": " + message); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Log found/loaded resources |
| 138 | * Use when loading data, finding pages, etc. |
| 139 | * |
| 140 | * @example logFound("Found 535 icons in sprite") |
| 141 | * @example logFound("Found existing Components page") |
| 142 | */ |
| 143 | export function logFound(...args: unknown[]): void { |
| 144 | log(LogLevel.INFO, "๐", ...args); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Log creation of new resources |
| 149 | * Use when creating pages, files, etc. |
| 150 | * |
| 151 | * @example logCreate("Creating new Components page") |
| 152 | */ |
| 153 | export function logCreate(...args: unknown[]): void { |
| 154 | log(LogLevel.INFO, "๐", ...args); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Log cleanup/purge operations |
| 159 | * Use when removing old content |
| 160 | * |
| 161 | * @example logPurge("Purging 12 items from Components page") |
| 162 | */ |
| 163 | export function logPurge(...args: unknown[]): void { |
| 164 | log(LogLevel.INFO, "๐๏ธ", ...args); |
| 165 | } |
| 166 | |