log-shape
v0.3.0
Published
[](https://www.npmjs.com/package/log-shape) [](https://opensource.org/licenses/MIT)
Readme
log-shape
log-shape is a TypeScript CLI that scans JS/TS source files and highlights inconsistent logging patterns. It uses static regex analysis to classify console.*, logger.*, pino.*, and winston.* calls, then reports mixed styles and distribution across the codebase.
Install
pnpm add -D log-shapeOr run locally from this repo:
pnpm install
pnpm build
node dist/index.js src/Usage
log-shape <path> [options]Options:
--jsonJSON output--no-failDon't exit with status code1on inconsistent files--ext <exts>Comma-separated extensions to scan, default:.ts,.js,.tsx,.jsx--ignore <pat>Comma-separated glob patterns to ignore, default:node_modules,dist,test--config <path>Load.logshapercJSON config from the current working directory or a custom path--fixRewrite supportedconsole.*calls to a consistent target style--target <style>Rewrite target:structuredortemplate, default:structured--migrate <style>Rewrite supported log calls topino,winston,bunyan, orconsolestyle--report <mode>Report output mode:text,json, orhtml--dry-runPreview rewrites without modifying files--suggestShow pino/winston migration snippets
Example:
log-shape src/ --suggestFix mode example:
log-shape src/ --fix --target structured --dry-runMigration mode example:
log-shape src/ --migrate pinoReport mode examples:
log-shape src/ --report json
log-shape src/ --report html > log-report.htmlWhat It Detects
Each supported log call is classified into one of these styles:
structuredlogger.info({ msg: "ok" })string-concatconsole.log("user:", user)template-literalconsole.log(`token=${token}`)raw-dumpconsole.log(obj)plain-stringconsole.log("simple message")
Files with more than two distinct styles are flagged as inconsistent.
You can tighten that rule with config by setting maxMixedStyles, or explicitly allow only certain styles with allowStyles.
Config
log-shape automatically loads .logshaperc from the current working directory when present. You can also point at a specific file with --config.
Example:
{
"logger": "pino",
"allowStyles": ["structured"],
"ignoreFiles": ["src/legacy/**"],
"maxMixedStyles": 1
}Fields:
logger: logger family to emit during--fixrewrites.pinoandwinstonemitlogger.info(...)/logger.error(...);consolekeepsconsole.*(...)allowStyles: styles considered acceptable during reportingignoreFiles: additional glob patterns to skip during scan/fixmaxMixedStyles: maximum distinct styles allowed per file before it is flagged
How It Works
log-shape uses line-based static regex analysis. It does not execute code or build a full AST. This keeps the CLI fast and simple, but means it is intentionally best-effort for common single-line logging patterns.
Round 3
--migrate <style>rewrites recognized single-line logging calls and adds or removesimport logger from "...";when needed--report jsonexposes asummaryblock with total calls, style counts, inconsistent files, and a heuristic score--report htmlemits a standalone HTML report suitable for CI artifacts
Migration Guide
If mixed styles are detected, move toward structured logging:
Pino
import pino from "pino";
const logger = pino();
logger.info({ msg: "starting", port });
logger.error({ msg: "db failed", err });Winston
import winston from "winston";
const logger = winston.createLogger({
transports: [new winston.transports.Console()]
});
logger.info({ msg: "starting", port });
logger.error({ msg: "db failed", err });Development
pnpm install
pnpm build
pnpm test