codemod/replace-console-log-with-vivid-trail-rj9w
Replace console.log with OTel logger
# @codemod/replace-console-log-with-vivid-trail-rj9w
Replace console.log with OTel logger
## What It Does
This codemod rewrites standalone `console.log(...)` statement calls in the server-side parts of `codemod/codemod-app` to the repository's existing OpenTelemetry logger API from `@codemod.com/otel`.
It uses the repo's established shape:
```ts
logger.info({ message: "...", attributes?: { ... } });
```
## Target Paths
- `apps/worker/src/**`
- `packages/api/src/**`
- `apps/nextjs/src/instrumentation.*`
- `apps/nextjs/src/instrumentation-node.*`
- `apps/nextjs/src/middleware.*`
## Skips
- comments and string literals that merely mention `console.log`
- `console.log` calls used inside larger expressions instead of standalone statements
- client-side Next.js files with a top-level `"use client"` directive
- `apps/nextjs/src/app/(studio)/studio/_hooks/_data/default/**`
- `apps/nextjs/scripts/**`
- `apps/website/**`
- `apps/promotional-videos/**`
- files outside the repo areas that already show `@codemod.com/otel` usage conventions
## Rewrite Rules
### Single message
```ts
console.log("hello");
```
becomes:
```ts
logger.info({ message: "hello" });
```
### String or template message with extra args
```ts
console.log("event", event);
console.log("[Github Webhook] Installation created", { installationId: id });
```
becomes:
```ts
logger.info({
message: "event",
attributes: {
console_log_arg_1: event,
},
});
logger.info({
message: "[Github Webhook] Installation created",
attributes: {
console_log_arg_1: { installationId: id },
},
});
```
### No leading string-ish message
```ts
console.log(value, other);
```
becomes:
```ts
logger.info({
message: "console.log",
attributes: {
console_log_arg_0: value,
console_log_arg_1: other,
},
});
```
## Import Handling
- reuses an existing `logger` import from `@codemod.com/otel`
- extends an existing `@codemod.com/otel` named import when `logger` is missing
- adds a new `@codemod.com/otel` import when needed
- aliases to `logger as otelLogger` when the file already binds `logger`
## Assumptions
- The codemod is intentionally repository-aware, not a generic `console.log` migration.
- It only rewrites files where using `@codemod.com/otel` is consistent with the repo's current server-side logging conventions.
- For Next.js, it stays conservative and only targets clearly server-side entry files.
## Installation
```bash
# Install from registry
codemod run @codemod/replace-console-log-with-vivid-trail-rj9w
# Or run locally
codemod run -w workflow.yaml
```
## Usage
```bash
codemod run @codemod/replace-console-log-with-vivid-trail-rj9w
```
## Development
```bash
# Test the transformation
pnpm test
# Validate the package
codemod workflow validate -w workflow.yaml
pnpm check-types
# Publish to registry
codemod login
codemod publish
```
## License
MIT