forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
144 lines (130 loc) · 3.97 KB
/
Copy pathlogger.ts
File metadata and controls
144 lines (130 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Structured Logger for Antigravity Plugin
*
* Provides TUI-integrated logging that is silent by default.
* Logs are only visible when:
* 1. TUI client is available (logs to app log panel)
* 2. OPENCODE_ANTIGRAVITY_CONSOLE_LOG=1 is set (logs to console)
*
* Ported from opencode-google-antigravity-auth/src/plugin/logger.ts
*/
import type { PluginClient } from "./types";
type LogLevel = "debug" | "info" | "warn" | "error";
const ENV_CONSOLE_LOG = "OPENCODE_ANTIGRAVITY_CONSOLE_LOG";
const ANTIGRAVITY_CONSOLE_PREFIX = "[Antigravity]";
export interface Logger {
debug(message: string, extra?: Record<string, unknown>): void;
info(message: string, extra?: Record<string, unknown>): void;
warn(message: string, extra?: Record<string, unknown>): void;
error(message: string, extra?: Record<string, unknown>): void;
}
let _client: PluginClient | null = null;
/**
* Check if console logging is enabled via environment variable.
*/
function isConsoleLogEnabled(): boolean {
const val = process.env[ENV_CONSOLE_LOG];
return val === "1" || val?.toLowerCase() === "true";
}
/**
* Initialize the logger with the plugin client.
* Must be called during plugin initialization to enable TUI logging.
*/
export function initLogger(client: PluginClient): void {
_client = client;
}
/**
* Get the current client (for testing or advanced usage).
*/
export function getLoggerClient(): PluginClient | null {
return _client;
}
/**
* Create a logger instance for a specific module.
*
* @param module - The module name (e.g., "refresh-queue", "transform.claude")
* @returns Logger instance with debug, info, warn, error methods
*
* @example
* ```typescript
* const log = createLogger("refresh-queue");
* log.debug("Checking tokens", { count: 5 });
* log.warn("Token expired", { accountIndex: 0 });
* ```
*/
export function createLogger(module: string): Logger {
const service = `antigravity.${module}`;
const log = (level: LogLevel, message: string, extra?: Record<string, unknown>): void => {
// Try TUI logging first
const app = _client?.app;
if (app && typeof app.log === "function") {
app
.log({
body: { service, level, message, extra },
})
.catch(() => {
// Silently ignore logging errors
});
} else if (isConsoleLogEnabled()) {
// Fallback to console if env var is set
const prefix = `[${service}]`;
const args = extra ? [prefix, message, extra] : [prefix, message];
switch (level) {
case "debug":
console.debug(...args);
break;
case "info":
console.info(...args);
break;
case "warn":
console.warn(...args);
break;
case "error":
console.error(...args);
break;
}
}
// If neither TUI nor console logging is enabled, log is silently discarded
};
return {
debug: (message, extra) => log("debug", message, extra),
info: (message, extra) => log("info", message, extra),
warn: (message, extra) => log("warn", message, extra),
error: (message, extra) => log("error", message, extra),
};
}
/**
* Print a message to the console with Antigravity prefix.
* Only outputs when OPENCODE_ANTIGRAVITY_CONSOLE_LOG=1 is set.
*
* Use this for standalone messages that don't belong to a specific module.
*
* @param level - Log level
* @param message - Message to print
* @param extra - Optional extra data
*/
export function printAntigravityConsole(
level: LogLevel,
message: string,
extra?: unknown,
): void {
if (!isConsoleLogEnabled()) {
return;
}
const prefixedMessage = `${ANTIGRAVITY_CONSOLE_PREFIX} ${message}`;
const args = extra === undefined ? [prefixedMessage] : [prefixedMessage, extra];
switch (level) {
case "debug":
console.debug(...args);
break;
case "info":
console.info(...args);
break;
case "warn":
console.warn(...args);
break;
case "error":
console.error(...args);
break;
}
}