( level: LogLevel, data: any, msg?: string, ...args: any[] )
| 120 | } |
| 121 | |
| 122 | function sendAnalyticsAndLog( |
| 123 | level: LogLevel, |
| 124 | data: any, |
| 125 | msg?: string, |
| 126 | ...args: any[] |
| 127 | ): void { |
| 128 | if (!IS_CI && !IS_TEST) { |
| 129 | let projectRoot: string | undefined |
| 130 | try { |
| 131 | projectRoot = getProjectRoot() |
| 132 | } catch { |
| 133 | projectRoot = undefined |
| 134 | } |
| 135 | if (projectRoot) { |
| 136 | const logTarget = |
| 137 | IS_DEV |
| 138 | ? path.join(projectRoot, 'debug', 'cli.jsonl') |
| 139 | : path.join(getCurrentChatDir(), 'log.jsonl') |
| 140 | |
| 141 | setLogPath(logTarget) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | const isStringOnly = typeof data === 'string' && msg === undefined |
| 146 | const normalizedData = isStringOnly ? undefined : data |
| 147 | const normalizedMsg = isStringOnly ? (data as string) : msg |
| 148 | const includeData = normalizedData != null && !isEmptyObject(normalizedData) |
| 149 | |
| 150 | const toTrack = { |
| 151 | ...(includeData ? { data: normalizedData } : {}), |
| 152 | level, |
| 153 | loggerContext, |
| 154 | msg: stringFormat(normalizedMsg, ...args), |
| 155 | } |
| 156 | |
| 157 | logAsErrorIfNeeded(toTrack) |
| 158 | |
| 159 | if (!IS_DEV && includeData && typeof normalizedData === 'object') { |
| 160 | const analyticsPayloads = analyticsDispatcher.process({ |
| 161 | data: normalizedData, |
| 162 | level, |
| 163 | msg: stringFormat(normalizedMsg ?? '', ...args), |
| 164 | fallbackUserId: loggerContext.userId, |
| 165 | }) |
| 166 | |
| 167 | analyticsPayloads.forEach((payload) => { |
| 168 | trackEvent(payload.event, payload.properties) |
| 169 | }) |
| 170 | } |
| 171 | |
| 172 | // Send all log events to PostHog in production for better observability |
| 173 | // Skip if the log already has an eventId (to avoid duplicate tracking) |
| 174 | const hasEventId = includeData && getAnalyticsEventId(normalizedData) !== null |
| 175 | if (!IS_DEV && !IS_TEST && !IS_CI && !hasEventId) { |
| 176 | const fullTelemetry = isFullTelemetryEnabled({ |
| 177 | distinctId: loggerContext.userId, |
| 178 | properties: loggerContext, |
| 179 | }) |
no test coverage detected